From d8b3284246af371162f586401994c46e321c6210 Mon Sep 17 00:00:00 2001 From: Crozzers Date: Wed, 17 Dec 2025 23:23:57 +0000 Subject: [PATCH 1/2] Fix unbound local error in GFM emphasis processing (#666) --- lib/markdown2.py | 20 +++++++++++--------- test/tm-cases/gfm_emphasis_issue666.html | 1 + test/tm-cases/gfm_emphasis_issue666.text | 1 + 3 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 test/tm-cases/gfm_emphasis_issue666.html create mode 100644 test/tm-cases/gfm_emphasis_issue666.text diff --git a/lib/markdown2.py b/lib/markdown2.py index 6af7c929..8d073cad 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -2786,17 +2786,19 @@ def has_middle( if len(open_syntax) < len(syntax) and opens: # expand the em span to the left, meaning we're covering additional chars. # check we don't cross an existing span border - if not self.body_crosses_span_borders(opens[-1], open): - middle = open + if self.body_crosses_span_borders(opens[-1], open): + return False - open = opens.pop(-1) - open_offset = unused_opens.pop(open, 0) - open_syntax = open.group(1)[open_offset:] + middle = open - if len(open_syntax) == len(syntax): - # if it turns out the previous open is a perfect match then ignore the middle part - # eg: **foo*bar** - middle = None + open = opens.pop(-1) + open_offset = unused_opens.pop(open, 0) + open_syntax = open.group(1)[open_offset:] + + if len(open_syntax) == len(syntax): + # if it turns out the previous open is a perfect match then ignore the middle part + # eg: **foo*bar** + middle = None elif len(open_syntax) > len(syntax) and unused_closes: # check if there is a previous closing delim run in the current body # since this is already within the body we don't need to do a cross-span border check diff --git a/test/tm-cases/gfm_emphasis_issue666.html b/test/tm-cases/gfm_emphasis_issue666.html new file mode 100644 index 00000000..8743e4f1 --- /dev/null +++ b/test/tm-cases/gfm_emphasis_issue666.html @@ -0,0 +1 @@ +

*test*

diff --git a/test/tm-cases/gfm_emphasis_issue666.text b/test/tm-cases/gfm_emphasis_issue666.text new file mode 100644 index 00000000..3ac0deb3 --- /dev/null +++ b/test/tm-cases/gfm_emphasis_issue666.text @@ -0,0 +1 @@ +*[*test**](https://example.com/) \ No newline at end of file From de9e432d051a326a0ea127c8385b715f184baa90 Mon Sep 17 00:00:00 2001 From: Crozzers Date: Wed, 17 Dec 2025 23:28:05 +0000 Subject: [PATCH 2/2] Update changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index c1c77f13..e1c9c958 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ - [pull #640] Fix code friendly extra stopping other syntax being processed (#638) - [pull #644] Fix a number of em/strong issues (#641, #642, #643) - [pull #659] Fix a number of safemode issues (#647) +- [pull #665] Rewrite emphasis and strong processing to be more GFM compliant ## python-markdown2 2.5.4