diff --git a/.changeset/weak-plums-sip.md b/.changeset/weak-plums-sip.md new file mode 100644 index 0000000000..9f206ca97f --- /dev/null +++ b/.changeset/weak-plums-sip.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fix build failure when image file name includes special characters diff --git a/packages/astro/src/vite-plugin-markdown/images.ts b/packages/astro/src/vite-plugin-markdown/images.ts index 1123c28784..66ec141067 100644 --- a/packages/astro/src/vite-plugin-markdown/images.ts +++ b/packages/astro/src/vite-plugin-markdown/images.ts @@ -13,7 +13,7 @@ export function getMarkdownCodeForImages(imagePaths: MarkdownImagePath[], html: .map((entry) => { const rawUrl = JSON.stringify(entry.raw); return `{ - const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl} + '[^"]*)"', 'g'); + const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\\\$&')} + '[^"]*)"', 'g'); let match; let occurrenceCounter = 0; while ((match = regex.exec(html)) !== null) { diff --git a/packages/astro/test/core-image.test.js b/packages/astro/test/core-image.test.js index 67a76ba880..fbaedd9a1e 100644 --- a/packages/astro/test/core-image.test.js +++ b/packages/astro/test/core-image.test.js @@ -427,6 +427,15 @@ describe('astro:image', () => { expect($img.attr('src').startsWith('/_image')).to.equal(true); }); + it('Supports special characters in file name', async () => { + let res = await fixture.fetch('/specialChars'); + let html = await res.text(); + $ = cheerio.load(html); + + let $img = $('img'); + expect($img.attr('src').startsWith('/_image')).to.equal(true); + }); + it('properly handles remote images', async () => { let res = await fixture.fetch('/httpImage'); let html = await res.text(); diff --git a/packages/astro/test/fixtures/core-image/src/assets/c++.png b/packages/astro/test/fixtures/core-image/src/assets/c++.png new file mode 100644 index 0000000000..25c97b7cb5 Binary files /dev/null and b/packages/astro/test/fixtures/core-image/src/assets/c++.png 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 new file mode 100644 index 0000000000..f3177cff75 --- /dev/null +++ b/packages/astro/test/fixtures/core-image/src/pages/specialChars.md @@ -0,0 +1,3 @@ +![C++](../assets/c++.png) + +Image with special characters in file name worked.