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

This commit is contained in:
santhreal 2026-08-02 23:51:50 -07:00 committed by Xubin Ren
parent a13e29bf07
commit 170c7083ed
2 changed files with 15 additions and 2 deletions

View File

@ -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')

View File

@ -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"