fix(mdx-loader): allow image paths to be URL encoded (#6792)

This commit is contained in:
Joshua Chen 2022-03-02 19:14:53 +08:00 committed by GitHub
parent 1ac41132e3
commit 8a24d56783
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 18 additions and 2 deletions

View file

@ -8,6 +8,8 @@
![img from second static folder](./static2/img2.png)
![img with URL encoded chars](./static2/img2%20copy.png)
![img](./static/img.png 'Title') ![img](/img.png)
![img with "quotes"](./static/img.png ''Quoted' title')

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

View file

@ -27,6 +27,8 @@ exports[`transformImage plugin transform md images to <img /> 1`] = `
<img loading=\\"lazy\\" alt={\\"img from second static folder\\"} 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!./static2/img2.png\\").default} width=\\"256\\" height=\\"82\\" />
<img loading=\\"lazy\\" alt={\\"img with URL encoded chars\\"} 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!./static2/img2 copy.png\\").default} width=\\"256\\" height=\\"82\\" />
<img loading=\\"lazy\\" 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.png\\").default} title=\\"Title\\" width=\\"200\\" height=\\"200\\" /> <img loading=\\"lazy\\" 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.png\\").default} width=\\"200\\" height=\\"200\\" />
<img loading=\\"lazy\\" alt={\\"img with &quot;quotes&quot;\\"} 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.png\\").default} title=\\"&#39;Quoted&#39; title\\" width=\\"200\\" height=\\"200\\" />

View file

@ -121,7 +121,10 @@ async function getImageAbsolutePath(
return imageFilePath;
}
// relative paths are resolved against the source file's folder
const imageFilePath = path.join(path.dirname(filePath), imagePath);
const imageFilePath = path.join(
path.dirname(filePath),
decodeURIComponent(imagePath),
);
await ensureImageFileExist(imageFilePath, filePath);
return imageFilePath;
}

View file

@ -4,6 +4,8 @@
[asset](./asset.pdf)
[asset with URL encoded chars](./asset%20%282%29.pdf)
[asset with hash](./asset.pdf#page=2)
[asset](asset.pdf 'Title')

View file

@ -16,6 +16,8 @@ exports[`transformAsset plugin transform md links to <a /> 1`] = `
<a target=\\"_blank\\" href={require('!<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[contenthash].[ext]!./asset.pdf').default}>asset</a>
<a target=\\"_blank\\" href={require('!<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[contenthash].[ext]!./asset (2).pdf').default}>asset with URL encoded chars</a>
<a target=\\"_blank\\" href={require('!<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[contenthash].[ext]!./asset.pdf').default + '#page=2'}>asset with hash</a>
<a target=\\"_blank\\" href={require('!<PROJECT_ROOT>/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[contenthash].[ext]!./asset.pdf').default} title=\\"Title\\">asset</a>

View file

@ -130,7 +130,10 @@ async function processLinkNode(node: Link, context: Context) {
return;
}
const assetPath = await getAssetAbsolutePath(parsedUrl.pathname, context);
const assetPath = await getAssetAbsolutePath(
decodeURIComponent(parsedUrl.pathname),
context,
);
if (assetPath) {
toAssetRequireNode(node, assetPath, context.filePath);
}

View file

@ -10,6 +10,8 @@ import docusaurusImport from '@site/static/img/docusaurus.png';
export const docusaurusRequire = require('@site/static/img/docusaurus.png');
![URL encoded image](./img/oss_logo%20%282%29.png)
## Regular images
<img src={docusaurusImport} />

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB