Skip to content

fix: don't linkify inside inline HTML markup tokens (link-patterns extra) - #771

Open
rahul05ranjan wants to merge 1 commit into
trentm:masterfrom
rahul05ranjan:fix/link-patterns-in-html-tags
Open

rahul05ranjan wants to merge 1 commit into
trentm:masterfrom
rahul05ranjan:fix/link-patterns-in-html-tags

Conversation

@rahul05ranjan

Copy link
Copy Markdown

With the link-patterns extra enabled, a pattern matching inside an inline HTML markup token got replaced there — inserting an anchor into an attribute value and destroying <word@example.com> auto-links:

>>> lp = [(re.compile(r"\b(word)\b"), r"/w/\1")]
>>> markdown2.markdown('See <span title="word">x</span>', extras=["link-patterns"], link_patterns=lp)
<p>See <span title="<a href="/w/word">word</a>">x</span></p>           # invalid HTML
>>> markdown2.markdown('<word@example.com>', extras=["link-patterns"], link_patterns=lp)
<p>&lt;<a href="/w/word">word</a>@example.com></p>                     # mailto auto-link destroyed

LinkPatterns.run excluded matches inside markdown link syntax and <http://…> auto-links (issues #385/#446 covered those), but not the other inline tokens markdown2's own tokenizer (_sorta_html_tokenize_re) classifies as markup: tags, comments, processing instructions, and <word@example.com> style auto-links — which _do_auto_links processes after the extra runs, so the match split the token before the auto-linker ever saw it.

This excludes matches that fall inside a _sorta_html_tokenize_re token. Prose still linkifies; matches inside an element's content still linkify; markup and auto-links are left intact for the later stages:

<p>See <span title="word"><a href="/w/word">word</a></span> here.</p>
<p><a href="mailto:word@example.com">word@example.com</a></p>
<p><img src="/x.png" alt="word"></p>
<p>A <a href="/w/word">word</a> in prose.</p>

Fixes #752.

Verification (system Python 3.13):

  • New test test/tm-cases/link_patterns_in_html_tags_issue752.{text,html,opts} covering all three symptoms plus prose/element-content controls: fails on master@6d0a518 with the exact attribute-corruption diff, passes with this change.
  • Full suite via test/test.py: exit 0, no failures (pygments present, wavedrom/latex2mathml/emoji cases skipped as unavailable).
  • Email auto-link output verified identical to the no-extra rendering after the harness's standard email-obfuscation normalization (which the tm-case comparison applies).

The link-patterns extra checked only markdown link syntax and <http://>
auto-links before replacing a match. A match inside any other inline
HTML markup token that markdown2's own tokenizer recognizes - a tag
(<span title="word">), a comment, a processing instruction, or a
<word@example.com> style auto-link - was replaced anyway, inserting an
anchor into an attribute value (invalid HTML) and destroying the email
auto-link before _do_auto_links could see it.

Exclude matches that fall inside _sorta_html_tokenize_re tokens, so
prose is still linkified while markup and auto-links are left intact
and _do_auto_links can produce the mailto link afterwards.

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant