Expand description
Telegram HTML and Markdown entity parsers for ferogram.
This crate is part of ferogram, an async Rust MTProto client built by Ankit Chaubey.
- Channel: t.me/Ferogram
- Chat: t.me/FerogramChat
Converts Telegram-flavoured Markdown and HTML into the (plain_text, entities) pair that the Telegram API expects, and generates those formats
back from entities. Also provides rich-text parsers that produce
Vec<PageBlock> for Telegraph-style structured content.
Most users reach this through the ferogram crate’s InputMessage
builder (.markdown() / .html()). Use ferogram-parsers directly only
when you need the parse/generate functions without the full client.
§What’s in here
parse_markdown/generate_markdown: Parse and generate Telegram MarkdownV2. Supports bold, italic, underline, strikethrough, spoiler, inline code, code blocks with language, blockquotes (regular and expandable), text URLs, user mentions, and custom emoji. All offsets are in UTF-16 code units as required by the Telegram API.parse_markdown_v1(deprecated): Legacy MarkdownV1 retained for backward compatibility. Prefer V2 for new code.parse_html/generate_html: Parse and generate Telegram Bot API HTML. Recognises<b>,<i>,<u>,<s>,<code>,<pre>,<a>,<blockquote>,<tg-spoiler>,<tg-emoji>,<tg-time>, and their aliases. Thehtml5everfeature switches to a spec-compliant parser for malformed input.parse_rich_markdown/parse_rich_inline_md: Parse extended Markdown intoVec<PageBlock>/RichTextfor Telegraph articles. Supports headings (H1–H6), paragraphs, ordered and unordered lists, task lists, tables, blockquotes, pull quotes, dividers, fenced code blocks, and math blocks.parse_rich_html/parse_rich_html_inline: Same as the Markdown rich parsers but driven by HTML input (<h1>–<h6>,<ul>,<ol>,<table>,<details>,<tg-map>,<tg-math-block>, etc.).
§Example: Markdown round-trip
use ferogram_parsers::{parse_markdown, generate_markdown};
let (text, entities) = parse_markdown("Hello **world**!");
assert_eq!(text, "Hello world!");
let back = generate_markdown(&text, &entities);
assert_eq!(back, "Hello *world*\\!");§Example: HTML to entities
use ferogram_parsers::parse_html;
let (text, entities) = parse_html("<b>bold</b> and <i>italic</i>");
assert_eq!(text, "bold and italic");
assert_eq!(entities.len(), 2);§Features
| Feature | Default | Description |
|---|---|---|
html | yes | Built-in hand-rolled HTML parser |
html5ever | no | Replaces the built-in parser with html5ever for spec-compliant handling of malformed HTML |
Functions§
- generate_
html - Generate Telegram-compatible HTML from plain text + entities.
- generate_
markdown - Generate Telegram markdown from plain text + entities.
Calls
generate_markdown_v2. - generate_
markdown_ v2 - Generate MarkdownV2 from plain text + entities.
- parse_
html - Parse a Telegram-compatible HTML string into
(plain_text, entities). - parse_
markdown - Parse Telegram markdown into
(plain_text, entities). - parse_
markdown_ v1 Deprecated - Parse Telegram-flavoured MarkdownV1 (legacy) into
(plain_text, entities). - parse_
markdown_ v2 - Parse MarkdownV2 (current Telegram format) into
(plain_text, entities). - parse_
rich_ html - parse_
rich_ html_ inline - Parse an HTML inline string into a
RichTexttree. Handles all inline tags: b, strong, i, em, u, ins, s, del, strike, code, mark, tg-spoiler, sub, sup, a, tg-emoji, tg-time, tg-math, tg-reference. - parse_
rich_ inline_ md - Parse a Rich Markdown inline string (may contain nested
**,*,_,__,~~,||,`,==text==,[label](url),,,$…$,<u>,<sub>,<sup>,<tg-spoiler>,<ins>,<mark>) into aRichTexttree. - parse_
rich_ markdown