diff --git a/.changeset/dirty-months-hunt.md b/.changeset/dirty-months-hunt.md new file mode 100644 index 000000000000..0a518d312ccc --- /dev/null +++ b/.changeset/dirty-months-hunt.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug where Astro couldn't probably use `inferSize` for images that contain apostrophe `'` in their name. diff --git a/packages/astro/src/vite-plugin-markdown/images.ts b/packages/astro/src/vite-plugin-markdown/images.ts index b99d1af233b2..c98d3c2ca04f 100644 --- a/packages/astro/src/vite-plugin-markdown/images.ts +++ b/packages/astro/src/vite-plugin-markdown/images.ts @@ -15,7 +15,7 @@ export function getMarkdownCodeForImages( const imageSources = {}; ${localImagePaths .map((entry) => { - const rawUrl = JSON.stringify(entry.raw); + const rawUrl = JSON.stringify(entry.raw).replace(/'/g, '''); return `{ const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace( /[.*+?^${}()|[\]\\]/g, @@ -25,7 +25,7 @@ export function getMarkdownCodeForImages( let occurrenceCounter = 0; while ((match = regex.exec(html)) !== null) { const matchKey = ${rawUrl} + '_' + occurrenceCounter; - const imageProps = JSON.parse(match[1].replace(/"/g, '"')); + const imageProps = JSON.parse(match[1].replace(/"/g, '"').replace(/'/g, "'")); const { src, ...props } = imageProps; imageSources[matchKey] = await getImage({src: Astro__${entry.safeName}, ...props}); occurrenceCounter++; @@ -35,7 +35,7 @@ export function getMarkdownCodeForImages( .join('\n')} ${remoteImagePaths .map((raw) => { - const rawUrl = JSON.stringify(raw); + const rawUrl = JSON.stringify(raw).replace(/'/g, '''); return `{ const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace( /[.*+?^${}()|[\]\\]/g, @@ -45,7 +45,7 @@ export function getMarkdownCodeForImages( let occurrenceCounter = 0; while ((match = regex.exec(html)) !== null) { const matchKey = ${rawUrl} + '_' + occurrenceCounter; - const props = JSON.parse(match[1].replace(/"/g, '"')); + const props = JSON.parse(match[1].replace(/"/g, '"').replace(/'/g, "'")); imageSources[matchKey] = await getImage(props); occurrenceCounter++; } diff --git a/packages/astro/test/core-image.test.js b/packages/astro/test/core-image.test.js index 10ca9b686412..353f544a75cb 100644 --- a/packages/astro/test/core-image.test.js +++ b/packages/astro/test/core-image.test.js @@ -493,7 +493,7 @@ describe('astro:image', () => { $ = cheerio.load(html); let $img = $('img'); - assert.equal($img.length, 3); + assert.equal($img.length, 4); $img.each((_, el) => { assert.equal(el.attribs.src?.startsWith('/_image'), true); }); diff --git a/packages/astro/test/fixtures/core-image/src/assets/penguin with apostrophe'.jpg b/packages/astro/test/fixtures/core-image/src/assets/penguin with apostrophe'.jpg new file mode 100644 index 000000000000..1a8986ac5092 Binary files /dev/null and b/packages/astro/test/fixtures/core-image/src/assets/penguin with apostrophe'.jpg differ diff --git a/packages/astro/test/fixtures/core-image/src/pages/specialChars.md b/packages/astro/test/fixtures/core-image/src/pages/specialChars.md index a5f22cb59e45..ab52da3c5d06 100644 --- a/packages/astro/test/fixtures/core-image/src/pages/specialChars.md +++ b/packages/astro/test/fixtures/core-image/src/pages/specialChars.md @@ -1,5 +1,6 @@ ![C++](../assets/c++.png) ![Penguin with space](../assets/penguin%20with%20space.jpg) ![Penguin with percent](../assets/penguin%20with%20percent%25.jpg) +![Penguin with apostrophe](../assets/penguin%20with%20apostrophe%27.jpg) Image with special characters in file name worked.