fix: keep the em/strong mask the same length as the source (#4044)

emStrong and del align maskedSrc with src by slicing the mask from the
end, so every mask must keep the length of the text it replaces. The
escape mask substituted a fixed two-character '++', but anyPunctuation is
/\\([\p{P}\p{S}])/gu and matches astral code points, so an escaped
character above U+FFFF is three code units masked as two.

The mask then ended up shorter than the source, the clip reached one code
unit too far back, and the resulting match.index was applied to src: the
closing delimiter was consumed a code unit late, leaving a stray
delimiter inside the emphasis and, for some inputs, dropping a character
of the document.

Substitute one '+' per code unit instead. '+' is punctuation, so the
delimiter flanking of the masked region is unchanged, and the ASCII case
still produces '++'.

Co-authored-by: Jayesh <jayesh.bhade@keychain.com>
This commit is contained in:
Jayesh Bhade
2026-08-05 08:51:04 -06:00
committed by GitHub
co-authored by Jayesh
parent 81bd750533
commit 681373ccc0
3 changed files with 28 additions and 2 deletions
+5 -2
View File
@@ -316,8 +316,11 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
}
}
// Mask out escaped characters
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.anyPunctuation, '++');
// Mask out escaped characters.
// Every mask must keep the length it replaces: emStrong and del line
// maskedSrc up with src by slicing from the end. `anyPunctuation` matches
// unicode punctuation, so an escaped astral character is 3 code units.
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.anyPunctuation, match0 => '+'.repeat(match0.length));
// Mask out other blocks
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.blockSkip, (match0, _link, context) => {
@@ -0,0 +1,7 @@
<p><em>a</em>\🙂</p>
<p><em>a</em> \🙂 <em>b</em></p>
<p>x<em>ab</em>\🙂y</p>
<p><em>a</em>a\𐄀<em>b</em></p>
<p><strong>a</strong>\🙂b</p>
<p><del>a</del>\🙂</p>
<p><em>foo\🙂bar</em></p>
@@ -0,0 +1,16 @@
---
gfm: true
---
*a*\🙂
*a* \🙂 *b*
x*ab*\🙂y
*a*a\𐄀*b*
__a__\🙂b
~~a~~\🙂
*foo\🙂bar*