• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { visit } from 'unist-util-visit';
2
3export const referenceToLocalMdFile = /^(?![+a-z]+:)([^#?]+)\.md(#.+)?$/i;
4
5export function replaceLinks({ filename, linksMapper }) {
6  return (tree) => {
7    const fileHtmlUrls = linksMapper[filename];
8
9    visit(tree, (node) => {
10      if (node.url) {
11        node.url = node.url.replace(
12          referenceToLocalMdFile,
13          (_, filename, hash) => `${filename}.html${hash || ''}`
14        );
15      }
16    });
17    visit(tree, 'definition', (node) => {
18      const htmlUrl = fileHtmlUrls && fileHtmlUrls[node.identifier];
19
20      if (htmlUrl && typeof htmlUrl === 'string') {
21        node.url = htmlUrl;
22      }
23    });
24  };
25}
26