NimarkoGram Docs
RU
Docs/Text formatting

Text formatting

Produce plain text and formatting ranges, including text containing emoji.

On this pageParse before displaying textWhy the offset is threeChoose a marker

Parse before displaying text

markdown_utils.parse_markdown accepts a string and returns a ParsedMessage. Its text contains the text with the markers removed. Its entities hold the formatting ranges. Test the result locally inside the callback before passing it on.

Python
from markdown_utils import parse_markdown

parsed = parse_markdown("🍎 *tea*")
assert parsed.text == "🍎 tea"
entity, = parsed.entities
assert entity.type.value == "bold"
assert (entity.offset, entity.length) == (3, 3)

Why the offset is three

Telegram counts UTF-16 code units. The apple uses two units and the space one, so “tea” starts at offset 3. Its Python string index is 2. Keep the ranges produced by the parser when using the parsed text. If you edit the beginning of the string, parse the final version again.

Choose a marker

Input Result
*tea* Bold.
_tea_ Italic.
__tea__ Underline.
~tea~ Strikethrough.
||tea|| Spoiler.

The SDK parser has its own marker syntax. An unclosed *tea raises SyntaxError. For user input, catch the exception and let the user correct the marker or display the original text without formatting. If an API expects TLRPC objects, call entity.to_tlrpc_object() on each range and pass the result alongside parsed.text.