-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathformatter.mjs
59 lines (55 loc) · 1.64 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
59
import { dirname, relative, sep } from "node:path"
// @ts-check
import { fileURLToPath } from "node:url"
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(sep)
const dirParts = parts.slice(3)
const dir = dirParts.length ? `${dirParts.join("/")}/` : ""
page.contents = page.contents.replace(
/(?<!\\)\[(.*?)]\((.*?)\)/g,
(_, text, link) => {
let newLink = link
if (!link.includes("://")) {
const url = new URL(link, `http://e.com/api/${dir}`)
newLink = `${url.pathname}${url.search}${url.hash}`
if (link.endsWith("/index")) {
newLink = link.slice(0, -6)
}
}
return `[${text}](${newLink})`
}
)
}
)
// 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, "")
}
)
}