fix: fix image alt text rendered to match common mark (#3668)

This commit is contained in:
Tony Brix 2025-04-24 18:41:02 -06:00 committed by GitHub
parent d5bc76eb3c
commit 2c0e47a4ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 45 additions and 45 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -123,6 +123,7 @@ export namespace Tokens {
href: string;
title: string | null;
text: string;
tokens: Token[];
}
export interface Link {

View File

@ -4596,8 +4596,7 @@
"example": 573,
"start_line": 8548,
"end_line": 8554,
"section": "Images",
"shouldFail": true
"section": "Images"
},
{
"markdown": "![foo ![bar](/url)](/url2)\n",
@ -4605,8 +4604,7 @@
"example": 574,
"start_line": 8557,
"end_line": 8561,
"section": "Images",
"shouldFail": true
"section": "Images"
},
{
"markdown": "![foo [bar](/url)](/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": "![foo](train.jpg)\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",

View File

@ -4596,8 +4596,7 @@
"example": 573,
"start_line": 8548,
"end_line": 8554,
"section": "Images",
"shouldFail": true
"section": "Images"
},
{
"markdown": "![foo ![bar](/url)](/url2)\n",
@ -4605,8 +4604,7 @@
"example": 574,
"start_line": 8557,
"end_line": 8561,
"section": "Images",
"shouldFail": true
"section": "Images"
},
{
"markdown": "![foo [bar](/url)](/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": "![foo](train.jpg)\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",

View File

@ -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;

View File

@ -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,
}],
},
],
});

View File

@ -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">',

View File

@ -959,6 +959,7 @@ br
['space', ''],
['paragraph', '![image](https://example.com/image.jpg)'],
['image', '![image](https://example.com/image.jpg)'],
['text', 'image'],
['space', ''],
['paragraph', '**strong**'],
['strong', '**strong**'],