Skip to content

Your lightweight open-source personal agent.

fix(telegram): require newline for language tag to preserve single-line fenced code

santhreal170c7083edb3
diff --git a/nanobot/channels/telegram/runtime.py b/nanobot/channels/telegram/runtime.py
index 461343d7d..4f5ae0ddd 100644
--- a/nanobot/channels/telegram/runtime.py
+++ b/nanobot/channels/telegram/runtime.py
@@ -166,7 +166,7 @@ def _strip_md_block(text: str) -> str:
markdown syntax while the response is still being generated.
"""
# Code blocks -> just the code
- text = re.sub(r'```[^\n]*\n?([\s\S]*?)```', r'\1', text)
+ text = re.sub(r'```(?:[^\n]*\n)?([\s\S]*?)```', r'\1', text)
# Headers -> plain text
text = re.sub(r'^#{1,6}\s+(.+)$', r'\1', text, flags=re.MULTILINE)
# Blockquotes
@@ -232,7 +232,7 @@ def _markdown_to_telegram_html(text: str) -> str:
code_blocks.append(m.group(1))
return f"\x00CB{len(code_blocks) - 1}\x00"
- text = re.sub(r'```[^\n]*\n?([\s\S]*?)```', save_code_block, text)
+ text = re.sub(r'```(?:[^\n]*\n)?([\s\S]*?)```', save_code_block, text)
# 1.5. Convert markdown tables to box-drawing (reuse code_block placeholders)
lines = text.split('\n')
diff --git a/nanobot/channels/telegram/tests/test_telegram_channel.py b/nanobot/channels/telegram/tests/test_telegram_channel.py
index 2e3da3842..160b3acd9 100644
--- a/nanobot/channels/telegram/tests/test_telegram_channel.py
+++ b/nanobot/channels/telegram/tests/test_telegram_channel.py
@@ -2405,3 +2405,16 @@ def test_markdown_to_html_code_block_special_chars_language() -> None:
stripped = _strip_md_block(text)
assert stripped == "int main() { return 0; }\n"
+def test_markdown_to_html_code_block_same_line_no_newline() -> None:
+ """
+ Locks out the regression where triple-backtick content without a newline
+ (e.g., Use ```<tag>``` here) was mistaken for a language info string and discarded.
+ """
+ from nanobot.channels.telegram.runtime import _markdown_to_telegram_html, _strip_md_block
+
+ text = "Use ```<tag>``` here"
+ html = _markdown_to_telegram_html(text)
+ assert html == "Use <pre><code>&lt;tag&gt;</code></pre> here"
+
+ stripped = _strip_md_block(text)
+ assert stripped == "Use <tag> here"