Documentation
¶
Overview ¶
Package markdown contains Markdown (CommonMark) styling options.
It parses standard Markdown and converts it into Telegram message entities.
Index ¶
- Variables
- func Bytes(resolver func(id int64) (tg.InputUserClass, error), b []byte) styling.StyledTextOption
- func Format(resolver func(id int64) (tg.InputUserClass, error), format string, ...) styling.StyledTextOption
- func Markdown(r io.Reader, b *entity.Builder, opts Options) error
- func NewSpoilerParser() parser.InlineParser
- func Reader(resolver func(id int64) (tg.InputUserClass, error), r io.Reader) styling.StyledTextOption
- func String(resolver func(id int64) (tg.InputUserClass, error), s string) styling.StyledTextOption
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var KindSpoiler = gast.NewNodeKind("Spoiler")
KindSpoiler is a NodeKind of a Spoiler node.
Spoiler is a Telegram MarkdownV2 construct (||text||) that has no CommonMark equivalent; it maps onto the message spoiler entity.
Functions ¶
func Bytes ¶
func Bytes(resolver func(id int64) (tg.InputUserClass, error), b []byte) styling.StyledTextOption
Bytes reads Markdown from given byte slice and returns styling option to build styled text block.
func Format ¶
func Format(resolver func(id int64) (tg.InputUserClass, error), format string, args ...interface{}) styling.StyledTextOption
Format formats string using fmt, parses Markdown from formatted string and returns styling option to build styled text block.
func Markdown ¶
Markdown parses given input from reader as Markdown and adds parsed entities to the given builder.
Parsing is backed by goldmark and follows CommonMark (plus GFM strikethrough and the Telegram ||spoiler|| extension). The constructs that map onto Telegram message entities are:
*text*, _text_ italic **text**, __text__ bold ~~text~~ strikethrough ||text|| spoiler `text` inline code ```lang pre-formatted code block [text](url) inline URL (tg://user?id=N becomes a mention)  custom emoji > text block quotation
Constructs without an entity equivalent (headings, lists, images, etc.) are rendered as plain text. The parser is lenient: unmatched markup is emitted as plain text instead of failing.
Parameter UserResolver of opts is used to resolve user by ID during formatting. May be nil. If UserResolver is nil, formatter will create tg.InputUser using only ID. Notice that it's okay for bots, but not for users.
func NewSpoilerParser ¶ added in v0.156.1
func NewSpoilerParser() parser.InlineParser
NewSpoilerParser returns a new InlineParser that parses ||spoiler|| expressions.
func Reader ¶
func Reader(resolver func(id int64) (tg.InputUserClass, error), r io.Reader) styling.StyledTextOption
Reader reads Markdown from given reader and returns styling option to build styled text block.
func String ¶
func String(resolver func(id int64) (tg.InputUserClass, error), s string) styling.StyledTextOption
String reads Markdown from given string and returns styling option to build styled text block.
Example ¶
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/markdown"
"github.com/gotd/td/tg"
)
func sendMarkdown(ctx context.Context) error {
client, err := telegram.ClientFromEnvironment(telegram.Options{})
if err != nil {
return err
}
// This example creates a styled message from Markdown
// and sends it to your Saved Messages folder.
return client.Run(ctx, func(ctx context.Context) error {
_, err := message.NewSender(tg.NewClient(client)).
Self().StyledText(ctx, markdown.String(nil, `**bold text**
_italic text_
~~strikethrough~~
**bold _italic bold_ bold**
[inline URL](http://www.example.com/)
[inline mention of a user](tg://user?id=123456789)

`+"`inline fixed-width code`"+`
`+"```python"+`
pre-formatted fixed-width code block written in the Python programming language
`+"```"+`
> Block quotation started
> The last line of the block quotation`))
return err
})
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := sendMarkdown(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(2)
}
}
Output:
Types ¶
type Options ¶
type Options struct {
// UserResolver is used to resolve user by ID during formatting. May be nil.
//
// If userResolver is nil, formatter will create tg.InputUser using only ID.
// Notice that it's okay for bots, but not for users.
UserResolver entity.UserResolver
}
Options is options of Markdown parser.