From 8e31ef1e399cfda1ad902c82b3bfbb3de7608c19 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Tue, 10 Nov 2026 05:16:02 -0800 Subject: [PATCH] fix(telegram): a prose $$ never pairs across a code span with a later formula Round-4 review: since the $ escape runs per prose stretch between code regions (78a87c7c), a prose "Use $$ to get the shell PID." paired with a later display formula's $$ across a code span, so the amounts in between ("Cost is $5 vs $10") were left bare and rendered as garbled math, and the real formula's $$ was left unpaired (6 of 20k corpus replies). A $$ pair now stays in one prose stretch: its body holds no backtick, blank line, or bare $ before a digit (an amount). --- plugins/platforms/telegram/telegram_markdown.py | 5 ++++- .../telegram/test_telegram_markdown_150997_regressions.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/platforms/telegram/telegram_markdown.py b/plugins/platforms/telegram/telegram_markdown.py index a6cf2e30ee..68ec0aec78 100644 --- a/plugins/platforms/telegram/telegram_markdown.py +++ b/plugins/platforms/telegram/telegram_markdown.py @@ -454,8 +454,11 @@ def _escape_bare_bracket(m, seg: str) -> str: # and the currency $ it escapes: a lone $ before an # amount, or after a currency prefix (R$ 1.000, US$ 1,200). Telegram's rich Markdown pairs any two # bare $ as inline LaTeX, so "$395k vs $483k" otherwise renders as one garbled math span. +# A $$ pair stays inside one stretch of prose: its body never holds a backtick (a code span +# split the prose there, so a prose "Use $$ for the PID" would pair across it with the next +# formula), a blank line, or a bare $ before a digit (an amount, never display math). _RICH_DOLLAR_RE = re.compile( - r'(?P\\\$|\$\$[\s\S]*?\$\$' + r'(?P\\\$|\$\$(?:[^$\n`]|(?<=\\)\$|(?]*(?:\n|$)))*?\$\$' r'|(?]|\\[a-zA-Z]++)(?:[\w.,(){}^_+*/=<>-]|\\[a-zA-Z]+)*+(?])\$(?![\w$])' r'|(?