Skip to content

Commit b6afe6a

Browse files
authoredJun 21, 2024··
Handle encoded image paths in markdown (#11310)
1 parent 89b46b8 commit b6afe6a

File tree

6 files changed

+13
-3
lines changed

6 files changed

+13
-3
lines changed
 

‎.changeset/eleven-hats-tell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/markdown-remark': patch
3+
---
4+
5+
Handles encoded image paths in internal rehype plugins and return decoded paths from markdown vfile's `data.imagePaths`

‎packages/astro/test/core-image.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ describe('astro:image', () => {
489489
$ = cheerio.load(html);
490490

491491
let $img = $('img');
492-
assert.equal($img.attr('src').startsWith('/_image'), true);
492+
assert.equal($img.length, 3)
493+
$img.each((_, el) => {
494+
assert.equal(el.attribs.src?.startsWith('/_image'), true);
495+
})
493496
});
494497

495498
it('properly handles remote images', async () => {
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
![C++](../assets/c++.png)
2+
![Penguin with space](../assets/penguin%20with%20space.jpg)
3+
![Penguin with percent](../assets/penguin%20with%20percent%25.jpg)
24

35
Image with special characters in file name worked.

‎packages/markdown/remark/src/remark-collect-images.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export function remarkCollectImages() {
1111
const imagePaths = new Set<string>();
1212
visit(tree, ['image', 'imageReference'], (node: Image | ImageReference) => {
1313
if (node.type === 'image') {
14-
if (shouldOptimizeImage(node.url)) imagePaths.add(node.url);
14+
if (shouldOptimizeImage(node.url)) imagePaths.add(decodeURI(node.url));
1515
}
1616
if (node.type === 'imageReference') {
1717
const imageDefinition = definition(node.identifier);
1818
if (imageDefinition) {
19-
if (shouldOptimizeImage(imageDefinition.url)) imagePaths.add(imageDefinition.url);
19+
if (shouldOptimizeImage(imageDefinition.url)) imagePaths.add(decodeURI(imageDefinition.url));
2020
}
2121
}
2222
});

0 commit comments

Comments
 (0)
Please sign in to comment.