From 4b3d2c28d52a5d70713a1443703ed7723dd8ac1a Mon Sep 17 00:00:00 2001 From: nk-batman21 Date: Tue, 20 Oct 2026 09:40:37 +0700 Subject: [PATCH 1/2] Fix link patterns inside wrapped Markdown labels --- README.md | 3 +++ lib/markdown2.py | 3 ++- test/tm-cases/link_patterns_markdown_syntax.html | 12 ++++++++++++ test/tm-cases/link_patterns_markdown_syntax.text | 14 +++++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0051a940..e1a20b98 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,9 @@ as a script: The `footnotes` extra keeps the same number when a footnote is referenced more than once. +The `link-patterns` extra leaves inline Markdown links and images unchanged, +including when their labels wrap onto another line. + The `tables` extra supports code spans delimited by single or multiple backticks, including an operator cell such as ```` ``|`` ````. A pipe immediately after the closing delimiter separates the next cell. diff --git a/lib/markdown2.py b/lib/markdown2.py index a55f7098..01984e47 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -3841,7 +3841,8 @@ class LinkPatterns(Extra): order = (Stage.LINKS,), () options: _link_patterns - _basic_link_re = re.compile(r'!?\[.*?\]\(.*?\)') + # LinkProcessor's 3000-character sentinel includes both label brackets. + _basic_link_re = re.compile(r'!?\[(?s:.{0,2998}?)\]\(.*?\)') def run(self, text: str): link_from_hash = {} diff --git a/test/tm-cases/link_patterns_markdown_syntax.html b/test/tm-cases/link_patterns_markdown_syntax.html index 968b9b48..7debe621 100644 --- a/test/tm-cases/link_patterns_markdown_syntax.html +++ b/test/tm-cases/link_patterns_markdown_syntax.html @@ -5,3 +5,15 @@

[Recipe 123]

Recipe 123

+ +

See +Recipe 123

+ +

See
+Recipe 123

+ +

Recipe 123 before See +Recipe 456 and Recipe 789 after.

+ +

Recipe 123 [nested] +label

diff --git a/test/tm-cases/link_patterns_markdown_syntax.text b/test/tm-cases/link_patterns_markdown_syntax.text index 4c0bff19..f19cf010 100644 --- a/test/tm-cases/link_patterns_markdown_syntax.text +++ b/test/tm-cases/link_patterns_markdown_syntax.text @@ -4,4 +4,16 @@ [Recipe 123] -Recipe 123 \ No newline at end of file +Recipe 123 + +[See +Recipe 123](https://example.com/recipe) + +![See +Recipe 123](recipe.png) + +Recipe 123 before [See +Recipe 456](https://example.com/other) and Recipe 789 after. + +[Recipe 123 [nested] +label](https://example.com/nested) From 9887e636d2b4819c241546c4df1bcf35ca9c013e Mon Sep 17 00:00:00 2001 From: nk-batman21 Date: Sat, 24 Oct 2026 16:00:41 +0700 Subject: [PATCH 2/2] Move the note out of the README and add a change note --- CHANGES.md | 1 + README.md | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 90f815ec..f4b326a9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,7 @@ - [pull #734] Fix empty list items being parsed as nested lists (#167) - [pull #735] Fix `smarty-pants` extra converting quotes inside inline HTML tags and comments (#150) - [pull #737] Fix XSS from code spans that the span gamut doesn't treat as code spans in safe mode (#726) +- [pull #747] Fix `link-patterns` extra linking text inside Markdown links and images whose label wraps onto another line ## python-markdown2 2.5.5 diff --git a/README.md b/README.md index e1a20b98..0051a940 100644 --- a/README.md +++ b/README.md @@ -106,9 +106,6 @@ as a script: The `footnotes` extra keeps the same number when a footnote is referenced more than once. -The `link-patterns` extra leaves inline Markdown links and images unchanged, -including when their labels wrap onto another line. - The `tables` extra supports code spans delimited by single or multiple backticks, including an operator cell such as ```` ``|`` ````. A pipe immediately after the closing delimiter separates the next cell.