summaryrefslogtreecommitdiff
path: root/tools/doc/markdown.mjs
blob: 21104e592b8a24822afb8710b473b314ff4d8ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
      }
    });
  };
}