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
16 changes: 16 additions & 0 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3861,11 +3861,27 @@ class LinkPatterns(Extra):
def run(self, text: str):
link_from_hash = {}
for regex, repl in self.options:
# Spans of inline HTML markup in the current text: tags,
# comments, processing instructions and `<...>` auto-link
# tokens (as recognized by the same tokenizer that
# `_escape_special_chars` uses). Matches inside them must
# not be linkified: that is markup, not prose, and replacing
# inside it corrupts attributes and destroys auto-links
# (e.g. `<word@example.com>`, `<img alt="word">`).
markup_spans = [
m.span(1)
for m in self.md._sorta_html_tokenize_re.finditer(text)
if self.md._is_unescaped_re.match(m.group(1))
]
replacements = []
for match in regex.finditer(text):
if any(self.md._match_overlaps_substr(text, match, h) for h in link_from_hash):
continue

# Do not match inside inline HTML markup.
if any(s <= match.start() and match.end() <= e for s, e in markup_spans):
continue

if callable(repl):
href = repl(match)
else:
Expand Down
1 change: 1 addition & 0 deletions test/tm-cases/link_patterns_email_autolink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><a href="mailto:foo@example.com">foo@example.com</a> is an auto-linked email, but plain <a href="/w/foo">foo</a> is still linkified.</p>
4 changes: 4 additions & 0 deletions test/tm-cases/link_patterns_email_autolink.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"extras": ["link-patterns"],
"link_patterns": [
(re.compile(r"\b(foo)\b"), r"/w/\1"),
]}
1 change: 1 addition & 0 deletions test/tm-cases/link_patterns_email_autolink.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
link_patterns
1 change: 1 addition & 0 deletions test/tm-cases/link_patterns_email_autolink.text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<foo@example.com> is an auto-linked email, but plain foo is still linkified.
7 changes: 7 additions & 0 deletions test/tm-cases/link_patterns_html_tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>Inline HTML attributes must not be linkified:</p>

<p><span title="foo bar">baz</span></p>

<p><img src="/qux.png" alt="foo"></p>

<p>But plain <a href="/w/foo">foo</a> in prose is linkified.</p>
4 changes: 4 additions & 0 deletions test/tm-cases/link_patterns_html_tags.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"extras": ["link-patterns"],
"link_patterns": [
(re.compile(r"\b(foo)\b"), r"/w/\1"),
]}
1 change: 1 addition & 0 deletions test/tm-cases/link_patterns_html_tags.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
link_patterns
7 changes: 7 additions & 0 deletions test/tm-cases/link_patterns_html_tags.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Inline HTML attributes must not be linkified:

<span title="foo bar">baz</span>

<img src="/qux.png" alt="foo">

But plain foo in prose is linkified.
Loading