mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
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:
co-authored by
Cursor Agent
Rahul Kashyap
parent
9470e8f719
commit
9cb84e07d1
@@ -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
@@ -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)\]/)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<p>[foo](/url)</p>
|
||||
<p></p>
|
||||
<p>[a](b "t")</p>
|
||||
<p>[a](b 't')</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 "t"</p>
|
||||
<p>[foo]</p>
|
||||
<p><a href="/url" title="t)">bar</a></p>
|
||||
<p>[baz]: /url 't'</p>
|
||||
<p>[baz]</p>
|
||||
@@ -0,0 +1,29 @@
|
||||
[foo](/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]
|
||||
Reference in New Issue
Block a user