mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-01 19:27:48 +02:00
fix(core): handle single quotes inside file paths (#9160)
This commit is contained in:
parent
43d070ec55
commit
cd61c7bddc
3 changed files with 12 additions and 4 deletions
|
@ -96,12 +96,16 @@ export function aliasedSitePath(filePath: string, siteDir: string): string {
|
||||||
* When you have a path like C:\X\Y
|
* When you have a path like C:\X\Y
|
||||||
* It is not safe to use directly when generating code
|
* It is not safe to use directly when generating code
|
||||||
* For example, this would fail due to unescaped \:
|
* For example, this would fail due to unescaped \:
|
||||||
* `<img src={require('${filePath}')} />`
|
* `<img src={require("${filePath}")} />`
|
||||||
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
* But this would work: `<img src={require("${escapePath(filePath)}")} />`
|
||||||
*
|
*
|
||||||
* posixPath can't be used in all cases, because forward slashes are only valid
|
* posixPath can't be used in all cases, because forward slashes are only valid
|
||||||
* Windows paths when they don't contain non-ascii characters, and posixPath
|
* Windows paths when they don't contain non-ascii characters, and posixPath
|
||||||
* doesn't escape those that fail to be converted.
|
* doesn't escape those that fail to be converted.
|
||||||
|
*
|
||||||
|
* This function escapes double quotes but not single quotes (because it uses
|
||||||
|
* `JSON.stringify`). Therefore, you must put the escaped path inside double
|
||||||
|
* quotes when generating code.
|
||||||
*/
|
*/
|
||||||
export function escapePath(str: string): string {
|
export function escapePath(str: string): string {
|
||||||
const escaped = JSON.stringify(str);
|
const escaped = JSON.stringify(str);
|
||||||
|
|
|
@ -179,7 +179,7 @@ export default ${JSON.stringify(siteConfig, null, 2)};
|
||||||
${clientModules
|
${clientModules
|
||||||
// Use `require()` because `import()` is async but client modules can have CSS
|
// Use `require()` because `import()` is async but client modules can have CSS
|
||||||
// and the order matters for loading CSS.
|
// and the order matters for loading CSS.
|
||||||
.map((clientModule) => ` require('${escapePath(clientModule)}'),`)
|
.map((clientModule) => ` require("${escapePath(clientModule)}"),`)
|
||||||
.join('\n')}
|
.join('\n')}
|
||||||
];
|
];
|
||||||
`,
|
`,
|
||||||
|
@ -193,7 +193,8 @@ ${Object.entries(registry)
|
||||||
.sort((a, b) => a[0].localeCompare(b[0]))
|
.sort((a, b) => a[0].localeCompare(b[0]))
|
||||||
.map(
|
.map(
|
||||||
([chunkName, modulePath]) =>
|
([chunkName, modulePath]) =>
|
||||||
` '${chunkName}': [() => import(/* webpackChunkName: '${chunkName}' */ '${modulePath}'), '${modulePath}', require.resolveWeak('${modulePath}')],`,
|
// modulePath is already escaped by escapePath
|
||||||
|
` "${chunkName}": [() => import(/* webpackChunkName: "${chunkName}" */ "${modulePath}"), "${modulePath}", require.resolveWeak("${modulePath}")],`,
|
||||||
)
|
)
|
||||||
.join('\n')}};
|
.join('\n')}};
|
||||||
`,
|
`,
|
||||||
|
|
3
website/_dogfooding/_docs tests/beginner's guide.mdx
Normal file
3
website/_dogfooding/_docs tests/beginner's guide.mdx
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# Beginner's guide
|
||||||
|
|
||||||
|
[#9160](https://github.com/facebook/docusaurus/pull/9160)
|
Loading…
Add table
Reference in a new issue