Skip to content

Your lightweight open-source personal agent.

fix(telegram): keep link URLs intact when rendering Markdown to HTML

bob2d52db24788a
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'<a href="\2">\1</a>', 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 ``<b>init</b>.py``).
+ link_urls: list[str] = []
+ def save_link(m: re.Match[str]) -> str:
+ link_urls.append(m.group(2).replace('"', "&quot;"))
+ return f'<a href="\x00LK{len(link_urls) - 1}\x00">{m.group(1)}</a>'
+
+ text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', save_link, text)
# 7. Bold **text** or __text__
text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', 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 "<b>bold text</b>" in result
+ ("markdown", "expected"),
+ [
+ (
+ "[init](https://github.com/o/r/blob/main/pkg/__init__.py)",
+ '<a href="https://github.com/o/r/blob/main/pkg/__init__.py">init</a>',
+ ),
+ (
+ "see [css](https://example.com/_static_/a.css) ok",
+ 'see <a href="https://example.com/_static_/a.css">css</a> ok',
+ ),
+ (
+ "[x](https://example.com/a**b**c~~d~~)",
+ '<a href="https://example.com/a**b**c~~d~~">x</a>',
+ ),
+ ],
+)
+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 == '<a href="https://example.com/?q=&quot;x&quot;&amp;y=1">q</a>'
+
+
+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 == '<a href="https://example.com/a_b_c"><b>bold</b> <i>it</i></a>'
+
+
# ---------------------------------------------------------------------------
# _strip_md_block tests
# ---------------------------------------------------------------------------