Documentation
¶
Overview ¶
Package telegramify converts standard (CommonMark/GFM) Markdown into the MarkdownV2 dialect understood by the Telegram Bot API.
Telegram's MarkdownV2 supports only a small subset of Markdown and requires many ASCII punctuation characters to be backslash-escaped; getting this wrong makes the Bot API reject the message. This package handles the escaping and maps unsupported constructs (headings, tables, images, task lists, ...) onto the closest Telegram-compatible representation, so you can feed it arbitrary Markdown and send the result with parse_mode = "MarkdownV2".
It is a Go port of the Python library telegramify-markdown (https://github.com/sudoskys/telegramify-markdown), covering the core Markdown -> MarkdownV2 string conversion and message splitting.
Basic usage:
text := telegramify.Markdownify("# Title\n\nHello **world**!")
// send text with parse_mode = "MarkdownV2"
Index ¶
- Constants
- func Markdownify(markdown string, opts ...Option) string
- func Split(text string, limit int) []string
- type Option
- func WithCiteExpandable(expandable bool) Option
- func WithExpandableThreshold(units int) Option
- func WithHeadingSymbols(symbols ...string) Option
- func WithHorizontalRule(rule string) Option
- func WithImageSymbol(symbol string) Option
- func WithTaskSymbols(done, todo string) Option
- func WithUnorderedMarker(marker string) Option
Examples ¶
Constants ¶
const DefaultMaxLength = 4096
DefaultMaxLength is Telegram's maximum message length in UTF-16 code units.
Variables ¶
This section is empty.
Functions ¶
func Markdownify ¶
Markdownify converts a Markdown string into Telegram MarkdownV2.
The result is safe to send to the Telegram Bot API with parse_mode = "MarkdownV2". Rendering can be tuned with Options such as WithHeadingSymbols or WithCiteExpandable.
Example ¶
package main
import (
"fmt"
telegramify "github.com/barbashov/telegramify-markdown-go"
)
func main() {
md := "## Shopping list\n\nDon't forget **milk** and `eggs`! Cost: $3.50."
fmt.Println(telegramify.Markdownify(md))
}
Output: ✏️ *__Shopping list__* Don't forget *milk* and `eggs`\! Cost: $3\.50\.
Example (Options) ¶
package main
import (
"fmt"
telegramify "github.com/barbashov/telegramify-markdown-go"
)
func main() {
out := telegramify.Markdownify("# Title", telegramify.WithHeadingSymbols("➤"))
fmt.Println(out)
}
Output: ➤ *__Title__*
func Split ¶
Split breaks text into chunks that each fit within limit UTF-16 code units (the unit Telegram measures message length in). If limit is <= 0, DefaultMaxLength is used.
Splitting happens at newline boundaries so that, as long as every individual block is shorter than the limit, no MarkdownV2 markup is broken across chunks. A single line longer than the limit is hard-split at rune boundaries as a last resort, which may break markup — keep individual blocks below the limit to avoid that.
Example ¶
package main
import (
"fmt"
telegramify "github.com/barbashov/telegramify-markdown-go"
)
func main() {
long := "first line\nsecond line\nthird line"
for _, chunk := range telegramify.Split(long, 22) {
fmt.Printf("%q\n", chunk)
}
}
Output: "first line\nsecond line" "third line"
Types ¶
type Option ¶
type Option func(*config)
Option configures how Markdownify renders Markdown into Telegram MarkdownV2.
func WithCiteExpandable ¶
WithCiteExpandable controls whether long blockquotes render as Telegram expandable blockquotes.
func WithExpandableThreshold ¶
WithExpandableThreshold sets the blockquote length, in UTF-16 code units, above which a blockquote becomes expandable. Has no effect unless WithCiteExpandable is enabled (the default).
func WithHeadingSymbols ¶
WithHeadingSymbols overrides the prefixes used for headings. Up to six symbols may be supplied, mapping to heading levels 1..6; missing levels keep their default. Pass an empty string to render a level with no prefix.
func WithHorizontalRule ¶
WithHorizontalRule sets the string emitted for a Markdown thematic break.
func WithImageSymbol ¶
WithImageSymbol sets the prefix used for the link that replaces an image.
func WithTaskSymbols ¶
WithTaskSymbols sets the markers used for completed and pending task list items.
func WithUnorderedMarker ¶
WithUnorderedMarker sets the bullet used for unordered list items.