fix: ability to link md files with relative paths when paths contain space (#5270)

* fix md linkify function when spaces in paths

* fix linkify
This commit is contained in:
Sébastien Lorber 2021-08-02 18:02:01 +02:00 committed by GitHub
parent cdc41d5bb6
commit bb825faf1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 36 additions and 12 deletions

View file

@ -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<T extends ContentPaths>({
// 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<T> = {
contentPaths,

View file

@ -11,3 +11,4 @@ Fancy things we can test for here:
- Webpack configs
- \_ prefix convention
- Huge sidebars impact
- Using folders with spaces on purpose

View file

@ -0,0 +1,5 @@
# Doc 1
Inside folder with space
[doc 2](./doc%202.md)

View file

@ -0,0 +1,5 @@
# Doc 2
Inside folder with space
[doc 1](./doc%201.md)

View file

@ -0,0 +1,3 @@
# Another test page
[Test link](./folder%20with%20space/doc%201.md)

View file

@ -1 +0,0 @@
# Another test page

View file

@ -1 +1 @@
_docs-tests
_docs tests

View file

@ -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',
},
],