mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-25 00:51:20 +00:00
fix(telegram): escape backslashes and backticks inside code entities for MarkdownV2
- Escape \ → \\ inside inline code and fenced code blocks - Escape ` → \` inside fenced code block bodies (not delimiters) - Add regression tests for code entity backslash handling
This commit is contained in:
parent
1aa7027be1
commit
43b3a0ac66
2 changed files with 43 additions and 2 deletions
|
|
@ -787,14 +787,30 @@ class TelegramAdapter(BasePlatformAdapter):
|
|||
text = content
|
||||
|
||||
# 1) Protect fenced code blocks (``` ... ```)
|
||||
# Per MarkdownV2 spec, \ and ` inside pre/code must be escaped.
|
||||
def _protect_fenced(m):
|
||||
raw = m.group(0)
|
||||
# Split off opening ``` (with optional language) and closing ```
|
||||
open_end = raw.index('\n') + 1 if '\n' in raw[3:] else 3
|
||||
opening = raw[:open_end]
|
||||
body_and_close = raw[open_end:]
|
||||
body = body_and_close[:-3]
|
||||
body = body.replace('\\', '\\\\').replace('`', '\\`')
|
||||
return _ph(opening + body + '```')
|
||||
|
||||
text = re.sub(
|
||||
r'(```(?:[^\n]*\n)?[\s\S]*?```)',
|
||||
lambda m: _ph(m.group(0)),
|
||||
_protect_fenced,
|
||||
text,
|
||||
)
|
||||
|
||||
# 2) Protect inline code (`...`)
|
||||
text = re.sub(r'(`[^`]+`)', lambda m: _ph(m.group(0)), text)
|
||||
# Escape \ inside inline code per MarkdownV2 spec.
|
||||
text = re.sub(
|
||||
r'(`[^`]+`)',
|
||||
lambda m: _ph(m.group(0).replace('\\', '\\\\')),
|
||||
text,
|
||||
)
|
||||
|
||||
# 3) Convert markdown links – escape the display text; inside the URL
|
||||
# only ')' and '\' need escaping per the MarkdownV2 spec.
|
||||
|
|
|
|||
|
|
@ -146,6 +146,31 @@ class TestFormatMessageCodeBlocks:
|
|||
# "text" between blocks should be present
|
||||
assert "text" in result
|
||||
|
||||
def test_inline_code_backslashes_escaped(self, adapter):
|
||||
r"""Backslashes in inline code must be escaped for MarkdownV2."""
|
||||
text = r"Check `C:\ProgramData\VMware\` path"
|
||||
result = adapter.format_message(text)
|
||||
assert r"`C:\\ProgramData\\VMware\\`" in result
|
||||
|
||||
def test_fenced_code_block_backslashes_escaped(self, adapter):
|
||||
r"""Backslashes in fenced code blocks must be escaped for MarkdownV2."""
|
||||
text = "```\npath = r'C:\\Users\\test'\n```"
|
||||
result = adapter.format_message(text)
|
||||
assert r"C:\\Users\\test" in result
|
||||
|
||||
def test_fenced_code_block_backticks_escaped(self, adapter):
|
||||
r"""Backticks inside fenced code blocks must be escaped for MarkdownV2."""
|
||||
text = "```\necho `hostname`\n```"
|
||||
result = adapter.format_message(text)
|
||||
assert r"echo \`hostname\`" in result
|
||||
|
||||
def test_inline_code_no_double_escape(self, adapter):
|
||||
r"""Already-escaped backslashes should not be quadruple-escaped."""
|
||||
text = r"Use `\\server\share`"
|
||||
result = adapter.format_message(text)
|
||||
# \\ in input → \\\\ in output (each \ escaped once)
|
||||
assert r"`\\\\server\\share`" in result
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# format_message - bold and italic
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue