fix: preserve enclosing link state after parsing images (#4123)

* fix: preserve enclosing link state after parsing images

* test: move image link regression cases to spec fixtures

* test: use standard HTML differ for image link regression
This commit is contained in:
snooze26h
2026-10-06 09:00:26 -06:00
committed by GitHub
parent 627d9ae4e5
commit cc07bb9cb6
3 changed files with 26 additions and 2 deletions
+3 -2
View File
@@ -19,14 +19,15 @@ function outputLink(cap: string[], link: Pick<Tokens.Link, 'href' | 'title'>, ra
const text = cap[1].replace(rules.other.outputLinkReplace, '$1');
const isImage = cap[0].charAt(0) === '!';
lexer.state.inLink = true;
const outerInLink = lexer.state.inLink;
const outerLinkEmitted = lexer.state.linkEmitted;
const outerInRawBlock = lexer.state.inRawBlock;
lexer.state.inLink = true;
lexer.state.linkEmitted = false;
const tokens = lexer.inlineTokens(text);
const textHasLink = lexer.state.linkEmitted;
lexer.state.linkEmitted = outerLinkEmitted;
lexer.state.inLink = false;
lexer.state.inLink = outerInLink;
if (!isImage) {
// CommonMark: "Links may not contain other links, at any level of nesting."
@@ -0,0 +1,6 @@
<p><a href="https://target.example"><img src="logo.png" alt="logo"> www.example.com</a></p>
<p><a href="https://target.example"><img src="logo.png" alt="logo"> https://example.com</a></p>
<p><a href="https://target.example"><img src="logo.png" alt="logo"> user@example.com</a></p>
<p><a href="https://target.example"><img src="logo.png" alt="logo"> www.example.com</a></p>
<p><a href="https://target.example"><img src="outer.png" alt="logo"> www.example.com</a></p>
<p><a href="https://target.example"><img src="logo.png" alt="logo"></a> <a href="http://www.example.com">www.example.com</a></p>
@@ -0,0 +1,17 @@
---
gfm: true
---
[![logo](logo.png) www.example.com](https://target.example)
[![logo](logo.png) https://example.com](https://target.example)
[![logo](logo.png) user@example.com](https://target.example)
[![logo][image] www.example.com][target]
[![![logo](inner.png)](outer.png) www.example.com](https://target.example)
[![logo](logo.png)](https://target.example) www.example.com
[image]: logo.png
[target]: https://target.example