Skip to content

Commit 215f096

Browse files
Merge pull request #598 from Crozzers/gfm-link-refs
Add `link-shortrefs` extra (#597)
2 parents 929d505 + d49f798 commit 215f096

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [pull #590] Fix underscores within bold text getting emphasized (#589)
66
- [pull #591] Add Alerts extra
77
- [pull #595] Fix img alt text being processed as markdown (#594)
8+
- [pull #598] Add `link-shortrefs` extra (#597)
89
- [pull #600] Use urandom for SECRET_SALT
910
- [pull #602] Fix XSS issue in safe mode (#601)
1011
- [pull #604] Fix XSS injection in image URLs (#603)

lib/markdown2.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
this for other tags.
6565
* link-patterns: Auto-link given regex patterns in text (e.g. bug number
6666
references, revision number references).
67+
* link-shortrefs: allow shortcut reference links, not followed by `[]` or
68+
a link label.
6769
* markdown-in-html: Allow the use of `markdown="1"` in a block HTML tag to
6870
have markdown processing be done on its contents. Similar to
6971
<http://michelf.com/projects/php-markdown/extra/#markdown-attr> but with
@@ -1646,7 +1648,23 @@ def _do_links(self, text: str) -> str:
16461648

16471649
# Reference anchor or img?
16481650
else:
1649-
match = self._tail_of_reference_link_re.match(text, p)
1651+
match = None
1652+
if 'link-shortrefs' in self.extras:
1653+
# check if there's no tailing id section
1654+
if link_text and re.match(r'[ ]?(?:\n[ ]*)?(?!\[)', text[p:]):
1655+
# try a match with `[]` inserted into the text
1656+
match = self._tail_of_reference_link_re.match(f'{text[:p]}[]{text[p:]}', p)
1657+
if match:
1658+
# if we get a match, we'll have to modify the `text` variable to insert the `[]`
1659+
# but we ONLY want to do that if the link_id is valid. This makes sure that we
1660+
# don't get stuck in any loops and also that when a user inputs `[abc]` we don't
1661+
# output `[abc][]` in the final HTML
1662+
if (match.group("id").lower() or link_text.lower()) in self.urls:
1663+
text = f'{text[:p]}[]{text[p:]}'
1664+
else:
1665+
match = None
1666+
1667+
match = match or self._tail_of_reference_link_re.match(text, p)
16501668
if match:
16511669
# Handle a reference-style anchor or img.
16521670
is_img = start_idx > 0 and text[start_idx-1] == "!"

test/tm-cases/link_shortrefs.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p><a href="https://www.python.org">Python</a></p>
2+
3+
<p>abc <a href="https://www.python.org">Python</a> def</p>
4+
5+
<p><a href="https://www.python.org">Python</a>s</p>
6+
7+
<p><a href="https://www.python.org">Python</a></p>
8+
9+
<p><a href="https://www.python.org">Python</a>with<em>more</em>text</p>
10+
11+
<p>[NoLink]</p>

test/tm-cases/link_shortrefs.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["link-shortrefs"]}

test/tm-cases/link_shortrefs.tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extras link-shortrefs

test/tm-cases/link_shortrefs.text

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Python]
2+
3+
abc [Python] def
4+
5+
[Python]s
6+
7+
[Python][]
8+
9+
[Python]with_more_text
10+
11+
[NoLink]
12+
13+
[Python]: https://www.python.org

0 commit comments

Comments
 (0)