fix: don't treat an escaped ) or quote as an inline link or title closer (#4151)

fix: do not close a link on an escaped delimiter

An escaped parenthesis or quote was accepted as the end of an inline link
or link title, so `[foo](/url\)` became a link. The closer now has to be
unescaped, matching CommonMark.

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Rahul Kashyap <itisrmk@users.noreply.github.com>
This commit is contained in:
Rahul Kashyap
2026-11-02 07:51:19 -07:00
committed by GitHub
co-authored by Cursor Agent Rahul Kashyap
parent 9470e8f719
commit 9cb84e07d1
4 changed files with 55 additions and 2 deletions
+6
View File
@@ -812,6 +812,12 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
cap[3] = '';
}
}
// The ')' that ends the match is the link closer only when it is not
// escaped. `[foo](/url\)` has no closer; that paren is destination text.
const unescaped = rtrim(cap[0].slice(0, -1), '\\');
if ((cap[0].length - unescaped.length) % 2 === 0) {
return;
}
let href = cap[2];
let title = '';
if (this.options.pedantic) {
+6 -2
View File
@@ -145,7 +145,10 @@ const blockText = /^[^\n]+/;
const _blockLabel = /(?!\s*\])(?:\\[\s\S]|[^\[\]\\])+/;
const def = edit(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n[ \t]*)?| *\n[ \t]*)(title))? *(?:\n+|$)/)
.replace('label', _blockLabel)
.replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/)
// An escaped delimiter is part of the title, so a backslash must consume the
// next character. An optional closer (`\"?`) lets that character backtrack
// into the closing quote (`[foo]: /url "t\"` is not a title).
.replace('title', /(?:"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\\n]|\n(?!\n))*'|\((?:\\[\s\S]|[^()\\])*\))/)
.getRegex();
const list = edit(/^(bull)([ \t][^\n]*?)?(?:\n|$)/)
@@ -475,7 +478,8 @@ const _inlineLabel = edit(/(?:\[(?:brackets|\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(?
const link = edit(/^!?\[(label)\]\([ \t\n]*(href)(?:(?:[ \t]+(?:\n[ \t]*)?|\n[ \t]*)(title))?[ \t\n]*\)/)
.replace('label', _inlineLabel)
.replace('href', /<(?:\\.|[^\n<>\\])+>|[^ \t\n\x00-\x1f]+|(?=\))/)
.replace('title', /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/)
// Same as the definition title: the escaped character is not the closer.
.replace('title', /"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|\((?:\\[\s\S]|[^)\\])*\)/)
.getRegex();
const reflink = edit(/^!?\[(label)\]\[(ref)\]/)
+14
View File
@@ -0,0 +1,14 @@
<p>[foo](/url)</p>
<p>![img](/url)</p>
<p>[a](b &quot;t&quot;)</p>
<p>[a](b &#39;t&#39;)</p>
<p>[a](b (title))</p>
<p><a href="b" title="ti)tle">a</a></p>
<p><a href="foo):">link</a></p>
<p><a href="/url%5C">foo</a></p>
<p><a href="foo">link</a>)</p>
<p>[foo]: /url &quot;t&quot;</p>
<p>[foo]</p>
<p><a href="/url" title="t)">bar</a></p>
<p>[baz]: /url &#39;t&#39;</p>
<p>[baz]</p>
+29
View File
@@ -0,0 +1,29 @@
[foo](/url\)
![img](/url\)
[a](b "t\")
[a](b 't\')
[a](b (title\))
[a](b (ti\)tle))
[link](foo\)\:)
[foo](/url\\)
[link](foo)\)
[foo]: /url "t\"
[foo]
[bar]: /url (t\))
[bar]
[baz]: /url 't\'
[baz]