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:
parent
5e466ef499
commit
ccc05d5401
5 changed files with 18 additions and 1 deletions
5
.changeset/weak-plums-sip.md
Normal file
5
.changeset/weak-plums-sip.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Fix build failure when image file name includes special characters
|
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
|
|
BIN
packages/astro/test/fixtures/core-image/src/assets/c++.png
vendored
Normal file
BIN
packages/astro/test/fixtures/core-image/src/assets/c++.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 677 B |
3
packages/astro/test/fixtures/core-image/src/pages/specialChars.md
vendored
Normal file
3
packages/astro/test/fixtures/core-image/src/pages/specialChars.md
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
![C++](../assets/c++.png)
|
||||
|
||||
Image with special characters in file name worked.
|
Loading…
Reference in a new issue