Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link-shortrefs extra (#597) #598

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
- [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)
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 @@ -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] == "!"
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
Loading