-
Notifications
You must be signed in to change notification settings - Fork 6
/
formatter.mjs
58 lines (54 loc) · 1.65 KB
/
formatter.mjs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// @ts-check
import { fileURLToPath } from 'node:url';
import { dirname, relative } from 'node:path';
import { MarkdownPageEvent } from 'typedoc-plugin-markdown';
/**
* @param {import('typedoc-plugin-markdown').MarkdownApplication} app
*/
export function load(app) {
const root = fileURLToPath(new URL('./', import.meta.url));
// Set "title" frontmatter for each page
app.renderer.on(
MarkdownPageEvent.BEGIN,
/** @param {import('typedoc-plugin-markdown').MarkdownPageEvent} page */
(page) => {
page.frontmatter = {
title: page.model.name,
};
},
);
// Resolve all Markdown links to root relative
app.renderer.on(
MarkdownPageEvent.END,
/** @param {import('typedoc-plugin-markdown').MarkdownPageEvent} page */
(page) => {
if (!page.contents) return;
const rel = relative(root, dirname(page.filename));
const parts = rel.split('/');
const pkg = parts[1];
const dirParts = parts.slice(3);
const dir = dirParts.length ? `${dirParts.join('/')}/` : '';
page.contents = page.contents.replace(
/(?<!\\)\[(.*?)]\((.*?)\)/g,
(_, text, link) => {
if (!link.includes('://')) {
const url = new URL(link, `http://e.com/${pkg}/api/${dir}`);
link = `${url.pathname}${url.search}${url.hash}`;
if (link.endsWith('/index')) {
link = link.slice(0, -6);
}
}
return `[${text}](${link})`;
});
},
);
// Remove ".mdx" from the file extension for each link on every page
app.renderer.on(
MarkdownPageEvent.END,
/** @param {import('typedoc-plugin-markdown').MarkdownPageEvent} page */
(page) => {
if (!page.contents) return;
page.contents = page.contents.replace(/\.mdx/g, '');
},
);
}