From 2d52db24788ab78956b0e7448b32629489b74104 Mon Sep 17 00:00:00 2001 From: bob <24078726@siswa.um.edu.my> Date: Tue, 29 Sep 2026 12:23:20 +0800 Subject: [PATCH] fix(telegram): keep link URLs intact when rendering Markdown to HTML Bold, italic and strikethrough passes ran over generated href attributes, so URLs such as .../__init__.py became .../init.py and Telegram rejected or mis-linked the message. Protect URLs with placeholders until inline formatting is done and escape double quotes inside href. --- nanobot/channels/telegram/runtime.py | 15 ++++++-- .../telegram/tests/test_telegram_channel.py | 34 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/nanobot/channels/telegram/runtime.py b/nanobot/channels/telegram/runtime.py index 76103c942..152dd5e9e 100644 --- a/nanobot/channels/telegram/runtime.py +++ b/nanobot/channels/telegram/runtime.py @@ -317,8 +317,15 @@ def _markdown_to_telegram_html(text: str) -> str: # 5. Escape HTML special characters text = _escape_telegram_html(text) - # 6. Links [text](url) - must be before bold/italic to handle nested cases - text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'\1', text) + # 6. Links [text](url) - must be before bold/italic to handle nested cases. + # Park the URL in a placeholder so later inline-formatting passes cannot + # rewrite it (e.g. ``__init__.py`` must not become ``init.py``). + link_urls: list[str] = [] + def save_link(m: re.Match[str]) -> str: + link_urls.append(m.group(2).replace('"', """)) + return f'{m.group(1)}' + + text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', save_link, text) # 7. Bold **text** or __text__ text = re.sub(r'\*\*(.+?)\*\*', r'\1', text) @@ -336,6 +343,10 @@ def _markdown_to_telegram_html(text: str) -> str: # 10.5. Numbered lists 1. item -> 1. item (keep number, normalize indent) text = re.sub(r'^(\d+)\.\s+', r'\1. ', text, flags=re.MULTILINE) + # 10.6. Restore link URLs protected in step 6 + for i, url in enumerate(link_urls): + text = text.replace(f"\x00LK{i}\x00", url) + # 11. Restore inline code with HTML tags for i, code in enumerate(inline_codes): # Escape HTML in code content diff --git a/nanobot/channels/telegram/tests/test_telegram_channel.py b/nanobot/channels/telegram/tests/test_telegram_channel.py index 1178f9276..d58216fb8 100644 --- a/nanobot/channels/telegram/tests/test_telegram_channel.py +++ b/nanobot/channels/telegram/tests/test_telegram_channel.py @@ -2960,6 +2960,40 @@ def test_markdown_to_html_mixed_formatting() -> None: assert "bold text" in result +@pytest.mark.parametrize( + ("markdown", "expected"), + [ + ( + "[init](https://github.com/o/r/blob/main/pkg/__init__.py)", + 'init', + ), + ( + "see [css](https://example.com/_static_/a.css) ok", + 'see css ok', + ), + ( + "[x](https://example.com/a**b**c~~d~~)", + 'x', + ), + ], +) +def test_markdown_to_html_link_urls_are_not_formatted(markdown: str, expected: str) -> None: + """Inline-formatting passes must not inject tags into href attributes.""" + assert _markdown_to_telegram_html(markdown) == expected + + +def test_markdown_to_html_link_url_quotes_are_escaped() -> None: + result = _markdown_to_telegram_html('[q](https://example.com/?q="x"&y=1)') + + assert result == 'q' + + +def test_markdown_to_html_link_text_keeps_formatting() -> None: + result = _markdown_to_telegram_html("[**bold** _it_](https://example.com/a_b_c)") + + assert result == 'bold it' + + # --------------------------------------------------------------------------- # _strip_md_block tests # ---------------------------------------------------------------------------