Skip to content

fix: preserve enclosing link state after parsing images - #4116

Closed
snooze26h wants to merge 3 commits into
markedjs:masterfrom
snooze26h:fix/preserve-link-state-after-image
Closed

snooze26h wants to merge 3 commits into
markedjs:masterfrom
snooze26h:fix/preserve-link-state-after-image

Conversation

@snooze26h

@snooze26h snooze26h commented Sep 29, 2026 •

Copy link
Copy Markdown

Marked version: 18.0.14, master at c61543ed0f16121ace9255626abcafe9514cb1b0

Markdown flavor: GitHub Flavored Markdown

Description

An image inside a link resets the lexer's inLink state to false. A following bare URL or email address is then autolinked inside the enclosing link, producing nested <a> elements and changing the document's clickable destinations.

[![logo](logo.png) www.example.com](https://target.example)

Expected:

<p><a href="https://target.example"><img src="logo.png" alt="logo"> www.example.com</a></p>

Current output:

<p><a href="https://target.example"><img src="logo.png" alt="logo"> <a href="http://www.example.com">www.example.com</a></a></p>

Save and restore the enclosing inLink value around the recursive label tokenization. Add a regression fixture in test/specs/new covering six inputs: bare URLs, email addresses, reference links/images, nested images, and normal autolinking after the enclosing link ends. Five of the inputs produce nested anchors before the fix.

This covers the state restoration left outside the scope of #4051. The trailing-URL test also checks the behavior fixed in #2186.

Validation

npm test passed on Node.js 24.20.0 (macOS arm64), including documentation/build generation, specification tests, unit tests, UMD/CJS checks, TypeScript checks, and lint. Re-run with the standard HTML differ: 1,863 specification tests and 193 unit tests passed. The fixture still fails against the pre-fix tokenizer using the standard HTML differ.

Contributor

  • Tests exist to ensure functionality and minimize regression.
  • No new feature or public API change requiring documentation.

Committer

  • CI is green.
  • Squash and merge following conventional commit guidelines.

@vercel

vercel Bot commented Sep 29, 2026

Copy link
Copy Markdown

@snooze26h is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

Comment thread test/unit/marked.test.js
@vercel

vercel Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
marked-website Ready Ready Preview Sep 30, 2026 4:43pm UTC

Request Review

Comment thread test/specs/new/autolink_after_image_in_link.md Outdated

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Thanks for helping improve marked 💯

@snooze26h snooze26h closed this by deleting the head repository Oct 2, 2026
@UziTech

UziTech commented Oct 2, 2026

Copy link
Copy Markdown
Member

@snooze26h did you mean to close this PR before it was merged?

This branch was successfully deployed

1 active deployment
Preview — 00073f9e Deployed Sep 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants