mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-15 01:57:28 +02:00
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:
parent
cdc41d5bb6
commit
bb825faf1c
14 changed files with 36 additions and 12 deletions
|
@ -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,
|
||||
|
|
|
@ -11,3 +11,4 @@ Fancy things we can test for here:
|
|||
- Webpack configs
|
||||
- \_ prefix convention
|
||||
- Huge sidebars impact
|
||||
- Using folders with spaces on purpose
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# Doc 1
|
||||
|
||||
Inside folder with space
|
||||
|
||||
[doc 2](./doc%202.md)
|
|
@ -0,0 +1,5 @@
|
|||
# Doc 2
|
||||
|
||||
Inside folder with space
|
||||
|
||||
[doc 1](./doc%201.md)
|
3
website/_dogfooding/_docs tests/more-test.md
Normal file
3
website/_dogfooding/_docs tests/more-test.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Another test page
|
||||
|
||||
[Test link](./folder%20with%20space/doc%201.md)
|
|
@ -1 +0,0 @@
|
|||
# Another test page
|
|
@ -1 +1 @@
|
|||
_docs-tests
|
||||
_docs tests
|
|
@ -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',
|
||||
},
|
||||
],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue