mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-26 04:57:50 +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
|
```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
|
\`\`\`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) {
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
// Workaround for https://github.com/yarnpkg/berry/pull/3889#issuecomment-1034469784
|
// Workaround for https://github.com/yarnpkg/berry/pull/3889#issuecomment-1034469784
|
||||||
// TODO remove this check once fixed in Yarn PnP
|
// TODO remove this check once fixed in Yarn PnP
|
||||||
if (!process.versions.pnp) {
|
if (!process.versions.pnp) {
|
||||||
|
@ -152,10 +153,7 @@ async function getImageAbsolutePath(
|
||||||
return imageFilePath;
|
return imageFilePath;
|
||||||
}
|
}
|
||||||
// relative paths are resolved against the source file's folder
|
// relative paths are resolved against the source file's folder
|
||||||
const imageFilePath = path.join(
|
const imageFilePath = path.join(path.dirname(filePath), imagePath);
|
||||||
path.dirname(filePath),
|
|
||||||
decodeURIComponent(imagePath),
|
|
||||||
);
|
|
||||||
await ensureImageFileExist(imageFilePath, filePath);
|
await ensureImageFileExist(imageFilePath, filePath);
|
||||||
return imageFilePath;
|
return imageFilePath;
|
||||||
}
|
}
|
||||||
|
@ -180,9 +178,14 @@ async function processImageNode(target: Target, context: Context) {
|
||||||
return;
|
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
|
// We try to convert image urls without protocol to images with require calls
|
||||||
// going through webpack ensures that image assets exist at build time
|
// 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);
|
await toImageRequireNode(target, imagePath, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
BIN
website/_dogfooding/_asset-tests/image with spaces.png
Normal file
BIN
website/_dogfooding/_asset-tests/image with spaces.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5 KiB |
|
@ -206,7 +206,7 @@ Code tag + double pipe: <code>||</code>
|
||||||
|
|
||||||
Code tag + double pipe: <code>||</code>
|
Code tag + double pipe: <code>||</code>
|
||||||
|
|
||||||
## Images edge cases
|
## Images
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
@ -214,6 +214,10 @@ Code tag + double pipe: <code>||</code>
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
## Details
|
## Details
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
@ -359,10 +363,14 @@ See [#3337](https://github.com/facebook/docusaurus/issues/3337)
|
||||||
|
|
||||||
- [/someFile.xyz](/someFile.xyz)
|
- [/someFile.xyz](/someFile.xyz)
|
||||||
|
|
||||||
|
- [/image with space.png](</image with spaces.png>)
|
||||||
|
|
||||||
- [@site/\_dogfooding/\_asset-tests/someFile.pdf](@site/_dogfooding/_asset-tests/someFile.pdf)
|
- [@site/\_dogfooding/\_asset-tests/someFile.pdf](@site/_dogfooding/_asset-tests/someFile.pdf)
|
||||||
|
|
||||||
- [@site/\_dogfooding/\_asset-tests/someFile.xyz](@site/_dogfooding/_asset-tests/someFile.xyz)
|
- [@site/\_dogfooding/\_asset-tests/someFile.xyz](@site/_dogfooding/_asset-tests/someFile.xyz)
|
||||||
|
|
||||||
|
- [@site/\_dogfooding/\_asset-tests/image with space.png](<@site/_dogfooding/_asset-tests/image with spaces.png>)
|
||||||
|
|
||||||
### Linking to non-SPA page hosted within website
|
### Linking to non-SPA page hosted within website
|
||||||
|
|
||||||
See [#3309](https://github.com/facebook/docusaurus/issues/3309)
|
See [#3309](https://github.com/facebook/docusaurus/issues/3309)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue