markdown

package
v0.157.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package markdown contains Markdown (CommonMark) styling options.

It parses standard Markdown and converts it into Telegram message entities.

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func Markdown(r io.Reader, b *entity.Builder, opts Options) error

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)
![e](tg://emoji?id=N) 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)
![👍](tg://emoji?id=5368324170671202286)
`+"`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)
	}
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL