diff --git a/CHANGES.md b/CHANGES.md index 10aabd33..35a547f4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 `` 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) diff --git a/lib/markdown2.py b/lib/markdown2.py index d6285404..34928b51 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -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 + # 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 diff --git a/test/tm-cases/link_patterns_in_html_tags_issue752.html b/test/tm-cases/link_patterns_in_html_tags_issue752.html new file mode 100644 index 00000000..f339cef9 --- /dev/null +++ b/test/tm-cases/link_patterns_in_html_tags_issue752.html @@ -0,0 +1,7 @@ +

See word here.

+ +

word@example.com

+ +

word

+ +

A word in prose.

diff --git a/test/tm-cases/link_patterns_in_html_tags_issue752.opts b/test/tm-cases/link_patterns_in_html_tags_issue752.opts new file mode 100644 index 00000000..31c85880 --- /dev/null +++ b/test/tm-cases/link_patterns_in_html_tags_issue752.opts @@ -0,0 +1,5 @@ +{"extras": ["link-patterns"], + "link_patterns": [ + (re.compile(r"\b(word)\b"), r"/w/\1"), + ], +} diff --git a/test/tm-cases/link_patterns_in_html_tags_issue752.text b/test/tm-cases/link_patterns_in_html_tags_issue752.text new file mode 100644 index 00000000..66a798e2 --- /dev/null +++ b/test/tm-cases/link_patterns_in_html_tags_issue752.text @@ -0,0 +1,7 @@ +See word here. + + + +word + +A word in prose.