-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypedoc.plugin.mjs
More file actions
184 lines (156 loc) · 5.77 KB
/
typedoc.plugin.mjs
File metadata and controls
184 lines (156 loc) · 5.77 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// @ts-check
import * as typedoc from "typedoc";
/** @param {typedoc.Application} app */
export function load(app) {
app.outputs.addOutput("schema-reference", async (outputDir, project) => {
app.renderer.router = new SchemaReferenceRouter(app);
app.renderer.theme = new typedoc.DefaultTheme(app.renderer);
app.renderer.trigger(typedoc.RendererEvent.BEGIN, new typedoc.RendererEvent(outputDir, project, []));
const pageEvents = buildPageEvents(project, app.renderer.router);
const rendered = renderPageEvents(pageEvents, /** @type {typedoc.DefaultTheme} */ (app.renderer.theme));
process.stdout.write(`---\n`);
process.stdout.write(`title: Schema Reference\n`);
process.stdout.write(`---\n\n`);
process.stdout.write(`<div id="schema-reference" />\n\n`);
process.stdout.write(rendered);
// Wait for all output to be written before allowing the process to exit.
await new Promise((resolve) => process.stdout.write("", () => resolve(undefined)));
})
app.outputs.setDefaultOutputName("schema-reference")
}
class SchemaReferenceRouter extends typedoc.StructureRouter {
/**
* @param {typedoc.RouterTarget} target
* @returns {string}
*/
getFullUrl(target) {
return "#" + this.getAnchor(target);
}
/**
* @param {typedoc.RouterTarget} target
* @returns {string}
*/
getAnchor(target) {
if (target instanceof typedoc.DeclarationReflection &&
target.kindOf(typedoc.ReflectionKind.Property) &&
!hasComment(target)
) {
return "";
} else {
// Must use `toLowerCase()` because Mintlify generates lower case IDs for Markdown headings.
return super.getFullUrl(target).replace(".html", "").replaceAll(/[./#]/g, "-").toLowerCase();
}
}
}
/**
* @param {typedoc.DeclarationReflection} member
* @returns {boolean}
*/
function hasComment(member) {
return member.hasComment() || (
member.type instanceof typedoc.ReflectionType &&
!!member.type.declaration.children?.some((child) => hasComment(child))
);
}
/**
* @param {typedoc.ProjectReflection} project
* @param {typedoc.Router} router
* @returns {typedoc.PageEvent[]}
*/
function buildPageEvents(project, router) {
const events = [];
for (const pageDefinition of router.buildPages(project)) {
const event = new typedoc.PageEvent(pageDefinition.model)
event.url = pageDefinition.url;
event.filename = pageDefinition.url;
event.pageKind = pageDefinition.kind;
event.project = project;
events.push(event)
}
return events;
}
/**
* @param {typedoc.PageEvent[]} events
* @param {typedoc.DefaultTheme} theme
* @returns {string}
*/
function renderPageEvents(events, theme) {
const declarationEvents = events.
filter(isDeclarationReflectionEvent).
sort((event1, event2) => event1.model.name.localeCompare(event2.model.name));
/** @type {Map<string, string[]>} */
const outputsByCategory = new Map();
for (const event of declarationEvents) {
const category = getReflectionCategory(event.model);
const rendered = renderReflection(event.model, theme.getRenderContext(event));
if (!outputsByCategory.has(category)) {
outputsByCategory.set(category, [renderCategory(category)]);
}
outputsByCategory.get(category)?.push(rendered);
}
return [...outputsByCategory.keys()].
sort().flatMap((category) => outputsByCategory.get(category)).join("\n");
}
/**
* @param {typedoc.PageEvent} event
* @returns {event is typedoc.PageEvent<typedoc.DeclarationReflection>}
*/
function isDeclarationReflectionEvent(event) {
return event.model instanceof typedoc.DeclarationReflection;
}
/**
* @param {typedoc.DeclarationReflection} reflection
* @returns {string}
*/
function getReflectionCategory(reflection) {
const categoryTag = reflection.comment?.getTag("@category");
return categoryTag ? categoryTag.content.map((part) => part.text).join(" ") : "";
}
/**
* @param {string} category
* @returns {string}
*/
function renderCategory(category) {
let heading = category || "Common Types";
if (heading.match(/^[a-z]/)) heading = "`" + heading + "`";
return `## ${heading}\n`;
}
/**
* @param {typedoc.DeclarationReflection} reflection
* @param {typedoc.DefaultThemeRenderContext} context
* @returns {string}
*/
function renderReflection(reflection, context) {
const name = reflection.getFriendlyFullName();
const members = reflection.children?.filter(hasComment) ?? [];
const codeBlock = context.reflectionPreview(reflection);
let content = renderJsxElements(
codeBlock ?
[codeBlock, context.commentSummary(reflection)] :
context.memberDeclaration(reflection),
members.map(member => context.member(member)),
);
// Convert `<hN>` elements to `<div>`.
content = content.
replaceAll(/<h([1-6])/g, `<div data-typedoc-h="$1"`).
replaceAll(/<\/h[1-6]>/g, `</div>`);
// Reduce code block indent from 4 spaces to 2 spaces.
content = content.replaceAll("\u00A0\u00A0", "\u00A0");
// Accommodate Mintlify's broken Markdown parser.
content = content.
replaceAll("\u00A0", " "). // Encode valid UTF-8 character as HTML entity
replaceAll(/\n+</g, " <"). // Newlines around tags are not significant
replaceAll("[", "["). // `[` inside HTML tags != link
replaceAll("_", "_"). // `_` inside HTML tags != emphasis
replaceAll("{", "{"); // Plain *.md is not supported, so must escape JSX interpolation
// Remove `@TJS-type` tags. (Ideally, we would include this tag in
// `excludeTags`, but a TypeDoc bug rejects tag names with dashes.)
content = content.replaceAll(/<p>@TJS-type [^<]+<\/p>/g, "");
return `### \`${name}\`\n\n${content}\n`;
}
/**
* @param {typedoc.JSX.Children[]} elements
*/
function renderJsxElements(...elements) {
return typedoc.JSX.renderElement(typedoc.JSX.createElement(typedoc.JSX.Fragment, null, elements));
}