mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-14 09:37:37 +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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue