Skip to content

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

Merged
Re-bin merged 1 commit into
HKUDS:mainfrom
Bdysj:fix/telegram-link-url-formatting
Oct 3, 2026
Merged

Re-bin merged 1 commit into
HKUDS:mainfrom
Bdysj:fix/telegram-link-url-formatting

Conversation

@Bdysj

@Bdysj Bdysj commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix Telegram Markdown→HTML rendering corrupting link URLs. Links whose URL contains __, _word_, ** or ~~ were rewritten by the inline-formatting passes, and a " in the URL broke out of the href attribute.

Root Cause

_markdown_to_telegram_html converts [text](url) to <a href="url">text</a> in step 6, then runs the bold / italic / strikethrough regexes over the whole string in steps 7–9 — including the freshly generated href values. _escape_telegram_html also does not escape ", which is needed inside a double-quoted attribute.

Markdown Before After
[init](https://github.com/o/r/blob/main/pkg/__init__.py) href=".../pkg/<b>init</b>.py" href=".../pkg/__init__.py"
[css](https://example.com/_static_/a.css) href="https://example.com/<i>static</i>/a.css" URL unchanged
[q](https://example.com/?q="x") href="https://example.com/?q="x"" href="https://example.com/?q=&quot;x&quot;"

Links to __init__.py, __main__.py, dunder paths and _static_ asset folders are common in coding-agent replies, so these messages end up with broken links or a Telegram can't parse entities rejection.

Changes

  • Park each link URL in a \x00LK{i}\x00 placeholder (same technique already used for code blocks and inline code) and restore it after the inline-formatting passes.
  • Escape " as &quot; inside the stored URL.
  • Link text is still formatted, so [**bold** _it_](url) keeps rendering as <a href="url"><b>bold</b> <i>it</i></a>.

Result

Link URLs reach Telegram byte-for-byte (HTML-escaped). Rendering of non-link text is unchanged.

Test

  • Red/green: 4 of the 5 new assertions (dunder URL, _static_ URL, **/~~ URL, quoted URL) failed before the fix and pass afterward; the link-text formatting case passes both before and after, guarding against regressions.
  • pytest nanobot/channels/telegram -q: 145 passed.
  • ruff check nanobot/channels/telegram: passed.
  • basedpyright nanobot/channels/telegram/runtime.py: 0 errors, 0 warnings.
  • git diff --check: passed.

@chengyongru chengyongru added bug Something isn't working channel fix priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. test labels Sep 29, 2026
@Re-bin
Re-bin force-pushed the fix/telegram-link-url-formatting branch from 36757b2 to b41481f Compare October 2, 2026 15:07
@Re-bin
Re-bin force-pushed the fix/telegram-link-url-formatting branch from b41481f to c877f26 Compare October 3, 2026 07:39
Bold, italic and strikethrough passes ran over generated href attributes, so URLs such as .../__init__.py became .../<b>init</b>.py and Telegram rejected or mis-linked the message. Protect URLs with placeholders until inline formatting is done and escape double quotes inside href.
@Re-bin
Re-bin force-pushed the fix/telegram-link-url-formatting branch from c877f26 to d4a3a24 Compare October 3, 2026 09:52

@Re-bin Re-bin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeps Telegram link destinations intact while preserving label formatting and escaping attribute quotes. Channel regression coverage and current-head CI pass.

@Re-bin
Re-bin merged commit 2d52db2 into HKUDS:main Oct 3, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working channel fix priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants