fix(telegram): add MarkdownV2 support for strikethrough, spoiler, and blockquotes

- Convert ~~text~~ to ~text~ (MarkdownV2 strikethrough)
- Protect ||text|| from pipe escaping (MarkdownV2 spoiler)
- Preserve > at line start as blockquote instead of escaping it
- Update _strip_mdv2() to strip ~strikethrough~ and ||spoiler|| markers
- Add tests covering new formatting paths and edge cases
This commit is contained in:
llbn 2026-03-20 18:21:24 +01:00
parent 1aa7027be1
commit 02f639e561
No known key found for this signature in database
2 changed files with 125 additions and 4 deletions

View file

@ -79,8 +79,8 @@ def _escape_mdv2(text: str) -> str:
def _strip_mdv2(text: str) -> str:
"""Strip MarkdownV2 escape backslashes to produce clean plain text.
Also removes MarkdownV2 bold markers (*text* -> text) so the fallback
doesn't show stray asterisks from header/bold conversion.
Also removes MarkdownV2 formatting markers so the fallback
doesn't show stray syntax characters from format_message conversion.
"""
# Remove escape backslashes before special characters
cleaned = re.sub(r'\\([_*\[\]()~`>#\+\-=|{}.!\\])', r'\1', text)
@ -89,6 +89,10 @@ def _strip_mdv2(text: str) -> str:
# Remove MarkdownV2 italic markers that format_message converted from *italic*
# Use word boundary (\b) to avoid breaking snake_case like my_variable_name
cleaned = re.sub(r'(?<!\w)_([^_]+)_(?!\w)', r'\1', cleaned)
# Remove MarkdownV2 strikethrough markers (~text~ → text)
cleaned = re.sub(r'~([^~]+)~', r'\1', cleaned)
# Remove MarkdownV2 spoiler markers (||text|| → text)
cleaned = re.sub(r'\|\|([^|]+)\|\|', r'\1', cleaned)
return cleaned
@ -832,10 +836,32 @@ class TelegramAdapter(BasePlatformAdapter):
text,
)
# 7) Escape remaining special characters in plain text
# 7) Convert strikethrough: ~~text~~ → ~text~ (MarkdownV2)
text = re.sub(
r'~~(.+?)~~',
lambda m: _ph(f'~{_escape_mdv2(m.group(1))}~'),
text,
)
# 8) Convert spoiler: ||text|| → ||text|| (protect from | escaping)
text = re.sub(
r'\|\|(.+?)\|\|',
lambda m: _ph(f'||{_escape_mdv2(m.group(1))}||'),
text,
)
# 9) Convert blockquotes: > at line start → protect > from escaping
text = re.sub(
r'^(>{1,3}) (.+)$',
lambda m: _ph(m.group(1) + ' ' + _escape_mdv2(m.group(2))),
text,
flags=re.MULTILINE,
)
# 10) Escape remaining special characters in plain text
text = _escape_mdv2(text)
# 8) Restore placeholders in reverse insertion order so that
# 11) Restore placeholders in reverse insertion order so that
# nested references (a placeholder inside another) resolve correctly.
for key in reversed(list(placeholders.keys())):
text = text.replace(key, placeholders[key])