mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-23 19:48:54 +02:00
fix(mdx-loader): fix md image paths with spaces bug related to transformImage encoding problem (#10723)
This commit is contained in:
parent
ffdd415129
commit
fb7ad2c1bb
7 changed files with 37 additions and 6 deletions
|
@ -29,3 +29,13 @@ in paragraph 
|
|||
```md
|
||||

|
||||
```
|
||||
|
||||
## Images with spaces
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 5 KiB |
Binary file not shown.
After Width: | Height: | Size: 5 KiB |
|
@ -48,5 +48,15 @@ in paragraph <img alt="img" src={require("!<PROJECT_ROOT>/node_modules/url-loade
|
|||
\`\`\`md
|
||||

|
||||
\`\`\`
|
||||
|
||||
## 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" />
|
||||
"
|
||||
`;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue