-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathtypedoc.plugin.mjs
More file actions
71 lines (57 loc) · 2.35 KB
/
Copy pathtypedoc.plugin.mjs
File metadata and controls
71 lines (57 loc) · 2.35 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
// @ts-check
import { dirname, resolve } from "node:path";
import { cwd } from "node:process";
import * as td from "typedoc-plugin-markdown";
import { typedocPageIndexData } from "./docs/typedoc-data.mjs";
// Extract the package directory name to be used as the part of the markdown links
let dirs = cwd().split("/packages/");
/** @param {td.MarkdownApplication} app */
export function load(app) {
// For multi-export packages, put each docs into a different directory
if (!app.options.getValue("out").endsWith("/.docs")) {
// console.warn("[app]", app.options.getValue("out"));
dirs = app.options.getValue("out").split("/.docs/");
}
const apiDirName = dirs[dirs.length - 1];
// Set Markdown frontmatter for each page
app.renderer.on(td.MarkdownPageEvent.BEGIN, (page) => {
page.frontmatter = {
title: page.model.name,
};
// Slice in the index data (when desired)
if (page.url == "index.mdx" && typedocPageIndexData[apiDirName]?.frontmatter) {
page.frontmatter = {
...page.frontmatter,
...typedocPageIndexData[apiDirName].frontmatter,
};
}
});
// Rewrite all of the internal links
// - root relative for compatibility with Next.js
// - strip the .mdx extension
app.renderer.on(td.MarkdownPageEvent.END, (page) => {
if (!page.contents) return;
// Slice in the index data (when desired)
if (page.url == "index.mdx" && typedocPageIndexData[apiDirName]?.contents) {
page.contents = insertAfterFrontmatter(page.contents, typedocPageIndexData[apiDirName].contents);
}
page.contents = page.contents.replace(/\(((?:[^\/\)]+\/)*[^\/\)]+)\.mdx\)/gm, (_, path) => {
// Remove trailing /index routes
if (path.endsWith("/index")) path = path.replace(/\/index$/gi, "");
const rootRelativeUrl = resolve(`/api/${apiDirName}`, dirname(page.url), path);
return `(${rootRelativeUrl})`;
});
});
}
function insertAfterFrontmatter(markdownText, textToInsert) {
if (!markdownText.startsWith("---")) {
return textToInsert + markdownText;
}
const secondDelimiter = markdownText.indexOf("\n---\n", 4);
if (secondDelimiter === -1) {
return textToInsert + markdownText;
}
const frontmatter = markdownText.substring(0, secondDelimiter + 5);
const content = markdownText.substring(secondDelimiter + 5);
return frontmatter + `\n${textToInsert}\n` + content;
}