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,