fix(telegram): render tilde and longer code fences as code

The Markdown-to-HTML converter only protected ``` blocks, and closed a
block at the first ``` it met. A ~~~ block was formatted as prose ("#
setup" turned bold, __init__ into <b>init</b>), and a ```` block showing a
``` example rendered as an empty <pre> followed by leaked, re-formatted
text.

Protect fenced blocks line by line first: open on ``` or ~~~, close on a
run of the same character at least as long. One-line ```code``` and
unclosed fences keep the previous regex handling. The streaming preview
strips fences through the same helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HuiandClaude Opus 5.5 authored and Xubin Ren committed 2026-10-18 18:33:24 +08:00
1 parent df6158ec18
commit 7f64efbb95
2 files changed
+73 -4

No files matched your search

@@ -2965,6 +2965,34 @@ def test_markdown_to_html_headers_become_bold() -> None:
assert _markdown_to_telegram_html("### Deep") == "<b>Deep</b>"
def test_markdown_to_html_tilde_fence_is_code() -> None:
from nanobot.channels.telegram.runtime import _markdown_to_telegram_html
text = "Run:\n~~~python\n# setup\ndef __init__(self): pass\n~~~\nDone"
assert _markdown_to_telegram_html(text) == (
"Run:\n<pre><code># setup\ndef __init__(self): pass\n</code></pre>\nDone"
)
def test_markdown_to_html_longer_fence_keeps_nested_example() -> None:
from nanobot.channels.telegram.runtime import _markdown_to_telegram_html
text = "Example:\n````markdown\n```python\nx = a * b\n```\n**raw**\n````\nAfter **bold**"
assert _markdown_to_telegram_html(text) == (
"Example:\n<pre><code>```python\nx = a * b\n```\n**raw**\n</code></pre>\n"
"After <b>bold</b>"
)
def test_markdown_to_html_single_line_and_backtick_fences_unchanged() -> None:
from nanobot.channels.telegram.runtime import _markdown_to_telegram_html
assert _markdown_to_telegram_html("```py\n# c\nx = __y__\n```") == (
"<pre><code># c\nx = __y__\n</code></pre>"
)
assert _markdown_to_telegram_html("Use ```ls -la``` now") == "Use <pre><code>ls -la</code></pre> now"
def test_markdown_to_html_numbered_lists_preserved() -> None:
from nanobot.channels.telegram.runtime import _markdown_to_telegram_html
@@ -3067,6 +3095,12 @@ def test_strip_md_block_converts_bullets_and_numbers() -> None:
assert "2. item c" in result
def test_strip_md_block_strips_tilde_fences() -> None:
from nanobot.channels.telegram.runtime import _strip_md_block
assert _strip_md_block("~~~bash\nls\n~~~\nDone") == "ls\n\nDone"
def test_strip_md_block_strips_links() -> None:
from nanobot.channels.telegram.runtime import _strip_md_block