mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: keep emphasis in the text of a rejected reference link (#4059)
A reference link candidate whose text already holds a link never becomes a link, but the mask em and strong read still flattened the whole span, so every emphasis marker inside it was invisible. Mask the links the text holds instead of the span, and leave images alone, since an image may hold a link. Fixes CommonMark example 533.
This commit is contained in:
+55
-4
@@ -300,6 +300,40 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this link text hold a link already? An image does not count: an image
|
||||
* may hold a link, a link may not.
|
||||
*/
|
||||
private linkInText(text: string): boolean {
|
||||
if (!text.includes('[')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const linkRule = this.tokenizer.rules.inline.link;
|
||||
for (const match of text.matchAll(this.tokenizer.rules.inline.blockSkip)) {
|
||||
// blockSkip also matches code spans and html, and the `!` of an image is
|
||||
// left out of the match, so read the character before it.
|
||||
if (linkRule.test(match[0]) && text.charAt(match.index - 1) !== '!') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (const match of text.matchAll(this.tokenizer.rules.inline.reflinkSearch)) {
|
||||
const match0 = match[0];
|
||||
const refStart = match0.lastIndexOf('[');
|
||||
if (match0.charAt(0) === '!' || !Object.hasOwn(this.tokens.links, match0.slice(refStart + 1, -1))) {
|
||||
continue;
|
||||
}
|
||||
// a candidate holding a link is not a link either, so it does not count
|
||||
if (refStart > 1 && this.linkInText(match0.slice(1, refStart - 1))) {
|
||||
continue;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lexing/Compiling
|
||||
*/
|
||||
@@ -310,10 +344,27 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
|
||||
|
||||
// Mask out reflinks
|
||||
if (this.tokens.links && src.includes('[')) {
|
||||
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.reflinkSearch, match0 =>
|
||||
Object.hasOwn(this.tokens.links, match0.slice(match0.lastIndexOf('[') + 1, -1))
|
||||
? '[' + 'a'.repeat(match0.length - 2) + ']'
|
||||
: match0);
|
||||
const reflinkSearch = this.tokenizer.rules.inline.reflinkSearch;
|
||||
const maskReflink = (match0: string): string => {
|
||||
const refStart = match0.lastIndexOf('[');
|
||||
if (!Object.hasOwn(this.tokens.links, match0.slice(refStart + 1, -1))) {
|
||||
return match0;
|
||||
}
|
||||
// CommonMark: "Links may not contain other links, at any level of
|
||||
// nesting." A candidate whose text already holds one never becomes a
|
||||
// link, so flattening the whole span would hide the emphasis that
|
||||
// does still apply inside it. Mask the links it holds instead.
|
||||
// Images are exempt: their text is flattened into an alt attribute.
|
||||
if (refStart > 1 && match0.charAt(0) !== '!') {
|
||||
const text = match0.slice(1, refStart - 1);
|
||||
if (this.linkInText(text)) {
|
||||
return '[' + text.replace(reflinkSearch, maskReflink)
|
||||
+ '][' + 'a'.repeat(match0.length - refStart - 2) + ']';
|
||||
}
|
||||
}
|
||||
return '[' + 'a'.repeat(match0.length - 2) + ']';
|
||||
};
|
||||
maskedSrc = maskedSrc.replace(reflinkSearch, maskReflink);
|
||||
}
|
||||
|
||||
// Mask out escaped characters.
|
||||
|
||||
@@ -4269,8 +4269,7 @@
|
||||
"example": 533,
|
||||
"start_line": 8053,
|
||||
"end_line": 8059,
|
||||
"section": "Links",
|
||||
"shouldFail": true
|
||||
"section": "Links"
|
||||
},
|
||||
{
|
||||
"markdown": "*[foo*][ref]\n\n[ref]: /uri\n",
|
||||
|
||||
@@ -4269,8 +4269,7 @@
|
||||
"example": 533,
|
||||
"start_line": 8053,
|
||||
"end_line": 8059,
|
||||
"section": "Links",
|
||||
"shouldFail": true
|
||||
"section": "Links"
|
||||
},
|
||||
{
|
||||
"markdown": "*[foo*][ref]\n\n[ref]: /uri\n",
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<p>[foo <em>bar <a href="/url">baz</a> qux</em>]<a href="/uri">ref</a></p>
|
||||
<p>[foo <em>bar <a href="/uri">baz</a> qux</em>]<a href="/uri">ref</a></p>
|
||||
<p><a href="/uri">foo <em>bar <img src="/img.png" alt="baz"> qux</em></a></p>
|
||||
<p>*foo <a href="/uri">bar*</a></p>
|
||||
@@ -0,0 +1,9 @@
|
||||
[foo *bar [baz](/url) qux*][ref]
|
||||
|
||||
[foo _bar [baz][ref] qux_][ref]
|
||||
|
||||
[foo *bar  qux*][ref]
|
||||
|
||||
*foo [bar*][ref]
|
||||
|
||||
[ref]: /uri
|
||||
Reference in New Issue
Block a user