Documentation
¶
Overview ¶
Package telegramify converts Markdown into Telegram text, entities and rich messages.
Code generated by scripts/generate_latex.py; DO NOT EDIT.
Index ¶
- Constants
- func ConvertWithSegments(markdown string, options *ConvertOptions) (string, []MessageEntity, []Segment)
- func CountText(text string) int
- func DownloadImage(ctx context.Context, url string, client *http.Client) ([]byte, error)
- func EntitiesToMarkdownV2(text string, entities []MessageEntity) string
- func ExtractValidFilename(line string) string
- func GeneratePako(diagram string, config *RenderConfig) string
- func GetExt(language string) string
- func GetFilename(code, language string) string
- func IsImage(data []byte) bool
- func LatexToUnicode(input string) string
- func Markdownify(markdown string, options *ConvertOptions) string
- func MermaidInkURL(diagram string, config *RenderConfig) string
- func MermaidLiveURL(diagram string, config *RenderConfig) string
- func RenderMermaid(ctx context.Context, diagram string, client *http.Client, config *RenderConfig) ([]byte, error)
- func SplitMarkdownV2(text string, entities []MessageEntity, limit int) ([]string, error)
- func Standardize(markdown string, options *ConvertOptions) string
- func UTF16Len(text string) int
- type Content
- type ContentTrace
- type ContentType
- type ConvertOptions
- type File
- type InputRichMessage
- type MermaidConfig
- type MermaidRenderer
- type MessageEntity
- type Photo
- type RenderConfig
- type RichMessage
- type RichOptions
- type RichSplitOptions
- type Segment
- type Symbols
- type TelegramifyOptions
- type Text
- type TextChunk
Examples ¶
Constants ¶
const RichBlockLimit = 500
const RichByteLimit = 32768
Variables ¶
This section is empty.
Functions ¶
func ConvertWithSegments ¶
func ConvertWithSegments(markdown string, options *ConvertOptions) (string, []MessageEntity, []Segment)
func DownloadImage ¶
func EntitiesToMarkdownV2 ¶
func EntitiesToMarkdownV2(text string, entities []MessageEntity) string
EntitiesToMarkdownV2 serializes text and entities for parse_mode="MarkdownV2". Entity boundaries inside a UTF-16 surrogate pair or outside text are ignored.
func ExtractValidFilename ¶
func GeneratePako ¶
func GeneratePako(diagram string, config *RenderConfig) string
GeneratePako encodes diagram source and theme for mermaid.live and mermaid.ink.
func GetFilename ¶
func LatexToUnicode ¶
LatexToUnicode converts mathematical LaTeX into readable Unicode text. Unknown commands are preserved. It does not invoke TeX or external programs.
func Markdownify ¶
func Markdownify(markdown string, options *ConvertOptions) string
func MermaidInkURL ¶
func MermaidInkURL(diagram string, config *RenderConfig) string
func MermaidLiveURL ¶
func MermaidLiveURL(diagram string, config *RenderConfig) string
func RenderMermaid ¶
func RenderMermaid(ctx context.Context, diagram string, client *http.Client, config *RenderConfig) ([]byte, error)
RenderMermaid downloads and validates an image from mermaid.ink. A nil client uses a ten-second timeout. Cancellation always propagates through the request.
func SplitMarkdownV2 ¶
func SplitMarkdownV2(text string, entities []MessageEntity, limit int) ([]string, error)
SplitMarkdownV2 includes escapes and formatting markers in its length budget.
func Standardize ¶
func Standardize(markdown string, options *ConvertOptions) string
Types ¶
type Content ¶
type Content interface{ Type() ContentType }
Content is one sendable item from Telegramify or TelegramifyRich. Use a type switch to select sendMessage, sendDocument, sendPhoto or sendRichMessage.
func Telegramify ¶
func Telegramify(ctx context.Context, markdown string, options *TelegramifyOptions) ([]Content, error)
Telegramify converts, extracts code and Mermaid blocks, and splits text. Default Mermaid rendering sends diagram source to mermaid.ink. Set DisableMermaid to keep diagrams inline, or provide a local RenderMermaid.
Example ¶
package main
import (
"context"
"fmt"
telegramify "github.com/discapes/telegramify-md-go"
)
func main() {
options := telegramify.DefaultTelegramifyOptions()
options.DisableMermaid = true
parts, err := telegramify.Telegramify(context.Background(), "Intro\n\n```go\nfmt.Println(1)\n```", &options)
if err != nil {
panic(err)
}
for _, part := range parts {
fmt.Println(part.Type())
}
}
Output: text file
type ContentTrace ¶
type ContentType ¶
type ContentType string
const ( ContentText ContentType = "text" ContentFile ContentType = "file" ContentPhoto ContentType = "photo" ContentRich ContentType = "rich" )
type ConvertOptions ¶
type ConvertOptions struct {
Config *RenderConfig
DisableLatexEscape bool
}
ConvertOptions uses defaults when nil. DisableLatexEscape affects only \(...\) and \[...\] preprocessing; dollar-delimited math is always recognized.
type File ¶
type File struct {
FileName string `json:"file_name"`
FileData []byte `json:"file_data"`
ContentTrace ContentTrace `json:"content_trace"`
CaptionText string `json:"caption_text,omitempty"`
CaptionEntities []MessageEntity `json:"caption_entities,omitempty"`
}
func (File) Type ¶
func (File) Type() ContentType
type InputRichMessage ¶
type InputRichMessage struct {
HTML *string `json:"html,omitempty"`
Markdown *string `json:"markdown,omitempty"`
IsRTL *bool `json:"is_rtl,omitempty"`
SkipEntityDetection *bool `json:"skip_entity_detection,omitempty"`
}
InputRichMessage holds exactly one body, including when that body is empty.
func Richify ¶
func Richify(markdown string, options *RichOptions) (InputRichMessage, error)
func SplitRich ¶
func SplitRich(message InputRichMessage, options *RichSplitOptions) ([]InputRichMessage, error)
SplitRich splits at top-level blocks, preserving inline formatting and list numbering. Atomic blocks that cannot be split are emitted alone with a warning, matching Python behavior; callers can inspect their byte length before sending.
func (InputRichMessage) MarshalJSON ¶
func (r InputRichMessage) MarshalJSON() ([]byte, error)
func (InputRichMessage) Validate ¶
func (r InputRichMessage) Validate() error
type MermaidConfig ¶
type MermaidRenderer ¶
type MessageEntity ¶
type MessageEntity struct {
Type string `json:"type"`
Offset int `json:"offset"`
Length int `json:"length"`
URL string `json:"url,omitempty"`
Language string `json:"language,omitempty"`
CustomEmojiID string `json:"custom_emoji_id,omitempty"`
User map[string]any `json:"user,omitempty"`
UnixTime *int64 `json:"unix_time,omitempty"`
DateTimeFormat *string `json:"date_time_format,omitempty"`
}
MessageEntity uses UTF-16 code units for Offset and Length, as required by Telegram.
func Convert ¶
func Convert(markdown string, options *ConvertOptions) (string, []MessageEntity)
Example ¶
package main
import (
"encoding/json"
"fmt"
telegramify "github.com/discapes/telegramify-md-go"
)
func main() {
text, entities := telegramify.Convert("Hello **world** 😀", nil)
data, _ := json.Marshal(entities)
fmt.Println(text)
fmt.Println(string(data))
}
Output: Hello world 😀 [{"type":"bold","offset":6,"length":5}]
type Photo ¶
type Photo struct {
FileName string `json:"file_name"`
FileData []byte `json:"file_data"`
ContentTrace ContentTrace `json:"content_trace"`
CaptionText string `json:"caption_text,omitempty"`
CaptionEntities []MessageEntity `json:"caption_entities,omitempty"`
}
func (Photo) Type ¶
func (Photo) Type() ContentType
type RenderConfig ¶
type RenderConfig struct {
Symbols Symbols
Mermaid MermaidConfig
CiteExpandable bool
}
RenderConfig is a value: copying it produces an independent configuration.
func DefaultConfig ¶
func DefaultConfig() RenderConfig
type RichMessage ¶
type RichMessage struct {
RichMessage InputRichMessage `json:"rich_message"`
ContentTrace ContentTrace `json:"content_trace"`
}
func TelegramifyRich ¶
func TelegramifyRich(markdown string, options *RichOptions) ([]RichMessage, error)
func (RichMessage) Type ¶
func (RichMessage) Type() ContentType
type RichOptions ¶
type RichSplitOptions ¶
type RichSplitOptions struct{ ByteLimit, BlockLimit int }
type Segment ¶
type Segment struct {
Kind string `json:"kind"`
TextStart int `json:"text_start"`
TextEnd int `json:"text_end"`
UTF16Start int `json:"utf16_start"`
UTF16End int `json:"utf16_end"`
Language string `json:"language"`
RawCode string `json:"raw_code"`
}
Segment records an extractable code block. TextStart/TextEnd are UTF-8 byte offsets; UTF16Start/UTF16End are Telegram entity offsets.
type Symbols ¶
type Symbols struct {
Headings [6]string
Image, Link, TaskCompleted, TaskUncompleted, HorizontalRule string
UnorderedListItem, OrderedListSuffix string
}
Symbols controls the plain-text decorations added during entity conversion.
type TelegramifyOptions ¶
type TelegramifyOptions struct {
ConvertOptions
MaxMessageLength int
DisableMermaid bool
// MinFileLines defaults to 1 with nil options; 0 keeps code blocks inline.
MinFileLines int
// RenderMermaid overrides the HTTP renderer, for local rendering or tests.
RenderMermaid MermaidRenderer
}
func DefaultTelegramifyOptions ¶
func DefaultTelegramifyOptions() TelegramifyOptions
type Text ¶
type Text struct {
Text string `json:"text"`
Entities []MessageEntity `json:"entities"`
ContentTrace ContentTrace `json:"content_trace"`
}
func (Text) Type ¶
func (Text) Type() ContentType
type TextChunk ¶
type TextChunk struct {
Text string `json:"text"`
Entities []MessageEntity `json:"entities"`
}
func SplitEntities ¶
func SplitEntities(text string, entities []MessageEntity, limit int) ([]TextChunk, error)
SplitEntities prefers newline boundaries and clips entities across chunks. A limit too small for a single rune returns an error instead of invalid UTF-8.