mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 20:29:23 +01:00
71785889c8
PR-URL: https://github.com/nodejs/node/pull/55044 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
24 lines
626 B
JavaScript
24 lines
626 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) => {
|
|
node.url &&= node.url.replace(
|
|
referenceToLocalMdFile,
|
|
(_, filename, hash) => `${filename}.html${hash || ''}`,
|
|
);
|
|
});
|
|
visit(tree, 'definition', (node) => {
|
|
const htmlUrl = fileHtmlUrls?.[node.identifier];
|
|
|
|
if (htmlUrl && typeof htmlUrl === 'string') {
|
|
node.url = htmlUrl;
|
|
}
|
|
});
|
|
};
|
|
}
|