Skip to content

Commit 60d5be4

Browse files
fix: images urls containing ' which are inferSize'd (#13692)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent 0cd3f32 commit 60d5be4

5 files changed

Lines changed: 11 additions & 5 deletions

File tree

.changeset/dirty-months-hunt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug where Astro couldn't probably use `inferSize` for images that contain apostrophe `'` in their name.

packages/astro/src/vite-plugin-markdown/images.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function getMarkdownCodeForImages(
1515
const imageSources = {};
1616
${localImagePaths
1717
.map((entry) => {
18-
const rawUrl = JSON.stringify(entry.raw);
18+
const rawUrl = JSON.stringify(entry.raw).replace(/'/g, '&#x27;');
1919
return `{
2020
const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace(
2121
/[.*+?^${}()|[\]\\]/g,
@@ -25,7 +25,7 @@ export function getMarkdownCodeForImages(
2525
let occurrenceCounter = 0;
2626
while ((match = regex.exec(html)) !== null) {
2727
const matchKey = ${rawUrl} + '_' + occurrenceCounter;
28-
const imageProps = JSON.parse(match[1].replace(/&#x22;/g, '"'));
28+
const imageProps = JSON.parse(match[1].replace(/&#x22;/g, '"').replace(/&#x27;/g, "'"));
2929
const { src, ...props } = imageProps;
3030
imageSources[matchKey] = await getImage({src: Astro__${entry.safeName}, ...props});
3131
occurrenceCounter++;
@@ -35,7 +35,7 @@ export function getMarkdownCodeForImages(
3535
.join('\n')}
3636
${remoteImagePaths
3737
.map((raw) => {
38-
const rawUrl = JSON.stringify(raw);
38+
const rawUrl = JSON.stringify(raw).replace(/'/g, '&#x27;');
3939
return `{
4040
const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace(
4141
/[.*+?^${}()|[\]\\]/g,
@@ -45,7 +45,7 @@ export function getMarkdownCodeForImages(
4545
let occurrenceCounter = 0;
4646
while ((match = regex.exec(html)) !== null) {
4747
const matchKey = ${rawUrl} + '_' + occurrenceCounter;
48-
const props = JSON.parse(match[1].replace(/&#x22;/g, '"'));
48+
const props = JSON.parse(match[1].replace(/&#x22;/g, '"').replace(/&#x27;/g, "'"));
4949
imageSources[matchKey] = await getImage(props);
5050
occurrenceCounter++;
5151
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ describe('astro:image', () => {
493493
$ = cheerio.load(html);
494494

495495
let $img = $('img');
496-
assert.equal($img.length, 3);
496+
assert.equal($img.length, 4);
497497
$img.each((_, el) => {
498498
assert.equal(el.attribs.src?.startsWith('/_image'), true);
499499
});
11.3 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
![C++](../assets/c++.png)
22
![Penguin with space](../assets/penguin%20with%20space.jpg)
33
![Penguin with percent](../assets/penguin%20with%20percent%25.jpg)
4+
![Penguin with apostrophe](../assets/penguin%20with%20apostrophe%27.jpg)
45

56
Image with special characters in file name worked.

0 commit comments

Comments
 (0)