Skip to content

Commit

Permalink
Merge pull request #501 from Crozzers/fix-link-patterns-issue287
Browse files Browse the repository at this point in the history
Fix link patterns extra matching against internal hashes
  • Loading branch information
nicholasserra committed Apr 11, 2023
2 parents d657fca + 01a98a1 commit bce3f18
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -3,6 +3,7 @@
## python-markdown2 2.4.9 (not yet released)

- [pull #500] Add `<thead>` tag to html-classes extra
- [pull #501] Fix link patterns extra matching against internal hashes
- [pull #502] Replace deprecated `optparse` with `argparse`
- [pull #506] Fix `_uniform_outdent` failing with empty strings (issue #505)

Expand Down
16 changes: 16 additions & 0 deletions lib/markdown2.py
Expand Up @@ -2533,6 +2533,9 @@ def _do_link_patterns(self, text):
for regex, repl in self.link_patterns:
replacements = []
for match in regex.finditer(text):
if any(self._match_overlaps_substr(text, match, h) for h in link_from_hash):
continue

if hasattr(repl, "__call__"):
href = repl(match)
else:
Expand Down Expand Up @@ -2642,6 +2645,19 @@ def _uniform_indent(self, text, indent, include_empty_lines=False):
for line in text.splitlines(True)
)

@staticmethod
def _match_overlaps_substr(text, match, substr):
'''
Checks if a regex match overlaps with a substring in the given text.
'''
for instance in re.finditer(re.escape(substr), text):
start, end = instance.span()
if start <= match.start() <= end:
return True
if start <= match.end() <= end:
return True
return False


class MarkdownWithExtras(Markdown):
"""A markdowner class that enables most extras:
Expand Down
1 change: 1 addition & 0 deletions test/tm-cases/link_patterns_hash_matching_issue287.html
@@ -0,0 +1 @@
<p>this is a test issue <a href="https://github.com/pyfa-org/Pyfa/issues/1234">#1234</a> with a test commit (<a href="https://github.com/pyfa-org/Pyfa/commit/addeddd">addeddd</a>) made by test <a href="https://github.com/username">@username</a> more text</p>
7 changes: 7 additions & 0 deletions test/tm-cases/link_patterns_hash_matching_issue287.opts
@@ -0,0 +1,7 @@
{"extras": ["link-patterns"],
"link_patterns": [
(re.compile("#(\d+)", re.I), r"https://github.com/pyfa-org/Pyfa/issues/\1"),
(re.compile("@(\w+)", re.I), r"https://github.com/\1"),
(re.compile("([0-9a-f]{6,40})", re.I), r"https://github.com/pyfa-org/Pyfa/commit/\1")
]
}
1 change: 1 addition & 0 deletions test/tm-cases/link_patterns_hash_matching_issue287.text
@@ -0,0 +1 @@
this is a test issue #1234 with a test commit (addeddd) made by test @username more text

0 comments on commit bce3f18

Please sign in to comment.