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

fix: prevent autolink parsing in images and links #50

Merged
merged 1 commit into from
Dec 13, 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
4 changes: 4 additions & 0 deletions src/mdformat_gfm/_mdit_gfm_autolink_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def gfm_autolink(state: StateInline, silent: bool) -> bool: # noqa: C901
Returns:
bool: True if GFM autolink found.
"""
# Prevents autolink parsing in link and image labels
if state.level > 0:
return False

pos = state.pos
src = state.src

Expand Down
51 changes: 51 additions & 0 deletions tests/data/gfm_autolink.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,37 @@ don't touch text in html <a> tags
<p><a href="https://example.com">https://example.com</a></p>
.


space separated autolink in html <a> tags
.
<a href="https://example.fi"> https://example.com </a>
.
<p><a href="https://example.fi"> <a href="https://example.com">https://example.com</a> </a></p>
.

space separated autolink after html </a> tag
.
</a> https://example.com
.
<p></a> <a href="https://example.com">https://example.com</a></p>
.

autolink in link after </a> tag
.
</a> [t https://example.fi](https://example.com)
.
<p></a> <a href="https://example.com">t https://example.fi</a></p>
.


autolink in link after <a> tag
.
<a> [t https://example.fi](https://example.com)
.
<p><a> <a href="https://example.com">t https://example.fi</a></p>
.


entities inside raw links
.
https://example.com/foo&amp;bar
Expand Down Expand Up @@ -262,3 +293,23 @@ This doesnt http://example.org/foo.*bar*-*baz
.
<p>This doesnt <a href="http://example.org/foo.*bar*-*baz">http://example.org/foo.*bar*-*baz</a></p>
.

autolink inside link
.
[t https://blaa.org](https://www.gaah.fi)

[https://blaa.org](https://www.gaah.fi)
.
<p><a href="https://www.gaah.fi">t https://blaa.org</a></p>
<p><a href="https://www.gaah.fi">https://blaa.org</a></p>
.

autolink inside image
.
![t https://blaa.org](https://www.gaah.fi)

![https://blaa.org](https://www.gaah.fi)
.
<p><img src="https://www.gaah.fi" alt="t https://blaa.org" /></p>
<p><img src="https://www.gaah.fi" alt="https://blaa.org" /></p>
.
Loading