diff options
author | 2025-04-28 14:08:55 +0200 | |
---|---|---|
committer | 2025-04-28 13:08:55 +0100 | |
commit | 60d5be4af49a72e3739f74424c3d5c423f98c133 (patch) | |
tree | dca10a72bccc49a50467b86c5009386736382622 | |
parent | 0cd3f320e8d8532288e3da2c0a39d1cbbf567005 (diff) | |
download | astro-60d5be4af49a72e3739f74424c3d5c423f98c133.tar.gz astro-60d5be4af49a72e3739f74424c3d5c423f98c133.tar.zst astro-60d5be4af49a72e3739f74424c3d5c423f98c133.zip |
fix: images urls containing ' which are inferSize'd (#13692)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
-rw-r--r-- | .changeset/dirty-months-hunt.md | 5 | ||||
-rw-r--r-- | packages/astro/src/vite-plugin-markdown/images.ts | 8 | ||||
-rw-r--r-- | packages/astro/test/core-image.test.js | 2 | ||||
-rw-r--r-- | packages/astro/test/fixtures/core-image/src/assets/penguin with apostrophe'.jpg | bin | 0 -> 11621 bytes | |||
-rw-r--r-- | packages/astro/test/fixtures/core-image/src/pages/specialChars.md | 1 |
5 files changed, 11 insertions, 5 deletions
diff --git a/.changeset/dirty-months-hunt.md b/.changeset/dirty-months-hunt.md new file mode 100644 index 000000000..0a518d312 --- /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 b99d1af23..c98d3c2ca 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 10ca9b686..353f544a7 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 Binary files differnew file mode 100644 index 000000000..1a8986ac5 --- /dev/null +++ b/packages/astro/test/fixtures/core-image/src/assets/penguin with apostrophe'.jpg 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 a5f22cb59..ab52da3c5 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 @@    + Image with special characters in file name worked. |