fix: keep a backslash in a code span from hiding emphasis (#4117)

* fix: keep a backslash in a code span from hiding emphasis

The em/strong mask replaced every backslash escape before it masked
code spans, but a backslash inside a code span is literal. A code span
ending in `\` therefore lost its closing backtick in the mask, so the
next backtick paired with it and the emphasis between them was masked
out and rendered as literal `_` or `*`.

Mask escapes only outside code spans.

* fix: keep emphasis out of a code span that holds a backtick

---------

Co-authored-by: breken-ai <312387581+breken-ai@users.noreply.github.com>
This commit is contained in:
breken
2026-10-10 13:12:56 -04:00
committed by GitHub
co-authored by breken-ai
parent eaa1fffb30
commit 6a57018fc9
4 changed files with 46 additions and 1 deletions
+11 -1
View File
@@ -397,7 +397,17 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
// 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));
// A backslash inside a code span is literal, not an escape. Mask the
// backticks inside the span instead, so blockSkip still skips all of it.
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.anyPunctuationOutsideCode, (match0, code, punct) => {
if (punct) {
return '+'.repeat(match0.length);
}
if (code) {
return code + match0.slice(code.length, -code.length).replace(/`/g, '+') + code;
}
return match0;
});
// Mask out other blocks
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.blockSkip, (match0, _link, context) => {
+9
View File
@@ -432,6 +432,14 @@ const anyPunctuation = edit(/\\(punct)/, 'gu')
.replace(/punct/g, _punctuation)
.getRegex();
// `anyPunctuation` for the em/strong mask. A backslash is literal inside a code
// span, so a code span (matched like `inline.code`), or a backtick run that
// opens none, is matched whole and kept; only the escapes outside code spans
// are masked.
const anyPunctuationOutsideCode = edit(/(`+)(?:[^`]|[^`][\s\S]*?[^`])\1(?!`)|`+|\\(punct)/, 'gu')
.replace(/punct/g, _punctuation)
.getRegex();
const autolink = edit(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/)
.replace('scheme', /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/)
.replace('email', /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/)
@@ -514,6 +522,7 @@ const _extendedEmailProtocol = edit(/(?:mailto:email|xmpp:email(?:\/[A-Za-z0-9@.
const inlineNormal = {
_backpedal: noopTest, // only used for GFM url
anyPunctuation,
anyPunctuationOutsideCode,
autolink,
blockSkip,
br,
@@ -0,0 +1,13 @@
<p>Note that <code>\</code> or <code>/</code> <em>will</em> be interpreted as path separators on Windows, and will match against <code>/</code> in glob expressions.</p>
<p>Use <code>C:\Users\</code> for <strong>home</strong> and <code>D:\</code> for data.</p>
<p><code>a\</code> <em>b</em> <code>c</code></p>
<p>`<em>escaped tick</em>` <code>code</code></p>
<p><code>\</code> <code>\</code> <strong>after double backticks</strong></p>
<p>_<code>\`_</code></p>
<p>_<code>a`_</code></p>
@@ -0,0 +1,13 @@
Note that `\` or `/` _will_ be interpreted as path separators on Windows, and will match against `/` in glob expressions.
Use `C:\Users\` for **home** and `D:\` for data.
`a\` *b* `c`
\`*escaped tick*\` `code`
`\` ``\`` **after double backticks**
_``\`_``
_`` a`_ ``