0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

Fix build failure when image file name includes special characters (#9781)

The latest version of `vite-plugin-markdown` uses a regular
expression that includes the file path via string concatenation.
However the file path is not escaped for use in a regular
expressions. So if a markdown document includes a reference to an
image file name which includes certain special characters it will
cause the build to fail.

This patch escapes regex special characters in the file path string
being injected into the regular expression. While I found that not
all special characters will cause this problem, it seems safer to
simply escape all regex specials. I also added test to verify this.

Related to: Commit 165cfc154b

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
This commit is contained in:
Steven Benner 2024-01-23 15:16:56 -08:00 committed by GitHub
parent 5e466ef499
commit ccc05d5401
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 18 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fix build failure when image file name includes special characters

View file

@ -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) {

View file

@ -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();

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

View file

@ -0,0 +1,3 @@
![C++](../assets/c++.png)
Image with special characters in file name worked.