Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle single quotes inside file paths #9160

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/docusaurus-utils/src/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ export function aliasedSitePath(filePath: string, siteDir: string): string {
* When you have a path like C:\X\Y
* It is not safe to use directly when generating code
* For example, this would fail due to unescaped \:
* `<img src={require('${filePath}')} />`
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
* `<img src={require("${filePath}")} />`
* But this would work: `<img src={require("${escapePath(filePath)}")} />`
*
* 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
* 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 {
const escaped = JSON.stringify(str);
Expand Down
5 changes: 3 additions & 2 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default ${JSON.stringify(siteConfig, null, 2)};
${clientModules
// Use `require()` because `import()` is async but client modules can have CSS
// and the order matters for loading CSS.
.map((clientModule) => ` require('${escapePath(clientModule)}'),`)
.map((clientModule) => ` require("${escapePath(clientModule)}"),`)
.join('\n')}
];
`,
Expand All @@ -193,7 +193,8 @@ ${Object.entries(registry)
.sort((a, b) => a[0].localeCompare(b[0]))
.map(
([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')}};
`,
Expand Down
3 changes: 3 additions & 0 deletions website/_dogfooding/_docs tests/beginner's guide.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Beginner's guide

[#9160](https://github.com/facebook/docusaurus/pull/9160)
Loading