Skip to content

fix: keep hard line breaks in image alt text - #4187

Open
mmporong wants to merge 1 commit into
markedjs:masterfrom
mmporong:fix-image-alt-hard-break
Open

mmporong wants to merge 1 commit into
markedjs:masterfrom
mmporong:fix-image-alt-hard-break

Conversation

@mmporong

@mmporong mmporong commented Nov 3, 2026

Copy link
Copy Markdown

Marked version: 18.1.3 (master ba50aa9)

Markdown flavor: CommonMark

Description

  • No existing issue that I could find.

Expectation

A hard line break inside an image description stays a line break in the alt text. commonmark.js 0.31.2 renders ![foo\ + newline + bar](a.png) and ![foo + newline + bar](b.png) with alt="foo\nbar", and marked already keeps a soft line break the same way (![foo + newline + bar](u) gives alt="foo\nbar").

Result

marked gives alt="foobar" for both hard break forms, including reference-style images, so the words run together in the alt text. _TextRenderer.br() returns an empty string, and image alt text is rendered with the text renderer.

Change

_TextRenderer.br() now returns '\n'. Image alt text is the only place marked's own parser uses the text renderer. marked.TextRenderer is also exported, so a renderer built on it now gets a newline for a hard break instead of nothing.

Demo (marked 18.1.3 and the CommonMark reference):

Tests: new spec test/specs/new/image_alt_hard_break (renderExact: true; backslash, two spaces, and a reference-style image) fails on master and passes with this change. With node esbuild.config.js, the spec tests (1985) and unit tests (242) pass, and eslint and tsc --noEmit are clean on the change. I did not run the full npm test (types with attw, UMD/CJS checks, docs build).

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

@vercel

vercel Bot commented Nov 3, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Nov 3, 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 Nov 3, 2026 1:05am UTC

Request Review

This branch was successfully deployed

1 active deployment
Preview — 0287e25c Deployed Nov 3, 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.

1 participant