diff --git a/.changeset/eleven-hats-tell.md b/.changeset/eleven-hats-tell.md new file mode 100644 index 000000000000..523c769dbe88 --- /dev/null +++ b/.changeset/eleven-hats-tell.md @@ -0,0 +1,5 @@ +--- +'@astrojs/markdown-remark': patch +--- + +Handles encoded image paths in internal rehype plugins and return decoded paths from markdown vfile's `data.imagePaths` diff --git a/packages/astro/test/core-image.test.js b/packages/astro/test/core-image.test.js index ee267f2b0a70..abb3326ef154 100644 --- a/packages/astro/test/core-image.test.js +++ b/packages/astro/test/core-image.test.js @@ -489,7 +489,10 @@ describe('astro:image', () => { $ = cheerio.load(html); let $img = $('img'); - assert.equal($img.attr('src').startsWith('/_image'), true); + assert.equal($img.length, 3) + $img.each((_, el) => { + assert.equal(el.attribs.src?.startsWith('/_image'), true); + }) }); it('properly handles remote images', async () => { diff --git a/packages/astro/test/fixtures/core-image/src/assets/penguin with percent%.jpg b/packages/astro/test/fixtures/core-image/src/assets/penguin with percent%.jpg new file mode 100644 index 000000000000..1a8986ac5092 Binary files /dev/null and b/packages/astro/test/fixtures/core-image/src/assets/penguin with percent%.jpg differ diff --git a/packages/astro/test/fixtures/core-image/src/assets/penguin with space.jpg b/packages/astro/test/fixtures/core-image/src/assets/penguin with space.jpg new file mode 100644 index 000000000000..1a8986ac5092 Binary files /dev/null and b/packages/astro/test/fixtures/core-image/src/assets/penguin with space.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 f3177cff75ce..a5f22cb59e45 100644 --- a/packages/astro/test/fixtures/core-image/src/pages/specialChars.md +++ b/packages/astro/test/fixtures/core-image/src/pages/specialChars.md @@ -1,3 +1,5 @@ ![C++](../assets/c++.png) +![Penguin with space](../assets/penguin%20with%20space.jpg) +![Penguin with percent](../assets/penguin%20with%20percent%25.jpg) Image with special characters in file name worked. diff --git a/packages/markdown/remark/src/remark-collect-images.ts b/packages/markdown/remark/src/remark-collect-images.ts index cfce51376592..676875b59dad 100644 --- a/packages/markdown/remark/src/remark-collect-images.ts +++ b/packages/markdown/remark/src/remark-collect-images.ts @@ -11,12 +11,12 @@ export function remarkCollectImages() { const imagePaths = new Set(); visit(tree, ['image', 'imageReference'], (node: Image | ImageReference) => { if (node.type === 'image') { - if (shouldOptimizeImage(node.url)) imagePaths.add(node.url); + if (shouldOptimizeImage(node.url)) imagePaths.add(decodeURI(node.url)); } if (node.type === 'imageReference') { const imageDefinition = definition(node.identifier); if (imageDefinition) { - if (shouldOptimizeImage(imageDefinition.url)) imagePaths.add(imageDefinition.url); + if (shouldOptimizeImage(imageDefinition.url)) imagePaths.add(decodeURI(imageDefinition.url)); } } });