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;
|
||||
}
|
||||
|
||||
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);
|
||||
if (cleanHref === null) {
|
||||
return escape(text);
|
||||
|
@ -14,26 +14,17 @@ function outputLink(cap: string[], link: Pick<Tokens.Link, 'href' | 'title'>, ra
|
||||
const title = link.title || null;
|
||||
const text = cap[1].replace(rules.other.outputLinkReplace, '$1');
|
||||
|
||||
if (cap[0].charAt(0) !== '!') {
|
||||
lexer.state.inLink = true;
|
||||
const token: Tokens.Link = {
|
||||
type: 'link',
|
||||
raw,
|
||||
href,
|
||||
title,
|
||||
text,
|
||||
tokens: lexer.inlineTokens(text),
|
||||
};
|
||||
lexer.state.inLink = false;
|
||||
return token;
|
||||
}
|
||||
return {
|
||||
type: 'image',
|
||||
lexer.state.inLink = true;
|
||||
const token: Tokens.Link | Tokens.Image = {
|
||||
type: cap[0].charAt(0) === '!' ? 'image' : 'link',
|
||||
raw,
|
||||
href,
|
||||
title,
|
||||
text,
|
||||
tokens: lexer.inlineTokens(text),
|
||||
};
|
||||
lexer.state.inLink = false;
|
||||
return token;
|
||||
}
|
||||
|
||||
function indentCodeCompensation(raw: string, text: string, rules: Rules) {
|
||||
|
@ -123,6 +123,7 @@ export namespace Tokens {
|
||||
href: string;
|
||||
title: string | null;
|
||||
text: string;
|
||||
tokens: Token[];
|
||||
}
|
||||
|
||||
export interface Link {
|
||||
|
@ -4596,8 +4596,7 @@
|
||||
"example": 573,
|
||||
"start_line": 8548,
|
||||
"end_line": 8554,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "](/url2)\n",
|
||||
@ -4605,8 +4604,7 @@
|
||||
"example": 574,
|
||||
"start_line": 8557,
|
||||
"end_line": 8561,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "](/url2)\n",
|
||||
@ -4614,8 +4612,7 @@
|
||||
"example": 575,
|
||||
"start_line": 8564,
|
||||
"end_line": 8568,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "![foo *bar*][]\n\n[foo *bar*]: train.jpg \"train & tracks\"\n",
|
||||
@ -4623,8 +4620,7 @@
|
||||
"example": 576,
|
||||
"start_line": 8578,
|
||||
"end_line": 8584,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "![foo *bar*][foobar]\n\n[FOOBAR]: train.jpg \"train & tracks\"\n",
|
||||
@ -4632,8 +4628,7 @@
|
||||
"example": 577,
|
||||
"start_line": 8587,
|
||||
"end_line": 8593,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "\n",
|
||||
@ -4697,8 +4692,7 @@
|
||||
"example": 585,
|
||||
"start_line": 8655,
|
||||
"end_line": 8661,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "![Foo][]\n\n[foo]: /url \"title\"\n",
|
||||
@ -4730,8 +4724,7 @@
|
||||
"example": 589,
|
||||
"start_line": 8700,
|
||||
"end_line": 8706,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "![[foo]]\n\n[[foo]]: /url \"title\"\n",
|
||||
|
@ -4596,8 +4596,7 @@
|
||||
"example": 573,
|
||||
"start_line": 8548,
|
||||
"end_line": 8554,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "](/url2)\n",
|
||||
@ -4605,8 +4604,7 @@
|
||||
"example": 574,
|
||||
"start_line": 8557,
|
||||
"end_line": 8561,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "](/url2)\n",
|
||||
@ -4614,8 +4612,7 @@
|
||||
"example": 575,
|
||||
"start_line": 8564,
|
||||
"end_line": 8568,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "![foo *bar*][]\n\n[foo *bar*]: train.jpg \"train & tracks\"\n",
|
||||
@ -4623,8 +4620,7 @@
|
||||
"example": 576,
|
||||
"start_line": 8578,
|
||||
"end_line": 8584,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "![foo *bar*][foobar]\n\n[FOOBAR]: train.jpg \"train & tracks\"\n",
|
||||
@ -4632,8 +4628,7 @@
|
||||
"example": 577,
|
||||
"start_line": 8587,
|
||||
"end_line": 8593,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "\n",
|
||||
@ -4697,8 +4692,7 @@
|
||||
"example": 585,
|
||||
"start_line": 8655,
|
||||
"end_line": 8661,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"markdown": "![Foo][]\n\n[foo]: /url \"title\"\n",
|
||||
@ -4730,8 +4724,7 @@
|
||||
"example": 589,
|
||||
"start_line": 8700,
|
||||
"end_line": 8706,
|
||||
"section": "Images",
|
||||
"shouldFail": true
|
||||
"section": "Images"
|
||||
},
|
||||
{
|
||||
"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 });
|
||||
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 });
|
||||
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;
|
||||
|
@ -1340,6 +1340,12 @@ paragraph
|
||||
text: 'image',
|
||||
href: 'https://example.com/image.png',
|
||||
title: null,
|
||||
tokens: [{
|
||||
type: 'text',
|
||||
raw: 'image',
|
||||
text: 'image',
|
||||
escaped: false,
|
||||
}],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -1355,6 +1361,12 @@ paragraph
|
||||
text: 'image',
|
||||
href: 'https://example.com/image.png',
|
||||
title: 'title',
|
||||
tokens: [{
|
||||
type: 'text',
|
||||
raw: 'image',
|
||||
text: 'image',
|
||||
escaped: false,
|
||||
}],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
@ -396,6 +396,12 @@ describe('Parser', () => {
|
||||
text: 'image',
|
||||
href: 'image.png',
|
||||
title: 'title',
|
||||
tokens: [{
|
||||
type: 'text',
|
||||
raw: 'image',
|
||||
text: 'image',
|
||||
escaped: false,
|
||||
}],
|
||||
},
|
||||
],
|
||||
html: '<img src="image.png" alt="image" title="title">',
|
||||
|
@ -959,6 +959,7 @@ br
|
||||
['space', ''],
|
||||
['paragraph', ''],
|
||||
['image', ''],
|
||||
['text', 'image'],
|
||||
['space', ''],
|
||||
['paragraph', '**strong**'],
|
||||
['strong', '**strong**'],
|
||||
|
Loading…
x
Reference in New Issue
Block a user