Skip to content

Markdown image alt and title attributes double-escape ampersands #18070

Closed

Description

@OlaoluwaM

Astro Info

Astro                    v7.2.9
Vite                     v8.2.2
Node                     v24.19.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

Not browser-specific; present in generated HTML.

Describe the Bug

With Satteri (satteri: 0.10.5 & @astrojs/markdown-satteri: 0.3.8) and a local image, an ampersand in Markdown image alt or title text is escaped twice. This changes the browser's attribute value.

Input:

![A & B](../assets/diagram.svg "C & D")

Output (other attributes omitted):

<img alt="A &amp;amp; B" title="C &amp;amp; D">

The DOM attribute values are "A & B" and "C & D".

Reproduced on Astro 7.2.9 in both:

  • A glob() content collection rendered with getEntry(), render(), and <Content />.
  • An ordinary Markdown page under src/pages/.

Steps for the reproduction:

  1. Run npm install.
  2. Run npm run build (astro build --force).
  3. Inspect dist/index.html and dist/direct/index.html. Both contain the double-escaped attributes.

The reproduction uses the Satteri processor and a small local SVG, without custom plugins or dependency patches. The build exits 0.

What's the expected result?

The DOM alt should be "A & B" and title should be "C & D". Generated HTML may use & or an equivalent single character reference. Literal entity text must be preserved, and final HTML attribute escaping must remain enabled.

Link to Minimal Reproducible Example

https://github.com/OlaoluwaM/digitalgarden-blog/tree/astro-rewrite

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. added
    triage: in progress
    on Sep 18, 2026
  2. astro-factory commented on Sep 18, 2026

    @astro-factory
    Contributor

    Triage complete

    • Prepare the workspace: ready
    • Install dependencies: complete
    • Build the project: complete
    • Reproduce the issue: confirmed
    • Diagnose the cause: high confidence
    • Verify the diagnosis: bug
    • Attempt a fix: verified
    • Publish the result: set triage: fix pending

    • Reproduced: Yes — confirmed on both direct Markdown pages and content collection pages on Astro 7.3.3 (current main).
    • Exploration: Yes — View branch
    • Unit Test: Yes — packages/astro/test/units/vite-plugin-markdown/images.test.ts (7 test cases)
    • Priority: P4: important. This affects any Markdown image whose alt or title contains an & — a common character. It corrupts the rendered HTML output with the default Satteri processor, producing visibly wrong text in the browser (e.g. "A & B" instead of "A & B").

    When Satteri serializes the __ASTRO_IMAGE_ marker attribute to HTML, it encodes & as &amp;, " as &quot;, and ' as &apos;. The image decode logic in runtime.ts and vite-plugin-markdown/images.ts reverses &quot;/&#x22; and &apos;/&#x27; before JSON.parse, but does not decode &amp;/&#x26;. The un-decoded &amp; becomes literal text in the parsed alt/title strings, and when the final <img> tag is rendered, the & in &amp; gets re-escaped to &amp;amp;.

    I found a potential fix for this issue. The fix adds .replace(/&(?:#x26|amp);/g, '&') to the end of each decode chain in all 4 affected locations. The &amp; decode is placed last to prevent double-decoding (e.g. &amp;quot; correctly becomes the literal text &quot;, not a quotation mark).

    Try this fix

    You can test this fix right now without waiting for a release:

    # astro
    npm i https://pkg.pr.new/astro@d6f2607

    If this fixes your issue, please leave a comment letting us know (for example
    "confirmed, this fixes it"). A pull request will then be opened to get it merged.

    Full Triage Report

    Reproduction

    Created a minimal project with:

    • A Markdown page at src/pages/direct/index.md containing ![A & B](../../assets/diagram.svg "C & D")
    • A content collection post with the same image syntax, rendered via getEntry() / render() / <Content />

    Both paths produced alt="A &amp;amp; B" title="C &amp;amp; D" in the generated HTML.

    Root Cause

    The __ASTRO_IMAGE_ attribute contains JSON-encoded image properties. Satteri's HTML serializer encodes the attribute value, turning & → &amp;, " → &quot;, ' → &apos;. The decode logic reverses quotes and apostrophes but not ampersands:

    // Before (incomplete — missing &amp; decode)
    JSON.parse(imagePath.replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'"))

    After JSON.parse, the alt/title values contain literal &amp; text, which gets re-escaped to &amp;amp; during final HTML rendering.

    Affected Files

    1. packages/astro/src/content/runtime.ts line 482 (content collections path)
    2. packages/astro/src/vite-plugin-markdown/images.ts lines 26, 45, 60 (direct Markdown page path)

    Fix Details

    Added .replace(/&(?:#x26|amp);/g, '&') to the end of each decode chain. The ordering is critical — &amp; must be decoded last so that &amp;quot; becomes the literal text &quot; rather than a double-decoded quotation mark.

    Verification

    • Before fix: alt="A &amp;amp; B" / title="C &amp;amp; D" (double-escaped)
    • After fix: alt="A &amp; B" / title="C &amp; D" (correct)

    All existing tests pass (image-references, markdown-rendering, asset-imports, 188 asset tests).

    Unit Tests Added

    packages/astro/test/units/vite-plugin-markdown/images.test.ts — 7 tests covering:

    • Named entity decoding (&amp; from Satteri)
    • Numeric entity decoding (&#x26; from remark)
    • Literal entity text preservation (&amp;quot; → &quot;, not ")
    • Quotes, apostrophes, plain text, and multiple ampersands

    Changeset

    .changeset/cool-paws-trade.md — astro: patch

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.

  3. added
    - P4: important
    pkg: astro
    triage: fix pending
    and removed
    triage: in progress
    on Sep 18, 2026
  4. self-assigned this
    on Sep 20, 2026
  5. Princesseuh commented on Sep 21, 2026

    @Princesseuh
    Member

    Fixed by #18086

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

- P4: importantpkg: astrotriage: fix pending

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions