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

Ouverte
#752 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 3 jours

@rahul05ranjan y travaille déjà.

Depuis le 28/10/2026.

  • #753 par @rahul05ranjan — ouverte

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
30/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
python
Domaine
backend

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Python
Étoiles
2.8k
Forks
477
Merge moyen
4 j 10 h
PR mergées (30 j)
12

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de trentm/python-markdown2

Toutes les issues de trentm/python-markdown2

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.