From 06e1d1090338b76c3d458a26d52ee67f72c8d6e1 Mon Sep 17 00:00:00 2001 From: yash Date: Mon, 16 Nov 2026 21:57:25 +0530 Subject: [PATCH] fix: let a mid-run opener close against a delimiter that can also open (#4203) --- src/Tokenizer.ts | 15 ++++++++------- .../new/em_strong_mid_run_partial_close.html | 11 +++++++++++ test/specs/new/em_strong_mid_run_partial_close.md | 11 +++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 test/specs/new/em_strong_mid_run_partial_close.html create mode 100644 test/specs/new/em_strong_mid_run_partial_close.md diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 9daf72ea..7f8c03f7 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -1033,14 +1033,15 @@ export class _Tokenizer { delimTotal += rLength; continue; } else if (match[5] || match[6] || openerCanClose) { // either Left or Right Delim - if (midRun && (match[5] || match[6])) { - // A mid-run opener cannot close against an ambiguous delimiter that - // can also open; that delimiter opens its own emphasis span instead. - break; - } - if (runLength % 3 && !((runLength + rLength) % 3)) { + if (runLength % 3 && !((runLength + rLength) % 3)) { // CommonMark Emphasis Rules 9-10 + if (midRun && (match[5] || match[6])) { + // A delimiter that can also open and cannot close this run + // opens its own span; a later closer pairs with it, not with a + // mid-run opener (`**a*a*` must be `**aa`). + break; + } midDelimTotal += rLength; - continue; // CommonMark Emphasis Rules 9-10 + continue; } } diff --git a/test/specs/new/em_strong_mid_run_partial_close.html b/test/specs/new/em_strong_mid_run_partial_close.html new file mode 100644 index 00000000..e905b97b --- /dev/null +++ b/test/specs/new/em_strong_mid_run_partial_close.html @@ -0,0 +1,11 @@ +

**aa

+ +

*aa

+ +

*aa

+ +

***aa

+ +

-**[h]"a

+ +

**aa

diff --git a/test/specs/new/em_strong_mid_run_partial_close.md b/test/specs/new/em_strong_mid_run_partial_close.md new file mode 100644 index 00000000..4c6ead0a --- /dev/null +++ b/test/specs/new/em_strong_mid_run_partial_close.md @@ -0,0 +1,11 @@ +***a*a + +***a**a + +***a*a* + +****a*a + +-***[h]*"a + +**a*a*