Skip to content

Commit 6a57018

Browse files
authored
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 <[email protected]>
1 parent eaa1fff commit 6a57018

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎src/Lexer.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,17 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
397397
// Every mask must keep the length it replaces: emStrong and del line
398398
// maskedSrc up with src by slicing from the end. `anyPunctuation` matches
399399
// unicode punctuation, so an escaped astral character is 3 code units.
400-
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.anyPunctuation, match0 => '+'.repeat(match0.length));
400+
// A backslash inside a code span is literal, not an escape. Mask the
401+
// backticks inside the span instead, so blockSkip still skips all of it.
402+
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.anyPunctuationOutsideCode, (match0, code, punct) => {
403+
if (punct) {
404+
return '+'.repeat(match0.length);
405+
}
406+
if (code) {
407+
return code + match0.slice(code.length, -code.length).replace(/`/g, '+') + code;
408+
}
409+
return match0;
410+
});
401411

402412
// Mask out other blocks
403413
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.blockSkip, (match0, _link, context) => {

‎src/rules.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,14 @@ const anyPunctuation = edit(/\\(punct)/, 'gu')
432432
.replace(/punct/g, _punctuation)
433433
.getRegex();
434434

435+
// `anyPunctuation` for the em/strong mask. A backslash is literal inside a code
436+
// span, so a code span (matched like `inline.code`), or a backtick run that
437+
// opens none, is matched whole and kept; only the escapes outside code spans
438+
// are masked.
439+
const anyPunctuationOutsideCode = edit(/(`+)(?:[^`]|[^`][\s\S]*?[^`])\1(?!`)|`+|\\(punct)/, 'gu')
440+
.replace(/punct/g, _punctuation)
441+
.getRegex();
442+
435443
const autolink = edit(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/)
436444
.replace('scheme', /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/)
437445
.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@.
514522
const inlineNormal = {
515523
_backpedal: noopTest, // only used for GFM url
516524
anyPunctuation,
525+
anyPunctuationOutsideCode,
517526
autolink,
518527
blockSkip,
519528
br,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<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>
2+
3+
<p>Use <code>C:\Users\</code> for <strong>home</strong> and <code>D:\</code> for data.</p>
4+
5+
<p><code>a\</code> <em>b</em> <code>c</code></p>
6+
7+
<p>`<em>escaped tick</em>` <code>code</code></p>
8+
9+
<p><code>\</code> <code>\</code> <strong>after double backticks</strong></p>
10+
11+
<p>_<code>\`_</code></p>
12+
13+
<p>_<code>a`_</code></p>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Note that `\` or `/` _will_ be interpreted as path separators on Windows, and will match against `/` in glob expressions.
2+
3+
Use `C:\Users\` for **home** and `D:\` for data.
4+
5+
`a\` *b* `c`
6+
7+
\`*escaped tick*\` `code`
8+
9+
`\` ``\`` **after double backticks**
10+
11+
_``\`_``
12+
13+
_`` a`_ ``

0 commit comments

Comments
 (0)