0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/tools/doc/markdown.mjs
Aviv Keller 71785889c8
lib: prefer logical assignment
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>
2024-10-09 06:42:16 +00:00

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;
}
});
};
}