diff --git a/CHANGES.md b/CHANGES.md index 8db51bf1..da3669de 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ - [pull #590] Fix underscores within bold text getting emphasized (#589) - [pull #591] Add Alerts extra - [pull #595] Fix img alt text being processed as markdown (#594) +- [pull #598] Add `link-shortrefs` extra (#597) - [pull #600] Use urandom for SECRET_SALT - [pull #602] Fix XSS issue in safe mode (#601) - [pull #604] Fix XSS injection in image URLs (#603) diff --git a/lib/markdown2.py b/lib/markdown2.py index 2473dd01..17317933 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -64,6 +64,8 @@ this for other tags. * link-patterns: Auto-link given regex patterns in text (e.g. bug number references, revision number references). +* link-shortrefs: allow shortcut reference links, not followed by `[]` or + a link label. * markdown-in-html: Allow the use of `markdown="1"` in a block HTML tag to have markdown processing be done on its contents. Similar to but with @@ -1646,7 +1648,23 @@ def _do_links(self, text: str) -> str: # Reference anchor or img? else: - match = self._tail_of_reference_link_re.match(text, p) + match = None + if 'link-shortrefs' in self.extras: + # check if there's no tailing id section + if link_text and re.match(r'[ ]?(?:\n[ ]*)?(?!\[)', text[p:]): + # try a match with `[]` inserted into the text + match = self._tail_of_reference_link_re.match(f'{text[:p]}[]{text[p:]}', p) + if match: + # if we get a match, we'll have to modify the `text` variable to insert the `[]` + # but we ONLY want to do that if the link_id is valid. This makes sure that we + # don't get stuck in any loops and also that when a user inputs `[abc]` we don't + # output `[abc][]` in the final HTML + if (match.group("id").lower() or link_text.lower()) in self.urls: + text = f'{text[:p]}[]{text[p:]}' + else: + match = None + + match = match or self._tail_of_reference_link_re.match(text, p) if match: # Handle a reference-style anchor or img. is_img = start_idx > 0 and text[start_idx-1] == "!" diff --git a/test/tm-cases/link_shortrefs.html b/test/tm-cases/link_shortrefs.html new file mode 100644 index 00000000..02824ce5 --- /dev/null +++ b/test/tm-cases/link_shortrefs.html @@ -0,0 +1,11 @@ +

Python

+ +

abc Python def

+ +

Pythons

+ +

Python

+ +

Pythonwithmoretext

+ +

[NoLink]

diff --git a/test/tm-cases/link_shortrefs.opts b/test/tm-cases/link_shortrefs.opts new file mode 100644 index 00000000..5e139454 --- /dev/null +++ b/test/tm-cases/link_shortrefs.opts @@ -0,0 +1 @@ +{"extras": ["link-shortrefs"]} diff --git a/test/tm-cases/link_shortrefs.tags b/test/tm-cases/link_shortrefs.tags new file mode 100644 index 00000000..c922086f --- /dev/null +++ b/test/tm-cases/link_shortrefs.tags @@ -0,0 +1 @@ +extras link-shortrefs diff --git a/test/tm-cases/link_shortrefs.text b/test/tm-cases/link_shortrefs.text new file mode 100644 index 00000000..c7c1adbf --- /dev/null +++ b/test/tm-cases/link_shortrefs.text @@ -0,0 +1,13 @@ +[Python] + +abc [Python] def + +[Python]s + +[Python][] + +[Python]with_more_text + +[NoLink] + +[Python]: https://www.python.org