From de8cea72aa494a871448d5d04a1122158c5a1b5e Mon Sep 17 00:00:00 2001 From: Crozzers Date: Sat, 21 Sep 2024 14:00:22 +0100 Subject: [PATCH] Add `link-shortrefs` extra (#597) --- CHANGES.md | 1 + lib/markdown2.py | 20 +++++++++++++++++++- test/tm-cases/link_shortrefs.html | 11 +++++++++++ test/tm-cases/link_shortrefs.opts | 1 + test/tm-cases/link_shortrefs.tags | 1 + test/tm-cases/link_shortrefs.text | 13 +++++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/tm-cases/link_shortrefs.html create mode 100644 test/tm-cases/link_shortrefs.opts create mode 100644 test/tm-cases/link_shortrefs.tags create mode 100644 test/tm-cases/link_shortrefs.text diff --git a/CHANGES.md b/CHANGES.md index 4283b966..97013952 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) ## python-markdown2 2.5.0 diff --git a/lib/markdown2.py b/lib/markdown2.py index d0f10eea..fdea42fd 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 @@ -1625,7 +1627,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