Skip to content

Commit fb7ad2c

Browse files
authored
fix(mdx-loader): fix md image paths with spaces bug related to transformImage encoding problem (#10723)
1 parent ffdd415 commit fb7ad2c

File tree

7 files changed

+37
-6
lines changed

7 files changed

+37
-6
lines changed

packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/img.md

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__snapshots__/index.test.ts.snap

+10
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ in paragraph <img alt="img" src={require("!<PROJECT_ROOT>/node_modules/url-loade
4848
\`\`\`md
4949
![img](./static/img.png)
5050
\`\`\`
51+
52+
## Images with spaces
53+
54+
<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" />
55+
56+
<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" />
57+
58+
<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" />
59+
60+
<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" />
5161
"
5262
`;

packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ async function toImageRequireNode(
9898
});
9999
}
100100
} catch (err) {
101+
console.error(err);
101102
// Workaround for https://github.com/yarnpkg/berry/pull/3889#issuecomment-1034469784
102103
// TODO remove this check once fixed in Yarn PnP
103104
if (!process.versions.pnp) {
@@ -152,10 +153,7 @@ async function getImageAbsolutePath(
152153
return imageFilePath;
153154
}
154155
// relative paths are resolved against the source file's folder
155-
const imageFilePath = path.join(
156-
path.dirname(filePath),
157-
decodeURIComponent(imagePath),
158-
);
156+
const imageFilePath = path.join(path.dirname(filePath), imagePath);
159157
await ensureImageFileExist(imageFilePath, filePath);
160158
return imageFilePath;
161159
}
@@ -180,9 +178,14 @@ async function processImageNode(target: Target, context: Context) {
180178
return;
181179
}
182180

181+
// We decode it first because Node Url.pathname is always encoded
182+
// while the image file-system path are not.
183+
// See https://github.com/facebook/docusaurus/discussions/10720
184+
const decodedPathname = decodeURIComponent(parsedUrl.pathname);
185+
183186
// We try to convert image urls without protocol to images with require calls
184187
// going through webpack ensures that image assets exist at build time
185-
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
188+
const imagePath = await getImageAbsolutePath(decodedPathname, context);
186189
await toImageRequireNode(target, imagePath, context);
187190
}
188191

Loading

website/_dogfooding/_pages tests/markdown-tests-mdx.mdx

+9-1
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,18 @@ Code tag + double pipe: <code>&#124;&#124;</code>
206206

207207
Code tag + double pipe: <code>||</code>
208208

209-
## Images edge cases
209+
## Images
210210

211211
![](/新控制器空间/图片.png)
212212

213213
![](/4/图片.png)
214214

215215
![](/4/docu.png)
216216

217+
![](</image with spaces.png>)
218+
219+
![](<@site/_dogfooding/_asset-tests/image with spaces.png>)
220+
217221
## Details
218222

219223
<details>
@@ -359,10 +363,14 @@ See [#3337](https://github.com/facebook/docusaurus/issues/3337)
359363

360364
- [/someFile.xyz](/someFile.xyz)
361365

366+
- [/image with space.png](</image with spaces.png>)
367+
362368
- [@site/\_dogfooding/\_asset-tests/someFile.pdf](@site/_dogfooding/_asset-tests/someFile.pdf)
363369

364370
- [@site/\_dogfooding/\_asset-tests/someFile.xyz](@site/_dogfooding/_asset-tests/someFile.xyz)
365371

372+
- [@site/\_dogfooding/\_asset-tests/image with space.png](<@site/_dogfooding/_asset-tests/image with spaces.png>)
373+
366374
### Linking to non-SPA page hosted within website
367375

368376
See [#3309](https://github.com/facebook/docusaurus/issues/3309)

0 commit comments

Comments
 (0)