fix: let emphasis open and close in angle brackets that are not html (#4190)

This commit is contained in:
kevin
2026-11-08 11:11:08 -07:00
committed by GitHub
parent 4cb767a966
commit 7bfa1f70e8
3 changed files with 53 additions and 4 deletions
+33 -4
View File
@@ -542,6 +542,38 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
return tokens;
}
/**
* Mask links, code spans, raw html and autolinks. blockSkip finds every
* `<...>` run, but only real raw html and autolinks take precedence over
* emphasis: other text in angle brackets, like `<*name*>`, stays unmasked so
* emphasis can open and close in it. A tag may also run past the first `>`,
* as in `<a title=">">`, so mask the whole tag.
*/
#maskBlocks(src: string): string {
const { blockSkip, tag, autolink } = this.tokenizer.rules.inline;
let masked = '';
let last = 0;
let match: RegExpExecArray | null;
blockSkip.lastIndex = 0;
while ((match = blockSkip.exec(src))) {
const start = match.index + (match[2] ? match[2].length : 0);
let end = match.index + match[0].length;
if (src.charAt(start) === '<') {
const rest = src.slice(start);
const html = tag.exec(rest) || autolink.exec(rest);
if (!html) {
blockSkip.lastIndex = start + 1;
continue;
}
end = start + html[0].length;
blockSkip.lastIndex = end;
}
masked += src.slice(last, start) + '[' + 'a'.repeat(end - start - 2) + ']';
last = end;
}
return masked + src.slice(last);
}
/**
* Mask links, code, html and escapes so emStrong and del do not see their
* delimiters inside them.
@@ -592,10 +624,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
});
// Mask out other blocks
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.blockSkip, (match0, _link, context) => {
const offset = context ? context.length : 0;
return match0.slice(0, offset) + '[' + 'a'.repeat(match0.length - offset - 2) + ']';
});
maskedSrc = this.#maskBlocks(maskedSrc);
// Mask out blocks from extensions
maskedSrc = this.options.hooks?.emStrongMask?.call({ lexer: this }, maskedSrc) ?? maskedSrc;
+7
View File
@@ -0,0 +1,7 @@
<p>Replace &lt;<em>name</em>&gt; with your name.</p>
<p>Pick &lt;<em>one</em>&gt; or &lt;<strong>two</strong>&gt;.</p>
<p><em>foo &lt;bar</em> baz&gt;</p>
<p><em>a <b x="*"></em></p>
<p><em>a <b title=">*"></em></p>
<p><em>a <a href="http://x.y/*z">http://x.y/*z</a></em></p>
<p><em>a <!-- * --></em></p>
+13
View File
@@ -0,0 +1,13 @@
Replace <*name*> with your name.
Pick <_one_> or <**two**>.
*foo <bar* baz>
*a <b x="*">*
*a <b title=">*">*
*a <http://x.y/*z>*
*a <!-- * -->*