Skip to content

fix(mermaid): inject xmlns:xlink namespace if missing before PNG export#542

Open
sleitor wants to merge 1 commit into
vercel:mainfrom
sleitor:fix-541
Open

fix(mermaid): inject xmlns:xlink namespace if missing before PNG export#542
sleitor wants to merge 1 commit into
vercel:mainfrom
sleitor:fix-541

Conversation

@sleitor

@sleitor sleitor commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Problem

Mermaid can emit SVG containing xlink-prefixed attributes (e.g. xlink:href in C4Context and sequence diagrams) without declaring the xmlns:xlink namespace on the root <svg> element. The resulting SVG is not valid XML, so browsers refuse to load it as an image, causing PNG export to fail silently.

Minimal reproduction (C4Context diagram):

C4Context
  title Generic system context
  Person(user, "User", "Uses digital services")
  System(application, "Application", "Web and mobile service platform")
Loading

Fix

Add addXlinkNamespaceIfMissing() in packages/streamdown/lib/mermaid/utils.ts which:

  1. Checks if the SVG string contains xlink: prefixed attributes
  2. Checks if xmlns:xlink namespace is already declared
  3. If the namespace is missing, injects xmlns:xlink="http://www.w3.org/1999/xlink" onto the root <svg> tag via a lightweight string replacement

svgToPngBlob() now normalises the input before base64-encoding it as a data URI, following the same pattern as other Mermaid SVG normalisation fixes (see #516).

Testing

Added 6 new unit tests covering:

  • Namespace injection when missing
  • No modification when namespace already present
  • No modification when no xlink: attributes
  • Only root <svg> tag is modified (not nested)
  • Various xlink attribute types (xlink:href, xlink:actuate, xlink:title)
  • Integration test verifying svgToPngBlob encodes the fixed SVG

Fixes #541

Mermaid can emit SVG containing xlink-prefixed attributes (e.g.
xlink:href in C4Context and sequence diagrams) without declaring the
xmlns:xlink namespace on the root <svg> element.  The resulting SVG
is not valid XML, so browsers refuse to load it as an image, causing
PNG export to fail silently.

Add addXlinkNamespaceIfMissing() which detects the missing declaration
via lightweight string checks and injects it onto the root <svg> tag.
svgToPngBlob() now normalises the input before base64-encoding it as a
data URI, matching the existing pattern for other Mermaid SVG
normalisation fixes (see vercel#516).

Fixes vercel#541
@vercel

vercel Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

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

A member of the Team first needs to authorize it.

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.

Mermaid PNG export fails for SVGs with xlink:href because generated SVG is not XML-valid

1 participant