mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
3ce4cef4e6
PR-URL: https://github.com/nodejs/node/pull/45889 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
26 lines
676 B
JavaScript
26 lines
676 B
JavaScript
import { visit } from 'unist-util-visit';
|
|
|
|
export const referenceToLocalMdFile = /^(?![+a-z]+:)([^#?]+)\.md(#.+)?$/i;
|
|
|
|
export function replaceLinks({ filename, linksMapper }) {
|
|
return (tree) => {
|
|
const fileHtmlUrls = linksMapper[filename];
|
|
|
|
visit(tree, (node) => {
|
|
if (node.url) {
|
|
node.url = node.url.replace(
|
|
referenceToLocalMdFile,
|
|
(_, filename, hash) => `${filename}.html${hash || ''}`,
|
|
);
|
|
}
|
|
});
|
|
visit(tree, 'definition', (node) => {
|
|
const htmlUrl = fileHtmlUrls && fileHtmlUrls[node.identifier];
|
|
|
|
if (htmlUrl && typeof htmlUrl === 'string') {
|
|
node.url = htmlUrl;
|
|
}
|
|
});
|
|
};
|
|
}
|