fix(v2): fix encoding of markdown image/file inline file-loaders (#4736)

* try to reproduce windows edge case due to file encoding

* mdx loader => required file paths should be escaped

* revert bad change

* try to fix posix path issues

* try to fix posix path issues

* attempt to fix the file-loader edge cases with non-ascii chars

* Add more example image edge-cases
This commit is contained in:
Sébastien Lorber 2021-05-06 16:48:20 +02:00 committed by GitHub
parent 39105f03d4
commit a79c70c954
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 148 additions and 40 deletions

View file

@ -11,7 +11,11 @@ const url = require('url');
const fs = require('fs-extra');
const escapeHtml = require('escape-html');
const {getFileLoaderUtils} = require('@docusaurus/core/lib/webpack/utils');
const {posixPath, toMessageRelativeFilePath} = require('@docusaurus/utils');
const {
posixPath,
escapePath,
toMessageRelativeFilePath,
} = require('@docusaurus/utils');
const {
loaders: {inlineMarkdownImageFileLoader},
@ -22,7 +26,9 @@ const createJSX = (node, pathUrl) => {
jsxNode.type = 'jsx';
jsxNode.value = `<img ${node.alt ? `alt={"${escapeHtml(node.alt)}"} ` : ''}${
node.url
? `src={require("${inlineMarkdownImageFileLoader}${pathUrl}").default}`
? `src={require("${inlineMarkdownImageFileLoader}${escapePath(
pathUrl,
)}").default}`
: ''
}${node.title ? ` title="${escapeHtml(node.title)}"` : ''} />`;

View file

@ -5,7 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/
const {toMessageRelativeFilePath, posixPath} = require('@docusaurus/utils');
const {
toMessageRelativeFilePath,
posixPath,
escapePath,
} = require('@docusaurus/utils');
const visit = require('unist-util-visit');
const path = require('path');
@ -43,7 +47,9 @@ function toAssetRequireNode({node, filePath, requireAssetPath}) {
? relativeRequireAssetPath
: `./${relativeRequireAssetPath}`;
const href = `require('${inlineMarkdownLinkFileLoader}${relativeRequireAssetPath}').default`;
const href = `require('${inlineMarkdownLinkFileLoader}${escapePath(
relativeRequireAssetPath,
)}').default`;
const children = (node.children || []).map((n) => toValue(n)).join('');
const title = node.title ? `title="${escapeHtml(node.title)}"` : '';