Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
9 changes: 9 additions & 0 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,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])?)+(?![-_])/)
Expand Down Expand Up @@ -513,6 +521,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,
Expand Down
13 changes: 13 additions & 0 deletions test/specs/new/codespan_backslash_emphasis.html
Original file line number Diff line number Diff line change
@@ -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>
13 changes: 13 additions & 0 deletions test/specs/new/codespan_backslash_emphasis.md
Original file line number Diff line number Diff line change
@@ -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`_ ``
Loading