Documentation
¶
Overview ¶
Package tgmd converts Markdown to Telegram-compatible plain text with MessageEntity objects.
Instead of producing MarkdownV2 strings (which require escaping 18 special characters with context-dependent rules), tgmd outputs plain text paired with entity objects that use UTF-16 offsets — matching what the Telegram Bot API expects natively.
All functions are safe for concurrent use.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SplitAtUTF16 ¶
SplitAtUTF16 splits s at the given UTF-16 offset. Returns the prefix (up to the offset) and the suffix (from the offset onward). If offset is beyond the string length, returns (s, "").
func UTF16Len ¶
UTF16Len returns the number of UTF-16 code units needed to encode s. BMP characters (< U+10000) take 1 unit; supplementary characters (emoji, etc.) take 2.
func UTF16RuneLen ¶
UTF16RuneLen returns the number of UTF-16 code units for a single rune.
Types ¶
type Entity ¶
type Entity struct {
Type EntityType
Offset int // UTF-16 offset from start of text
Length int // length in UTF-16 code units
URL string // only for TextLink
Language string // only for Pre (fenced code block language)
}
Entity represents a Telegram MessageEntity. Offset and Length are in UTF-16 code units (Telegram's native encoding).
type EntityType ¶
type EntityType string
EntityType represents the type of a Telegram message entity. Values match Telegram Bot API MessageEntity type strings.
const ( Bold EntityType = "bold" Italic EntityType = "italic" Underline EntityType = "underline" Strikethrough EntityType = "strikethrough" Code EntityType = "code" Pre EntityType = "pre" TextLink EntityType = "text_link" Blockquote EntityType = "blockquote" )
type Message ¶
Message is a single Telegram message: plain text plus formatting entities.
func Convert ¶
Convert parses Markdown text and returns plain text with Telegram entities.
Example ¶
package main
import (
"fmt"
tgmd "github.com/eekstunt/telegramify-markdown-go"
)
func main() {
msg := tgmd.Convert("**bold** and *italic*")
fmt.Println(msg.Text)
for _, e := range msg.Entities {
fmt.Printf("%s at %d len %d\n", e.Type, e.Offset, e.Length)
}
}
Output: bold and italic bold at 0 len 4 italic at 9 len 6
Example (CodeBlock) ¶
package main
import (
"fmt"
tgmd "github.com/eekstunt/telegramify-markdown-go"
)
func main() {
msg := tgmd.Convert("```go\nfmt.Println(\"hello\")\n```")
fmt.Println(msg.Text)
for _, e := range msg.Entities {
fmt.Printf("%s lang=%q at %d len %d\n", e.Type, e.Language, e.Offset, e.Length)
}
}
Output: fmt.Println("hello") pre lang="go" at 0 len 20
Example (Options) ¶
package main
import (
"fmt"
tgmd "github.com/eekstunt/telegramify-markdown-go"
)
func main() {
msg := tgmd.Convert("# Hello",
tgmd.WithHeadingSymbols([6]string{">>", "", "", "", "", ""}),
)
fmt.Println(msg.Text)
}
Output: >> Hello
func ConvertAndSplit ¶
ConvertAndSplit parses Markdown and splits the result into messages that each fit within the configured max message length (UTF-16 units).
Example ¶
package main
import (
"fmt"
tgmd "github.com/eekstunt/telegramify-markdown-go"
)
func main() {
msgs := tgmd.ConvertAndSplit("short message")
fmt.Println(len(msgs))
fmt.Println(msgs[0].Text)
}
Output: 1 short message
type Option ¶
type Option func(*config)
Option configures the converter.
func WithBulletMarker ¶
WithBulletMarker sets the bullet character for unordered lists.
func WithHeadingSymbols ¶
WithHeadingSymbols sets the emoji prefix for each heading level (h1 through h6).
func WithMaxMessageLen ¶
WithMaxMessageLen sets the maximum message length in UTF-16 code units for splitting.
func WithTaskMarkers ¶
WithTaskMarkers sets the unicode markers for checked/unchecked task list items.