MarkdownV2Parser.parse ignores backslash escapes and deviates from the spec in several places
Problem
MarkdownV2Parser.parse (in gramjs/extensions/markdownv2.ts) departs from
the Telegram MarkdownV2 spec
in several ways:
-
Backslash escapes are not honored. Per the spec,
\Xfor any X in_*[]()~`>#+-=|{}.!becomes the literal X, and\\becomes\. Today these are passed through verbatim:- Input
1\.5→ output text1\.5(expected1.5). - Input
\*not bold\*→ output text<b>not bold</b>(expected literal text*not bold*with no entity, since the delimiters are escaped).
- Input
-
Italic uses
-instead of_. The current code matches-text-for italic. The spec uses_text_and reserves__text__for underline. -
No blockquote support. Per spec, lines beginning with
>form a blockquote, and a final line ending in||marks it as expandable (MessageEntityBlockquote.collapsed = true). Today these are emitted as literal>chars. -
Per-region escape rules are not applied. Inside
preandcodeonly\\and\`should unescape; inside the(URL)of a link or custom emoji only\\and\)should unescape. There's no pass to apply this selectively. -
HTML special characters in plain text confuse the downstream HTML parser. A user-typed
<is not escaped before being handed toHTMLParser.parse, so<b>not bold</b>typed into MarkdownV2 input is incorrectly interpreted as bold. -
HTMLParseris missing some Telegram HTML-spec tags thathtmlToMarkdownV2(and external callers) need to round-trip cleanly:<tg-spoiler>,<span class="tg-spoiler">,<ins>(underline alternative),<strike>(strikethrough alternative). AndHTMLParser.unparseemits the library-internal<spoiler>tag (not in the spec) and drops theexpandableattribute on collapsed blockquotes, so the flag doesn't survive round-trips.
Proposal
Rewrite the markdown→HTML transform inside the existing
markdown → HTML → HTMLParser pipeline as a staged process: extract
protected regions (pre/code/link/emoji) up front with their own escape
rules; mask remaining backslash-escapes; HTML-escape & and < in user
content; run span and blockquote markup; unmask; restore protected regions.
Switch italic to _ per spec. Expose markdownV2ToHtml and
htmlToMarkdownV2 as standalone functions so external callers can convert
between formats. Patch HTMLParser to recognize the missing tag forms and
to preserve the expandable attribute on round-trip.