Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-image-attr-ampersand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix double-escaped ampersands in Markdown image `alt` and `title` attributes. The `__ASTRO_IMAGE_` round-trip now decodes the numeric (`&`) and named (`&`) character references the Markdown processors emit, so an `&` in an alt or title is escaped exactly once in the final HTML instead of twice.
8 changes: 5 additions & 3 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,12 @@ async function updateImageReferencesInBody(html: string, fileName: string) {
for (const [_full, imagePath] of html.matchAll(CONTENT_LAYER_IMAGE_REGEX)) {
try {
// Markdown processors disagree on which character references to emit when
// serialising attribute values: remark uses the numeric forms (`"` / `'`),
// satteri uses the named forms (`"` / `'`). Decode both before JSON.parse.
// serialising attribute values: remark uses the numeric forms (`"` / `'` / `&`),
// satteri uses the named forms (`"` / `'` / `&`). Decode both styles of
// each before JSON.parse, ampersands last — see the same chain in
// `vite-plugin-markdown/images.ts` for why the order matters.
const decodedImagePath = JSON.parse(
imagePath.replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'"),
imagePath.replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'").replace(/&(?:amp|#x26|#38);/g, '&'),
);

let image: GetImageResult;
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/vite-plugin-markdown/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getMarkdownCodeForImages(
)} + '[^"]*)"', 'g');
let match;
while ((match = regex.exec(html)) !== null) {
const imageProps = JSON.parse(match[1].replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'"));
const imageProps = JSON.parse(match[1].replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'").replace(/&(?:amp|#x26|#38);/g, '&'));
const { src, ...props } = imageProps;
// Key on the decoded src so it lines up with the lookup in updateImageReferences,
// which JSON-parses the attribute too (so its key uses the decoded path).
Expand All @@ -42,7 +42,7 @@ export function getMarkdownCodeForImages(
)} + '[^"]*)"', 'g');
let match;
while ((match = regex.exec(html)) !== null) {
const props = JSON.parse(match[1].replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'"));
const props = JSON.parse(match[1].replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'").replace(/&(?:amp|#x26|#38);/g, '&'));
imageSources[props.src + '_' + props.index] = await getImage(props);
}
}`;
Expand All @@ -57,7 +57,7 @@ export function getMarkdownCodeForImages(
return html.replaceAll(/__ASTRO_IMAGE_="([^"]+)"/gm, (full, imagePath) => {
// Markdown processors disagree on character-reference style — remark emits
// \`"\`/\`'\`, satteri emits \`"\`/\`'\`. Decode both before JSON.parse.
const decodedImagePath = JSON.parse(imagePath.replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'"));
const decodedImagePath = JSON.parse(imagePath.replace(/&(?:#x22|quot);/g, '"').replace(/&(?:#x27|apos);/g, "'").replace(/&(?:amp|#x26|#38);/g, '&'));

// Use the 'index' property for each image occurrence
const srcKey = decodedImagePath.src + '_' + decodedImagePath.index;
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/core-image-remark-imgattr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ describe('astro:image', () => {
const $img = $('img');
assert.equal(new URL($img.attr('src')!, 'http://example.com').searchParams.get('w'), '50');
});

it('Image alt and title survive the __ASTRO_IMAGE_ round-trip escaped exactly once', async () => {
// The attribute is JSON-serialized into the markup and decoded again
// later; an ampersand must come back as one ampersand, not as the
// text of an entity left behind by an incomplete decode. cheerio
// hands back the decoded DOM value, so a double escape shows up
// here as a literal "&".
const $img = $('img');
assert.equal($img.attr('alt'), 'A & B');
assert.equal($img.attr('title'), 'C & D');
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
![alt](../assets/penguin2.jpg)
![A & B](../assets/penguin2.jpg "C & D")
Loading