Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## python-markdown2 2.5.6 (not yet released)

- Fix the `link-patterns` extra replacing matches inside inline HTML markup tokens (tag attributes, HTML comments, processing instructions, and `<word@example.com>` style auto-links) (#752)

- [pull #762] Speed up documents with many links by skipping the backslash-escape scan for text without a backslash
- [pull #748] Speed up `_unescape_special_chars` by skipping text with no hashes and dropping the full-text comparison per pass (#635)
- [pull #749] URL-encode destinations generated by the `link-patterns` extra (#55)
Expand Down
12 changes: 12 additions & 0 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3918,6 +3918,18 @@ def run(self, text: str):
continue
break

if not is_inside_link:
# Also exclude markdown2's own inline HTML markup tokens
# (tags, comments, processing instructions, and
# <word@example.com> style auto-links): a match inside
# one of them would corrupt the markup or destroy the
# auto-link (e.g. linkifying an attribute value).
for match in self.md._sorta_html_tokenize_re.finditer(text):
m_start, m_end = match.span(1)
if m_start <= start and end <= m_end:
is_inside_link = True
break

if is_inside_link:
continue

Expand Down
7 changes: 7 additions & 0 deletions test/tm-cases/link_patterns_in_html_tags_issue752.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<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>
5 changes: 5 additions & 0 deletions test/tm-cases/link_patterns_in_html_tags_issue752.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"extras": ["link-patterns"],
"link_patterns": [
(re.compile(r"\b(word)\b"), r"/w/\1"),
],
}
7 changes: 7 additions & 0 deletions test/tm-cases/link_patterns_in_html_tags_issue752.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
See <span title="word">word</span> here.

<word@example.com>

<img src="/x.png" alt="word">

A word in prose.
Loading