fix(mdx-loader): fix md image paths with spaces bug related to transformImage encoding problem (#10723)

This commit is contained in:
Sébastien Lorber 2024-11-28 16:39:43 +01:00 committed by GitHub
parent ffdd415129
commit fb7ad2c1bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 37 additions and 6 deletions

View file

@ -29,3 +29,13 @@ in paragraph ![img](static/img.png)
```md
![img](./static/img.png)
```
## Images with spaces
![img](</img with spaces.png>)
![img](<@site/static/img with spaces.png>)
![img](</img with one encoded%2520space.png>)
![img](<@site/static/img with one encoded%2520space.png>)

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

View file

@ -48,5 +48,15 @@ in paragraph <img alt="img" src={require("!<PROJECT_ROOT>/node_modules/url-loade
\`\`\`md
![img](./static/img.png)
\`\`\`
## Images with spaces
<img alt="img" src={require("!<PROJECT_ROOT>/node_modules/url-loader/dist/cjs.js?limit=10000&name=assets/images/[name]-[contenthash].[ext]&fallback=<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js!./static/img with spaces.png").default} width="200" height="200" />
<img alt="img" src={require("!<PROJECT_ROOT>/node_modules/url-loader/dist/cjs.js?limit=10000&name=assets/images/[name]-[contenthash].[ext]&fallback=<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js!./static/img with spaces.png").default} width="200" height="200" />
<img alt="img" src={require("!<PROJECT_ROOT>/node_modules/url-loader/dist/cjs.js?limit=10000&name=assets/images/[name]-[contenthash].[ext]&fallback=<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js!./static/img with one encoded%20space.png").default} width="200" height="200" />
<img alt="img" src={require("!<PROJECT_ROOT>/node_modules/url-loader/dist/cjs.js?limit=10000&name=assets/images/[name]-[contenthash].[ext]&fallback=<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js!./static/img with one encoded%20space.png").default} width="200" height="200" />
"
`;

View file

@ -98,6 +98,7 @@ async function toImageRequireNode(
});
}
} catch (err) {
console.error(err);
// Workaround for https://github.com/yarnpkg/berry/pull/3889#issuecomment-1034469784
// TODO remove this check once fixed in Yarn PnP
if (!process.versions.pnp) {
@ -152,10 +153,7 @@ async function getImageAbsolutePath(
return imageFilePath;
}
// relative paths are resolved against the source file's folder
const imageFilePath = path.join(
path.dirname(filePath),
decodeURIComponent(imagePath),
);
const imageFilePath = path.join(path.dirname(filePath), imagePath);
await ensureImageFileExist(imageFilePath, filePath);
return imageFilePath;
}
@ -180,9 +178,14 @@ async function processImageNode(target: Target, context: Context) {
return;
}
// We decode it first because Node Url.pathname is always encoded
// while the image file-system path are not.
// See https://github.com/facebook/docusaurus/discussions/10720
const decodedPathname = decodeURIComponent(parsedUrl.pathname);
// We try to convert image urls without protocol to images with require calls
// going through webpack ensures that image assets exist at build time
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
const imagePath = await getImageAbsolutePath(decodedPathname, context);
await toImageRequireNode(target, imagePath, context);
}