link-patterns extra replaces matches inside inline HTML tags and destroys email auto-links

Offen
#752 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 3 Tagen

@rahul05ranjan arbeitet bereits daran.

Seit 28.10.2026.

  • #753 von @rahul05ranjan — offen

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
30/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
backend

Rechercherichtung

Start by reading LinkPatterns.run and is_inside_link in lib/markdown2.py, then inspect the inline HTML tokenization and auto-link tests. Reproduce the three examples with the link-patterns extra and compare them with output without it. Done means matches in prose are linkified while tag attributes and email auto-link tokens remain intact; pull request #753 is already open.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Bug

Summary

With the link-patterns extra enabled, a pattern that matches inside an inline HTML tag (its attributes) or inside a <...> auto-link token gets replaced there, corrupting the HTML and destroying email auto-links. The same input renders correctly without the extra.

Repro

import re
import markdown2

lp = [(re.compile(r"\b(word)\b"), r"/w/\1")]
print(markdown2.markdown('See <span title="word">x</span>', extras=["link-patterns"], link_patterns=lp))
print(markdown2.markdown('<[email protected]>', extras=["link-patterns"], link_patterns=lp))
print(markdown2.markdown('<img src="/x.png" alt="word">', extras=["link-patterns"], link_patterns=lp))

Output with the extra:

<p>See <span title="<a href="/w/word">word</a>">x</span></p>
<p>&lt;<a href="/w/word">word</a>@example.com&gt;</p>
<p><img src="/x.png" alt="<a href="/w/word">word</a>"></p>

Expected (matches what plain markdown2 does for the markup, while prose matches are linkified):

<p>See <span title="word">x</span></p>
<p><a href="mailto:[email protected]">[email protected]</a></p>
<p><img src="/x.png" alt="word"></p>

Three distinct symptoms:

  1. <span title="word">x</span> — an anchor gets inserted inside the title attribute value, producing invalid HTML (and, depending on the pattern, allowing attribute content to be rewritten inside a raw tag).
  2. <[email protected]> — the mailto auto-link is destroyed entirely: the match splits the token so the auto-linker no longer recognizes it, and the angle brackets end up escaped.
  3. <img src="/x.png" alt="word"> — the alt attribute is corrupted the same way.

Root cause

LinkPatterns.run (lib/markdown2.py) guards matches with is_inside_link, which checks only _auto_link_re (<http://...> style auto-links) and _basic_link_re (markdown [...](...) syntax) — #385/#446 covered those. It does not check the other tokens that markdown2's own tokenizer _sorta_html_tokenize_re classifies as inline HTML markup: tags, comments, processing instructions and <[email protected]> style auto-link tokens. _do_auto_links runs after _do_links, so at the time the extra runs the email auto-link is still the raw <[email protected]> text and a match inside it destroys it.

Environment

python-markdown2 2.5.6 (current master, 6e89cff), Python 3.13, Linux.

Vorherrschende Sprache
Python
Sterne
2.8k
Forks
477
Ø Merge
4 T. 10 Std.
Gemergte PRs (30 T.)
12

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus trentm/python-markdown2

Alle Issues in trentm/python-markdown2

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.