fix: fix image alt text rendered to match common mark (#3668)
This commit is contained in:
parent
d5bc76eb3c
commit
2c0e47a4ce
@ -193,7 +193,10 @@ export class _Renderer {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
image({ href, title, text }: Tokens.Image): string {
|
image({ href, title, text, tokens }: Tokens.Image): string {
|
||||||
|
if (tokens) {
|
||||||
|
text = this.parser.parseInline(tokens, this.parser.textRenderer);
|
||||||
|
}
|
||||||
const cleanHref = cleanUrl(href);
|
const cleanHref = cleanUrl(href);
|
||||||
if (cleanHref === null) {
|
if (cleanHref === null) {
|
||||||
return escape(text);
|
return escape(text);
|
||||||
|
@ -14,26 +14,17 @@ function outputLink(cap: string[], link: Pick<Tokens.Link, 'href' | 'title'>, ra
|
|||||||
const title = link.title || null;
|
const title = link.title || null;
|
||||||
const text = cap[1].replace(rules.other.outputLinkReplace, '$1');
|
const text = cap[1].replace(rules.other.outputLinkReplace, '$1');
|
||||||
|
|
||||||
if (cap[0].charAt(0) !== '!') {
|
lexer.state.inLink = true;
|
||||||
lexer.state.inLink = true;
|
const token: Tokens.Link | Tokens.Image = {
|
||||||
const token: Tokens.Link = {
|
type: cap[0].charAt(0) === '!' ? 'image' : 'link',
|
||||||
type: 'link',
|
|
||||||
raw,
|
|
||||||
href,
|
|
||||||
title,
|
|
||||||
text,
|
|
||||||
tokens: lexer.inlineTokens(text),
|
|
||||||
};
|
|
||||||
lexer.state.inLink = false;
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
type: 'image',
|
|
||||||
raw,
|
raw,
|
||||||
href,
|
href,
|
||||||
title,
|
title,
|
||||||
text,
|
text,
|
||||||
|
tokens: lexer.inlineTokens(text),
|
||||||
};
|
};
|
||||||
|
lexer.state.inLink = false;
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
function indentCodeCompensation(raw: string, text: string, rules: Rules) {
|
function indentCodeCompensation(raw: string, text: string, rules: Rules) {
|
||||||
|
@ -123,6 +123,7 @@ export namespace Tokens {
|
|||||||
href: string;
|
href: string;
|
||||||
title: string | null;
|
title: string | null;
|
||||||
text: string;
|
text: string;
|
||||||
|
tokens: Token[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Link {
|
export interface Link {
|
||||||
|
@ -4596,8 +4596,7 @@
|
|||||||
"example": 573,
|
"example": 573,
|
||||||
"start_line": 8548,
|
"start_line": 8548,
|
||||||
"end_line": 8554,
|
"end_line": 8554,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "](/url2)\n",
|
"markdown": "](/url2)\n",
|
||||||
@ -4605,8 +4604,7 @@
|
|||||||
"example": 574,
|
"example": 574,
|
||||||
"start_line": 8557,
|
"start_line": 8557,
|
||||||
"end_line": 8561,
|
"end_line": 8561,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "](/url2)\n",
|
"markdown": "](/url2)\n",
|
||||||
@ -4614,8 +4612,7 @@
|
|||||||
"example": 575,
|
"example": 575,
|
||||||
"start_line": 8564,
|
"start_line": 8564,
|
||||||
"end_line": 8568,
|
"end_line": 8568,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![foo *bar*][]\n\n[foo *bar*]: train.jpg \"train & tracks\"\n",
|
"markdown": "![foo *bar*][]\n\n[foo *bar*]: train.jpg \"train & tracks\"\n",
|
||||||
@ -4623,8 +4620,7 @@
|
|||||||
"example": 576,
|
"example": 576,
|
||||||
"start_line": 8578,
|
"start_line": 8578,
|
||||||
"end_line": 8584,
|
"end_line": 8584,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![foo *bar*][foobar]\n\n[FOOBAR]: train.jpg \"train & tracks\"\n",
|
"markdown": "![foo *bar*][foobar]\n\n[FOOBAR]: train.jpg \"train & tracks\"\n",
|
||||||
@ -4632,8 +4628,7 @@
|
|||||||
"example": 577,
|
"example": 577,
|
||||||
"start_line": 8587,
|
"start_line": 8587,
|
||||||
"end_line": 8593,
|
"end_line": 8593,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "\n",
|
"markdown": "\n",
|
||||||
@ -4697,8 +4692,7 @@
|
|||||||
"example": 585,
|
"example": 585,
|
||||||
"start_line": 8655,
|
"start_line": 8655,
|
||||||
"end_line": 8661,
|
"end_line": 8661,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![Foo][]\n\n[foo]: /url \"title\"\n",
|
"markdown": "![Foo][]\n\n[foo]: /url \"title\"\n",
|
||||||
@ -4730,8 +4724,7 @@
|
|||||||
"example": 589,
|
"example": 589,
|
||||||
"start_line": 8700,
|
"start_line": 8700,
|
||||||
"end_line": 8706,
|
"end_line": 8706,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![[foo]]\n\n[[foo]]: /url \"title\"\n",
|
"markdown": "![[foo]]\n\n[[foo]]: /url \"title\"\n",
|
||||||
|
@ -4596,8 +4596,7 @@
|
|||||||
"example": 573,
|
"example": 573,
|
||||||
"start_line": 8548,
|
"start_line": 8548,
|
||||||
"end_line": 8554,
|
"end_line": 8554,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "](/url2)\n",
|
"markdown": "](/url2)\n",
|
||||||
@ -4605,8 +4604,7 @@
|
|||||||
"example": 574,
|
"example": 574,
|
||||||
"start_line": 8557,
|
"start_line": 8557,
|
||||||
"end_line": 8561,
|
"end_line": 8561,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "](/url2)\n",
|
"markdown": "](/url2)\n",
|
||||||
@ -4614,8 +4612,7 @@
|
|||||||
"example": 575,
|
"example": 575,
|
||||||
"start_line": 8564,
|
"start_line": 8564,
|
||||||
"end_line": 8568,
|
"end_line": 8568,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![foo *bar*][]\n\n[foo *bar*]: train.jpg \"train & tracks\"\n",
|
"markdown": "![foo *bar*][]\n\n[foo *bar*]: train.jpg \"train & tracks\"\n",
|
||||||
@ -4623,8 +4620,7 @@
|
|||||||
"example": 576,
|
"example": 576,
|
||||||
"start_line": 8578,
|
"start_line": 8578,
|
||||||
"end_line": 8584,
|
"end_line": 8584,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![foo *bar*][foobar]\n\n[FOOBAR]: train.jpg \"train & tracks\"\n",
|
"markdown": "![foo *bar*][foobar]\n\n[FOOBAR]: train.jpg \"train & tracks\"\n",
|
||||||
@ -4632,8 +4628,7 @@
|
|||||||
"example": 577,
|
"example": 577,
|
||||||
"start_line": 8587,
|
"start_line": 8587,
|
||||||
"end_line": 8593,
|
"end_line": 8593,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "\n",
|
"markdown": "\n",
|
||||||
@ -4697,8 +4692,7 @@
|
|||||||
"example": 585,
|
"example": 585,
|
||||||
"start_line": 8655,
|
"start_line": 8655,
|
||||||
"end_line": 8661,
|
"end_line": 8661,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![Foo][]\n\n[foo]: /url \"title\"\n",
|
"markdown": "![Foo][]\n\n[foo]: /url \"title\"\n",
|
||||||
@ -4730,8 +4724,7 @@
|
|||||||
"example": 589,
|
"example": 589,
|
||||||
"start_line": 8700,
|
"start_line": 8700,
|
||||||
"end_line": 8706,
|
"end_line": 8706,
|
||||||
"section": "Images",
|
"section": "Images"
|
||||||
"shouldFail": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"markdown": "![[foo]]\n\n[[foo]]: /url \"title\"\n",
|
"markdown": "![[foo]]\n\n[[foo]]: /url \"title\"\n",
|
||||||
|
@ -104,7 +104,7 @@ class ExtendedRenderer extends marked.Renderer {
|
|||||||
br = ({ type, raw }: Tokens.Br): string => super.br({ type, raw });
|
br = ({ type, raw }: Tokens.Br): string => super.br({ type, raw });
|
||||||
del = ({ type, raw, text, tokens }: Tokens.Del): string => super.del({ type, raw, text, tokens });
|
del = ({ type, raw, text, tokens }: Tokens.Del): string => super.del({ type, raw, text, tokens });
|
||||||
link = ({ type, raw, href, title, text, tokens }: Tokens.Link): string => super.link({ type, raw, href, title, text, tokens });
|
link = ({ type, raw, href, title, text, tokens }: Tokens.Link): string => super.link({ type, raw, href, title, text, tokens });
|
||||||
image = ({ type, raw, href, title, text }: Tokens.Image): string => super.image({ type, raw, href, title, text });
|
image = ({ type, raw, href, title, text }: Tokens.Image): string => super.image({ type, raw, href, title, text, tokens });
|
||||||
}
|
}
|
||||||
|
|
||||||
const rendererOptions: MarkedOptions = renderer.options;
|
const rendererOptions: MarkedOptions = renderer.options;
|
||||||
|
@ -1340,6 +1340,12 @@ paragraph
|
|||||||
text: 'image',
|
text: 'image',
|
||||||
href: 'https://example.com/image.png',
|
href: 'https://example.com/image.png',
|
||||||
title: null,
|
title: null,
|
||||||
|
tokens: [{
|
||||||
|
type: 'text',
|
||||||
|
raw: 'image',
|
||||||
|
text: 'image',
|
||||||
|
escaped: false,
|
||||||
|
}],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@ -1355,6 +1361,12 @@ paragraph
|
|||||||
text: 'image',
|
text: 'image',
|
||||||
href: 'https://example.com/image.png',
|
href: 'https://example.com/image.png',
|
||||||
title: 'title',
|
title: 'title',
|
||||||
|
tokens: [{
|
||||||
|
type: 'text',
|
||||||
|
raw: 'image',
|
||||||
|
text: 'image',
|
||||||
|
escaped: false,
|
||||||
|
}],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
@ -396,6 +396,12 @@ describe('Parser', () => {
|
|||||||
text: 'image',
|
text: 'image',
|
||||||
href: 'image.png',
|
href: 'image.png',
|
||||||
title: 'title',
|
title: 'title',
|
||||||
|
tokens: [{
|
||||||
|
type: 'text',
|
||||||
|
raw: 'image',
|
||||||
|
text: 'image',
|
||||||
|
escaped: false,
|
||||||
|
}],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
html: '<img src="image.png" alt="image" title="title">',
|
html: '<img src="image.png" alt="image" title="title">',
|
||||||
|
@ -959,6 +959,7 @@ br
|
|||||||
['space', ''],
|
['space', ''],
|
||||||
['paragraph', ''],
|
['paragraph', ''],
|
||||||
['image', ''],
|
['image', ''],
|
||||||
|
['text', 'image'],
|
||||||
['space', ''],
|
['space', ''],
|
||||||
['paragraph', '**strong**'],
|
['paragraph', '**strong**'],
|
||||||
['strong', '**strong**'],
|
['strong', '**strong**'],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user