mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: Fix bracket escapes inside code spans in link text being stripped (#4205)
* Fix bracket escapes inside code spans in link text being stripped The link label matcher consumes backslash-escaped brackets, and outputLink then removes those escapes from the label text. It did so over the whole label, so a backslash-escaped bracket inside a code span also lost its backslash even though code span content is literal: [`\]`] rendered <code>]</code> instead of <code>\]</code>. Scan the label and copy code spans through verbatim, unescaping brackets only outside of them. Refs #4176 Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> * Add spec tests for bracket escapes in link text code spans Refs #4176 Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> * Add expected HTML for bracket escapes in link text code spans Refs #4176 Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --------- Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
This commit is contained in:
co-authored by
generaltony007
parent
ac2256e113
commit
9a26ae9cee
+27
-1
@@ -15,10 +15,36 @@ import type { _Lexer } from './Lexer.ts';
|
||||
import type { Links, Tokens, Token } from './Tokens.ts';
|
||||
import type { MarkedOptions } from './MarkedOptions.ts';
|
||||
|
||||
/**
|
||||
* Remove the bracket escapes that the label match consumed, but leave code
|
||||
* spans untouched: their content is literal, so a `\[` or `\]` inside a
|
||||
* code span is not an escape.
|
||||
*/
|
||||
function unescapeLinkLabel(label: string, rules: Rules): string {
|
||||
let result = '';
|
||||
for (let i = 0; i < label.length; i++) {
|
||||
if (label[i] === '`') {
|
||||
const code = rules.inline.code.exec(label.slice(i));
|
||||
if (code) {
|
||||
result += code[0];
|
||||
i += code[0].length - 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (label[i] === '\\' && (label[i + 1] === '[' || label[i + 1] === ']')) {
|
||||
result += label[i + 1];
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
result += label[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function outputLink(cap: string[], link: Pick<Tokens.Link, 'href' | 'title'>, raw: string, lexer: _Lexer, rules: Rules): Tokens.Link | Tokens.Image | undefined {
|
||||
const href = link.href;
|
||||
const title = link.title || null;
|
||||
const text = cap[1].replace(rules.other.outputLinkReplace, '$1');
|
||||
const text = unescapeLinkLabel(cap[1], rules);
|
||||
const isImage = cap[0].charAt(0) === '!';
|
||||
|
||||
const outerInLink = lexer.state.inLink;
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<p><a href="https://x.test/"><code>\]</code></a></p>
|
||||
<p><a href="https://x.test/"><code>\[</code></a></p>
|
||||
<p><img src="https://img.test/" alt="\]alt"></p>
|
||||
@@ -0,0 +1,5 @@
|
||||
[`\]`](https://x.test/)
|
||||
|
||||
[`\[`](https://x.test/)
|
||||
|
||||
![`\]`alt](https://img.test/)
|
||||
Reference in New Issue
Block a user