diff --git a/.changeset/fix-image-attr-ampersand.md b/.changeset/fix-image-attr-ampersand.md new file mode 100644 index 000000000000..2b7463e00e16 --- /dev/null +++ b/.changeset/fix-image-attr-ampersand.md @@ -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. diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index befcae103b6a..437b6f4af6b3 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -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; diff --git a/packages/astro/src/vite-plugin-markdown/images.ts b/packages/astro/src/vite-plugin-markdown/images.ts index c16214b9150b..3494690a6879 100644 --- a/packages/astro/src/vite-plugin-markdown/images.ts +++ b/packages/astro/src/vite-plugin-markdown/images.ts @@ -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). @@ -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); } }`; @@ -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; diff --git a/packages/astro/test/core-image-remark-imgattr.test.ts b/packages/astro/test/core-image-remark-imgattr.test.ts index e2b98f043794..62656954db0c 100644 --- a/packages/astro/test/core-image-remark-imgattr.test.ts +++ b/packages/astro/test/core-image-remark-imgattr.test.ts @@ -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'); + }); }); }); }); diff --git a/packages/astro/test/fixtures/core-image-remark-imgattr/src/pages/index.md b/packages/astro/test/fixtures/core-image-remark-imgattr/src/pages/index.md index e415e505d69d..8152f8f9b25e 100644 --- a/packages/astro/test/fixtures/core-image-remark-imgattr/src/pages/index.md +++ b/packages/astro/test/fixtures/core-image-remark-imgattr/src/pages/index.md @@ -1 +1 @@ -![alt](../assets/penguin2.jpg) +![A & B](../assets/penguin2.jpg "C & D")