mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import type {DocsMarkdownOption} from '../types';
|
|
import {getDocsDirPaths} from '../versions';
|
|
import {replaceMarkdownLinks} from '@docusaurus/utils';
|
|
|
|
function getVersion(filePath: string, options: DocsMarkdownOption) {
|
|
const versionFound = options.versionsMetadata.find((version) =>
|
|
getDocsDirPaths(version).some((docsDirPath) =>
|
|
filePath.startsWith(docsDirPath),
|
|
),
|
|
);
|
|
if (!versionFound) {
|
|
throw new Error(
|
|
`Unexpected error: Markdown file at "${filePath}" does not belong to any docs version!`,
|
|
);
|
|
}
|
|
return versionFound;
|
|
}
|
|
|
|
export function linkify(
|
|
fileString: string,
|
|
filePath: string,
|
|
options: DocsMarkdownOption,
|
|
): string {
|
|
const {siteDir, sourceToPermalink, onBrokenMarkdownLink} = options;
|
|
|
|
const {newContent, brokenMarkdownLinks} = replaceMarkdownLinks({
|
|
siteDir,
|
|
fileString,
|
|
filePath,
|
|
contentPaths: getVersion(filePath, options),
|
|
sourceToPermalink,
|
|
});
|
|
|
|
brokenMarkdownLinks.forEach((l) => onBrokenMarkdownLink(l));
|
|
|
|
return newContent;
|
|
}
|