forked from modelcontextprotocol/modelcontextprotocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypedoc.plugin.mjs
More file actions
281 lines (235 loc) · 9.32 KB
/
typedoc.plugin.mjs
File metadata and controls
281 lines (235 loc) · 9.32 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// @ts-check
import { readFile } from "fs/promises";
import * as cheerio from "cheerio";
import * as typedoc from "typedoc";
// Markdown (and JSX) special characters that should be rendered literally
const MARKDOWN_SPECIAL_CHARS = ["[", "_", "*", "`", "~", "\\", "$", "{"];
const MARKDOWN_SPECIAL_CHARS_REGEX = new RegExp(
"[" + MARKDOWN_SPECIAL_CHARS.map(c => "\\" + c).join("") + "]", "g"
);
const MARKDOWN_SPECIAL_CHARS_HTML_ENTITIES = Object.fromEntries(
MARKDOWN_SPECIAL_CHARS.map(c => [c, `&#x${c.charCodeAt(0).toString(16).toUpperCase()};`])
);
/** @param {typedoc.Application} app */
export function load(app) {
app.options.addDeclaration({
name: "schemaPageTemplate",
help: "Template file for schema reference page.",
type: typedoc.ParameterType.String,
});
app.outputs.addOutput("schema-page", async (outputDir, project) => {
const templatePath = /** @type {string} */ (app.options.getValue("schemaPageTemplate"));
const template = await readFile(templatePath, { encoding: "utf-8" });
app.renderer.router = new SchemaPageRouter(app);
app.renderer.theme = new typedoc.DefaultTheme(app.renderer);
const outputEvent = new typedoc.RendererEvent(outputDir, project, []);
await app.renderer.theme.preRender(outputEvent);
app.renderer.trigger(typedoc.RendererEvent.BEGIN, outputEvent);
const pageEvents = buildPageEvents(project, app.renderer.router);
process.stdout.write(
renderTemplate(template, pageEvents, /** @type {typedoc.DefaultTheme} */ (app.renderer.theme))
);
// 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-page")
}
class SchemaPageRouter extends typedoc.StructureRouter {
/**
* @param {typedoc.RouterTarget} target
* @returns {string}
*/
getFullUrl(target) {
return "#" + this.getAnchor(target);
}
/**
* @param {typedoc.RouterTarget} target
* @returns {string}
*/
getAnchor(target) {
// Must use `toLowerCase()` because Mintlify generates lower case IDs for Markdown headings.
return super.getFullUrl(target).replace(".html", "").replaceAll(/[./#]/g, "-").toLowerCase();
}
}
/**
* @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 {string} template
* @param {typedoc.PageEvent[]} pageEvents
* @param {typedoc.DefaultTheme} theme
* @returns {string}
*/
function renderTemplate(template, pageEvents, theme) {
const reflectionEvents = pageEvents.filter(isDeclarationReflectionEvent);
/** @type {Set<string>} */
const renderedCategories = new Set();
const rendered = template.replaceAll(
/^\{\/\* @category (.+) \*\/\}$/mg,
(match, category) => {
renderedCategories.add(category);
return renderCategory(category, reflectionEvents, theme);
}
);
const missingCategories = reflectionEvents.
map((event) => getReflectionCategory(event.model)).
filter((category) => category && !renderedCategories.has(category)).
filter((category, i, array) => array.indexOf(category) === i). // Remove duplicates.
sort();
if (missingCategories.length > 0) {
throw new Error(
"The following categories are missing from the schema page template:\n\n" +
missingCategories.map((category) => `- ${category}\n`).join("")
);
}
return rendered;
}
/**
* @param {typedoc.PageEvent} event
* @returns {event is typedoc.PageEvent<typedoc.DeclarationReflection>}
*/
function isDeclarationReflectionEvent(event) {
return event.model instanceof typedoc.DeclarationReflection;
}
/**
* @param {string} category
* @param {typedoc.DeclarationReflection} reflection1
* @param {typedoc.DeclarationReflection} reflection2
* @returns {number}
*/
function getReflectionOrder(category, reflection1, reflection2) {
let order = 0;
if (isRpcMethodCategory(category)) {
order ||= +reflection2.name.endsWith("Request") - +reflection1.name.endsWith("Request");
order ||= +reflection2.name.endsWith("RequestParams") - +reflection1.name.endsWith("RequestParams");
order ||= +reflection2.name.endsWith("ResultResponse") - +reflection1.name.endsWith("ResultResponse");
order ||= +reflection2.name.endsWith("Result") - +reflection1.name.endsWith("Result");
order ||= +reflection2.name.endsWith("Notification") - +reflection1.name.endsWith("Notification");
order ||= +reflection2.name.endsWith("NotificationParams") - +reflection1.name.endsWith("NotificationParams");
}
order ||= reflection1.name.localeCompare(reflection2.name);
return order;
}
/**
* @param {typedoc.DeclarationReflection} reflection
* @returns {string | undefined}
*/
function getReflectionCategory(reflection) {
const categoryTag = reflection.comment?.getTag("@category");
return categoryTag ? categoryTag.content.map((part) => part.text).join(" ") : undefined;
}
/**
* @param {string} category
* @returns {boolean}
*/
function isRpcMethodCategory(category) {
return /^`[a-z]/.test(category);
}
/**
* @param {string} category
* @param {typedoc.PageEvent<typedoc.DeclarationReflection>[]} events
* @param {typedoc.DefaultTheme} theme
* @returns {string}
*/
function renderCategory(category, events, theme) {
const categoryEvents = events.filter((event) => getReflectionCategory(event.model) === category);
if (categoryEvents.length === 0) {
throw new Error(`Invalid category: ${category}`);
}
return categoryEvents.
sort((event1, event2) => getReflectionOrder(category, event1.model, event2.model)).
map((event) => renderReflection(event.model, theme.getRenderContext(event))).
join("\n");
}
/**
* @param {typedoc.DeclarationReflection} reflection
* @param {typedoc.DefaultThemeRenderContext} context
* @returns {string}
*/
function renderReflection(reflection, context) {
const name = reflection.getFriendlyFullName();
const members = reflection.children ?? [];
const codeBlock = context.reflectionPreview(reflection);
let content = codeBlock ?
// Interfaces/classes: render preview, summary, members, then block tags (e.g., `@example`).
renderJsxElements(
codeBlock,
context.commentSummary(reflection),
context.commentTags(reflection),
members.map(member => context.member(member)),
) :
// Type aliases: `memberDeclaration` handles signature, summary, and block tags internally.
renderJsxElements(
context.memberDeclaration(reflection),
members.map(member => context.member(member)),
);
// Use cheerio for robust HTML transformations
const $ = cheerio.load(content, { xml: { decodeEntities: false } });
// Wrap `@example` blocks in `<details>` elements for collapsibility, move
// `id` to first element of hidden content so browser auto-expands on fragment
// navigation, and prefix `id` with the reflection's anchor for namespacing.
$(".tsd-tag-example").each((_, el) => {
const h4 = $(el).children("h4:first-child")[0];
const namespacedId = `${context.getAnchor(reflection)}-${h4.attribs.id}`;
$(h4).removeAttr("id");
$(h4).next().attr("id", namespacedId);
$(h4).find("a.tsd-anchor-icon").attr("href", `#${namespacedId}`);
h4.tagName = "summary";
el.tagName = "details";
});
// Convert `<hN>` elements to `<div data-typedoc-h="N">`.
$("h1,h2,h3,h4,h5,h6").each((_, el) => {
$(el).attr("data-typedoc-h", el.tagName[1]);
el.tagName = "div";
});
// Remove ids for `@see` blocks because they are not unique
$('[id="see"]').removeAttr("id");
// Copy member type signature text into its heading anchor, and remove the
// signature (which is redundant with the overall type signature).
$(".tsd-member").each((_, el) => {
const anchor = $(el).find(".tsd-anchor-link");
const signature = $(el).find(".tsd-signature");
// Clean up signature text for complex member types.
const signatureText = signature.text().
replaceAll("\u00A0", " ").
replace(/;}$/, "; }");
// Remove "Optional" tags in favor of signature text.
anchor.find(".tsd-tag").remove();
anchor.find("span:first-child").text(signatureText);
signature.remove();
});
// Serialize back to HTML
content = $.html();
// Reduce code block indent from 4 spaces to 2 spaces.
content = content.replaceAll("\u00A0\u00A0", "\u00A0");
// Accommodate Mintlify's janky Markdown parser.
content = content.
replaceAll("\u00A0", " "). // Encode valid UTF-8 character as HTML entity
replaceAll(/\n+</g, " <"). // Newlines around tags are not significant
replaceAll( // Treat special characters inside HTML as literal text, not Markdown
MARKDOWN_SPECIAL_CHARS_REGEX,
char => MARKDOWN_SPECIAL_CHARS_HTML_ENTITIES[char]
);
return `<div class="type">\n\n### \`${name}\`\n\n${content}\n</div>\n\n`;
}
/**
* @param {typedoc.JSX.Children[]} elements
*/
function renderJsxElements(...elements) {
return typedoc.JSX.renderElement(typedoc.JSX.createElement(typedoc.JSX.Fragment, null, elements));
}