Skip to main content

Crate telegram_markdown_v2

Crate telegram_markdown_v2 

Source
Expand description

Convert Markdown into Telegram MarkdownV2.

Telegram’s MarkdownV2 is a restricted dialect with its own escaping rules. This crate takes a Markdown document and renders it into a string suitable to be sent with Telegram’s parse_mode = MarkdownV2.

The main entry points are convert (default behavior) and convert_with_strategy (custom handling for unsupported constructs).

§Basic usage

use telegram_markdown_v2::convert;

let out = convert("Hello world!")?;
assert_eq!(out, "Hello world\\!\n");

§Unsupported constructs

Telegram MarkdownV2 does not support some Markdown/HTML constructs (e.g. blockquotes, tables, and raw HTML blocks). Use UnsupportedTagsStrategy to decide what to do with such input:

use telegram_markdown_v2::{convert_with_strategy, UnsupportedTagsStrategy};

let out = convert_with_strategy("> test", UnsupportedTagsStrategy::Escape)?;
assert_eq!(out, "\\> test\n");

§Telegram-specific extensions

Outside inline/fenced code, the converter recognizes:

  • <u>…</u> as underline (__…__)
  • <span class="tg-spoiler">…</span> as spoiler (||…||)

Structs§

Definition
Reference-style link definition extracted from the Markdown AST.

Enums§

Error
Errors returned while converting Markdown into Telegram MarkdownV2.
TextType
Escaping context used by escape_symbols.
UnsupportedTagsStrategy
Strategy used to handle Markdown nodes that cannot be represented in Telegram MarkdownV2.

Functions§

convert
Converts a Markdown document into Telegram MarkdownV2.
convert_with_strategy
Converts a Markdown document into Telegram MarkdownV2, controlling how unsupported constructs are handled.

Type Aliases§

Result
Convenience result type used across this crate.