Skip to content

Commit

Permalink
Add link-shortrefs extra (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Crozzers committed Sep 21, 2024
1 parent 1e0fbf2 commit de8cea7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
<http://michelf.com/projects/php-markdown/extra/#markdown-attr> but with
Expand Down Expand Up @@ -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] == "!"
Expand Down
11 changes: 11 additions & 0 deletions test/tm-cases/link_shortrefs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p><a href="https://www.python.org">Python</a></p>

<p>abc <a href="https://www.python.org">Python</a> def</p>

<p><a href="https://www.python.org">Python</a>s</p>

<p><a href="https://www.python.org">Python</a></p>

<p><a href="https://www.python.org">Python</a>with<em>more</em>text</p>

<p>[NoLink]</p>
1 change: 1 addition & 0 deletions test/tm-cases/link_shortrefs.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["link-shortrefs"]}
1 change: 1 addition & 0 deletions test/tm-cases/link_shortrefs.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extras link-shortrefs
13 changes: 13 additions & 0 deletions test/tm-cases/link_shortrefs.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Python]

abc [Python] def

[Python]s

[Python][]

[Python]with_more_text

[NoLink]

[Python]: https://www.python.org

0 comments on commit de8cea7

Please sign in to comment.