Skip to content

Commit e8544e6

Browse files
authored
fix: an unmatched strong run before emphasis stays literal (#4008)
1 parent 823093f commit e8544e6

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/Tokenizer.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,12 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
746746
const lLength = [...match[0]].length - 1;
747747
let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
748748

749-
const endReg = match[0][0] === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
749+
const delimChar = match[0][0];
750+
// A mid-run opener (for example the second star of an unmatched `**`) must
751+
// only pair with a delimiter that can only close, otherwise it steals the
752+
// opener of a later span (`**a*b*c` must be `**a<em>b</em>c`).
753+
const midRun = prevChar === delimChar;
754+
const endReg = delimChar === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
750755
endReg.lastIndex = 0;
751756

752757
// Clip maskedSrc to same section of string as src (move to lexer?)
@@ -767,6 +772,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
767772
midDelimTotal += rLength;
768773
continue; // CommonMark Emphasis Rules 9-10
769774
}
775+
if (midRun) {
776+
// A mid-run opener cannot close against an ambiguous delimiter that
777+
// can also open; that delimiter opens its own emphasis span instead.
778+
break;
779+
}
770780
}
771781

772782
delimTotal -= rLength;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>**a<em>b</em>c</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**a*b*c

0 commit comments

Comments
 (0)