From bb825faf1c7dc34288dbca1514ddc02149f29804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Mon, 2 Aug 2021 18:02:01 +0200 Subject: [PATCH] fix: ability to link md files with relative paths when paths contain space (#5270) * fix md linkify function when spaces in paths * fix linkify --- .../docusaurus-utils/src/markdownLinks.ts | 27 +++++++++++++------ website/_dogfooding/README.md | 1 + .../2020-08-03-second-blog-intro.md | 0 .../{_blog-tests => _blog tests}/_partial.mdx | 0 .../_docs tests/folder with space/doc 1.md | 5 ++++ .../_docs tests/folder with space/doc 2.md | 5 ++++ .../{_docs-tests => _docs tests}/index.md | 0 website/_dogfooding/_docs tests/more-test.md | 3 +++ .../standalone.md | 0 website/_dogfooding/_docs-tests/more-test.md | 1 - .../_pagePartial.md | 0 .../{_pages-tests => _pages tests}/index.md | 0 website/_dogfooding/docs-tests-symlink | 2 +- website/_dogfooding/dogfooding.config.js | 4 +-- 14 files changed, 36 insertions(+), 12 deletions(-) rename website/_dogfooding/{_blog-tests => _blog tests}/2020-08-03-second-blog-intro.md (100%) rename website/_dogfooding/{_blog-tests => _blog tests}/_partial.mdx (100%) create mode 100644 website/_dogfooding/_docs tests/folder with space/doc 1.md create mode 100644 website/_dogfooding/_docs tests/folder with space/doc 2.md rename website/_dogfooding/{_docs-tests => _docs tests}/index.md (100%) create mode 100644 website/_dogfooding/_docs tests/more-test.md rename website/_dogfooding/{_docs-tests => _docs tests}/standalone.md (100%) delete mode 100644 website/_dogfooding/_docs-tests/more-test.md rename website/_dogfooding/{_pages-tests => _pages tests}/_pagePartial.md (100%) rename website/_dogfooding/{_pages-tests => _pages tests}/index.md (100%) diff --git a/packages/docusaurus-utils/src/markdownLinks.ts b/packages/docusaurus-utils/src/markdownLinks.ts index b8c874a53c..6379d7cd38 100644 --- a/packages/docusaurus-utils/src/markdownLinks.ts +++ b/packages/docusaurus-utils/src/markdownLinks.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {resolve} from 'url'; +import path from 'path'; import {aliasedSitePath} from './index'; export type ContentPaths = { @@ -63,16 +63,27 @@ export function replaceMarkdownLinks({ // Replace it to correct html link. const mdLink = mdMatch[1]; - const aliasedSource = (source: string) => - aliasedSitePath(source, siteDir); + const sourcesToTry = [ + path.resolve(path.dirname(filePath), decodeURIComponent(mdLink)), + `${contentPathLocalized}/${decodeURIComponent(mdLink)}`, + `${contentPath}/${decodeURIComponent(mdLink)}`, + ]; - const permalink: string | undefined = - sourceToPermalink[aliasedSource(resolve(filePath, mdLink))] || - sourceToPermalink[aliasedSource(`${contentPathLocalized}/${mdLink}`)] || - sourceToPermalink[aliasedSource(`${contentPath}/${mdLink}`)]; + const aliasedSourceMatch = sourcesToTry + .map((source) => aliasedSitePath(source, siteDir)) + .find((source) => sourceToPermalink[source]); + + const permalink: string | undefined = aliasedSourceMatch + ? sourceToPermalink[aliasedSourceMatch] + : undefined; if (permalink) { - modifiedLine = modifiedLine.replace(mdLink, permalink); + // MDX won't be happy if the permalink contains a space, we need to convert it to %20 + const encodedPermalink = permalink + .split('/') + .map((part) => part.replace(/\s/g, '%20')) + .join('/'); + modifiedLine = modifiedLine.replace(mdLink, encodedPermalink); } else { const brokenMarkdownLink: BrokenMarkdownLink = { contentPaths, diff --git a/website/_dogfooding/README.md b/website/_dogfooding/README.md index 59eeac6876..e49cae3ade 100644 --- a/website/_dogfooding/README.md +++ b/website/_dogfooding/README.md @@ -11,3 +11,4 @@ Fancy things we can test for here: - Webpack configs - \_ prefix convention - Huge sidebars impact +- Using folders with spaces on purpose diff --git a/website/_dogfooding/_blog-tests/2020-08-03-second-blog-intro.md b/website/_dogfooding/_blog tests/2020-08-03-second-blog-intro.md similarity index 100% rename from website/_dogfooding/_blog-tests/2020-08-03-second-blog-intro.md rename to website/_dogfooding/_blog tests/2020-08-03-second-blog-intro.md diff --git a/website/_dogfooding/_blog-tests/_partial.mdx b/website/_dogfooding/_blog tests/_partial.mdx similarity index 100% rename from website/_dogfooding/_blog-tests/_partial.mdx rename to website/_dogfooding/_blog tests/_partial.mdx diff --git a/website/_dogfooding/_docs tests/folder with space/doc 1.md b/website/_dogfooding/_docs tests/folder with space/doc 1.md new file mode 100644 index 0000000000..3320d8d9b7 --- /dev/null +++ b/website/_dogfooding/_docs tests/folder with space/doc 1.md @@ -0,0 +1,5 @@ +# Doc 1 + +Inside folder with space + +[doc 2](./doc%202.md) diff --git a/website/_dogfooding/_docs tests/folder with space/doc 2.md b/website/_dogfooding/_docs tests/folder with space/doc 2.md new file mode 100644 index 0000000000..b2c3f1ab0c --- /dev/null +++ b/website/_dogfooding/_docs tests/folder with space/doc 2.md @@ -0,0 +1,5 @@ +# Doc 2 + +Inside folder with space + +[doc 1](./doc%201.md) diff --git a/website/_dogfooding/_docs-tests/index.md b/website/_dogfooding/_docs tests/index.md similarity index 100% rename from website/_dogfooding/_docs-tests/index.md rename to website/_dogfooding/_docs tests/index.md diff --git a/website/_dogfooding/_docs tests/more-test.md b/website/_dogfooding/_docs tests/more-test.md new file mode 100644 index 0000000000..a8b7310c06 --- /dev/null +++ b/website/_dogfooding/_docs tests/more-test.md @@ -0,0 +1,3 @@ +# Another test page + +[Test link](./folder%20with%20space/doc%201.md) diff --git a/website/_dogfooding/_docs-tests/standalone.md b/website/_dogfooding/_docs tests/standalone.md similarity index 100% rename from website/_dogfooding/_docs-tests/standalone.md rename to website/_dogfooding/_docs tests/standalone.md diff --git a/website/_dogfooding/_docs-tests/more-test.md b/website/_dogfooding/_docs-tests/more-test.md deleted file mode 100644 index 0f3856b736..0000000000 --- a/website/_dogfooding/_docs-tests/more-test.md +++ /dev/null @@ -1 +0,0 @@ -# Another test page diff --git a/website/_dogfooding/_pages-tests/_pagePartial.md b/website/_dogfooding/_pages tests/_pagePartial.md similarity index 100% rename from website/_dogfooding/_pages-tests/_pagePartial.md rename to website/_dogfooding/_pages tests/_pagePartial.md diff --git a/website/_dogfooding/_pages-tests/index.md b/website/_dogfooding/_pages tests/index.md similarity index 100% rename from website/_dogfooding/_pages-tests/index.md rename to website/_dogfooding/_pages tests/index.md diff --git a/website/_dogfooding/docs-tests-symlink b/website/_dogfooding/docs-tests-symlink index bdc2840915..4a3222e9fd 120000 --- a/website/_dogfooding/docs-tests-symlink +++ b/website/_dogfooding/docs-tests-symlink @@ -1 +1 @@ -_docs-tests \ No newline at end of file +_docs tests \ No newline at end of file diff --git a/website/_dogfooding/dogfooding.config.js b/website/_dogfooding/dogfooding.config.js index 8c7e34c6aa..099f838195 100644 --- a/website/_dogfooding/dogfooding.config.js +++ b/website/_dogfooding/dogfooding.config.js @@ -19,7 +19,7 @@ exports.dogfoodingPluginInstances = [ '@docusaurus/plugin-content-blog', { id: 'blog-tests', - path: '_dogfooding/_blog-tests', + path: '_dogfooding/_blog tests', routeBasePath: '/tests/blog', editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/_dogfooding/_blog-tests', @@ -35,7 +35,7 @@ exports.dogfoodingPluginInstances = [ '@docusaurus/plugin-content-pages', { id: 'pages-tests', - path: '_dogfooding/_pages-tests', + path: '_dogfooding/_pages tests', routeBasePath: '/tests/pages', }, ],