From a26c1887ff054b80081d7b9ac9765d07797d464b Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:53:00 +0200 Subject: [PATCH] fix: prevent autolink parsing in images and links --- src/mdformat_gfm/_mdit_gfm_autolink_plugin.py | 4 ++ tests/data/gfm_autolink.md | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/mdformat_gfm/_mdit_gfm_autolink_plugin.py b/src/mdformat_gfm/_mdit_gfm_autolink_plugin.py index aa320b4..d877550 100644 --- a/src/mdformat_gfm/_mdit_gfm_autolink_plugin.py +++ b/src/mdformat_gfm/_mdit_gfm_autolink_plugin.py @@ -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 diff --git a/tests/data/gfm_autolink.md b/tests/data/gfm_autolink.md index 93af7b4..5f0594a 100644 --- a/tests/data/gfm_autolink.md +++ b/tests/data/gfm_autolink.md @@ -29,6 +29,37 @@ don't touch text in html tags

https://example.com

. + +space separated autolink in html tags +. + https://example.com +. +

https://example.com

+. + +space separated autolink after html tag +. + https://example.com +. +

https://example.com

+. + +autolink in link after tag +. + [t https://example.fi](https://example.com) +. +

t https://example.fi

+. + + +autolink in link after tag +. + [t https://example.fi](https://example.com) +. +

t https://example.fi

+. + + entities inside raw links . https://example.com/foo&bar @@ -262,3 +293,23 @@ This doesnt http://example.org/foo.*bar*-*baz .

This doesnt http://example.org/foo.*bar*-*baz

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

t https://blaa.org

+

https://blaa.org

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

t https://blaa.org

+

https://blaa.org

+.