fix: apply emphasis flanking and rules 9-10 to the whole delimiter run (#4145)

This commit is contained in:
Yang Bai
2026-10-25 23:41:34 -06:00
committed by GitHub
parent 3a11ad1483
commit 5d8e2340d7
3 changed files with 55 additions and 12 deletions
+35 -12
View File
@@ -852,21 +852,44 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
if (!match) return;
if (!match[1] && !match[2] && !match[3] && !match[4]) return;
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 `**a<em>b</em>c`).
const midRun = prevChar === delimChar;
// The start of this delimiter run may already be behind src, either taken
// as text after the whole run failed to match or used to close an earlier
// span. Flanking and rules 9-10 are about the whole run, so find its start.
// Pedantic follows markdown.pl, which has no such rules.
const srcStart = maskedSrc.length - src.length;
let runStart = srcStart;
while (!this.options.pedantic && runStart > 0 && maskedSrc[runStart - 1] === delimChar) {
runStart--;
}
const runPrevChar = [...maskedSrc.slice(Math.max(0, runStart - 2), runStart)].at(-1) ?? '';
// The other delimiter character counts as punctuation here (`_**a*`)
const runPrevPunct = runPrevChar === '*' || runPrevChar === '_' || !!this.rules.inline.punctuation.exec(runPrevChar);
if (runStart < srcStart) {
prevChar = runPrevChar;
}
const prevPunct = runStart < srcStart ? runPrevPunct : !!this.rules.inline.punctuation.exec(prevChar);
// _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
if (match[4] && prevChar.match(this.rules.other.unicodeAlphaNumeric)) return;
const nextChar = match[1] || match[3] || '';
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
if (!nextChar || !prevChar || prevPunct) {
// unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)
const lLength = [...match[0]].length - 1;
let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
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 `**a<em>b</em>c`).
const midRun = prevChar === delimChar;
// Rules 9-10 count the whole run, and also apply when the opener could
// close as well (`a*b**`, where `*` is both left- and right-flanking).
const runLength = srcStart - runStart + lLength;
const openerCanClose = !this.options.pedantic && !!runPrevChar.trim()
&& (!!nextChar || (delimChar === '*' && !runPrevPunct));
const endReg = delimChar === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
endReg.lastIndex = 0;
@@ -883,16 +906,16 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
if (match[3] || match[4]) { // found another Left Delim
delimTotal += rLength;
continue;
} else if (match[5] || match[6]) { // either Left or Right Delim
if (lLength % 3 && !((lLength + rLength) % 3)) {
midDelimTotal += rLength;
continue; // CommonMark Emphasis Rules 9-10
}
if (midRun) {
} 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)) {
midDelimTotal += rLength;
continue; // CommonMark Emphasis Rules 9-10
}
}
delimTotal -= rLength;
@@ -0,0 +1,7 @@
<p>**<em>.a</em></p>
<p>*<em>(a)</em></p>
<p>foo **<em>!bar</em></p>
<p>a*b**</p>
<p>x a*b** y</p>
<p>__(_*</p>
<p>a_<em>a_</em>*</p>
+13
View File
@@ -0,0 +1,13 @@
***.a*
**(a)*
foo ***!bar*
a*b**
x a*b** y
__(_*
a_*a_**