Skip to content

Commit

Permalink
Handle encoded image paths in markdown (#11310)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jun 21, 2024
1 parent 89b46b8 commit b6afe6a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-hats-tell.md
Original file line number Diff line number Diff line change
@@ -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`
5 changes: 4 additions & 1 deletion packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions packages/markdown/remark/src/remark-collect-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export function remarkCollectImages() {
const imagePaths = new Set<string>();
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));
}
}
});
Expand Down

0 comments on commit b6afe6a

Please sign in to comment.