mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 16:07:39 +00:00
PR-URL: https://github.com/nodejs/node/pull/55045 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
26 lines
662 B
JavaScript
26 lines
662 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?.[node.identifier];
|
|
|
|
if (htmlUrl && typeof htmlUrl === 'string') {
|
|
node.url = htmlUrl;
|
|
}
|
|
});
|
|
};
|
|
}
|