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

Aberta
#752 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 3 dias

@rahul05ranjan já está trabalhando nisso.

Desde 28/10/2026.

  • #753 de @rahul05ranjan — aberto

Avaliação

Dificuldade
3/5
Tempo estimado
1-2 dias
Facilidade para iniciantes
30/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Estagnada
Stack de tecnologia
python
Domínio
backend

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
Python
Estrelas
2.8k
Forks
477
Merge médio
3d 19h
PRs com merge (30d)
14

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de trentm/python-markdown2

Todas as issues de trentm/python-markdown2

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.