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 .../<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.
This commit is contained in:
1 parent
4904305450
commit
2d52db2478
2 files changed
+47
-2
No files matched your search
@@ -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('"', """))
|
||||
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
|
||||
|
||||
@@ -2960,6 +2960,40 @@ def test_markdown_to_html_mixed_formatting() -> None:
|
||||
assert "<b>bold text</b>" in result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("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="x"&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
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user