From ad9447bf49b548021efdd22315281b8c9d883fe1 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 2 Jul 2026 14:20:36 +0100 Subject: [PATCH] fix: an unmatched strong run before emphasis stays literal --- src/Tokenizer.ts | 12 +++++++++++- test/specs/new/em_strong_unclosed_orphan.html | 1 + test/specs/new/em_strong_unclosed_orphan.md | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/specs/new/em_strong_unclosed_orphan.html create mode 100644 test/specs/new/em_strong_unclosed_orphan.md diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index e6f900d457..d0389534a3 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -746,7 +746,12 @@ export class _Tokenizer { const lLength = [...match[0]].length - 1; let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0; - const endReg = match[0][0] === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd; + const delimChar = match[0][0]; + // A mid-run opener (for example the second star of an unmatched `**`) must + // only pair with a delimiter that can only close, otherwise it steals the + // opener of a later span (`**a*b*c` must be `**abc`). + const midRun = prevChar === delimChar; + const endReg = delimChar === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd; endReg.lastIndex = 0; // Clip maskedSrc to same section of string as src (move to lexer?) @@ -767,6 +772,11 @@ export class _Tokenizer { midDelimTotal += rLength; continue; // CommonMark Emphasis Rules 9-10 } + if (midRun) { + // A mid-run opener cannot close against an ambiguous delimiter that + // can also open; that delimiter opens its own emphasis span instead. + break; + } } delimTotal -= rLength; diff --git a/test/specs/new/em_strong_unclosed_orphan.html b/test/specs/new/em_strong_unclosed_orphan.html new file mode 100644 index 0000000000..0b721adf83 --- /dev/null +++ b/test/specs/new/em_strong_unclosed_orphan.html @@ -0,0 +1 @@ +

**abc

diff --git a/test/specs/new/em_strong_unclosed_orphan.md b/test/specs/new/em_strong_unclosed_orphan.md new file mode 100644 index 0000000000..a1ab9ab1cd --- /dev/null +++ b/test/specs/new/em_strong_unclosed_orphan.md @@ -0,0 +1 @@ +**a*b*c