/
renderer.ts
385 lines (342 loc) · 13.1 KB
/
renderer.ts
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* Holds all logic used render and output the final documentation.
*
* The {@link Renderer} class is the central controller within this namespace. When invoked it creates
* an instance of {@link Theme} which defines the layout of the documentation and fires a
* series of {@link RendererEvent} events. Instances of {@link BasePlugin} can listen to these events and
* alter the generated output.
*/
import type * as ts from "typescript";
import * as fs from "fs";
import * as path from "path";
import type { Application } from "../application";
import type { Theme } from "./theme";
import { RendererEvent, PageEvent } from "./events";
import type { ProjectReflection } from "../models/reflections/project";
import type { UrlMapping } from "./models/UrlMapping";
import { remove, writeFileSync } from "../utils/fs";
import { DefaultTheme } from "./themes/default/DefaultTheme";
import { RendererComponent } from "./components";
import { Component, ChildableComponent } from "../utils/component";
import { BindOption, EventHooks } from "../utils";
import { loadHighlighter } from "../utils/highlighter";
import type { Theme as ShikiTheme } from "shiki";
import { Reflection } from "../models";
import type { JsxElement } from "../utils/jsx.elements";
import type { DefaultThemeRenderContext } from "./themes/default/DefaultThemeRenderContext";
/**
* Describes the hooks available to inject output in the default theme.
* If the available hooks don't let you put something where you'd like, please open an issue!
*/
export interface RendererHooks {
/**
* Applied immediately after the opening `<head>` tag.
*/
"head.begin": [DefaultThemeRenderContext];
/**
* Applied immediately before the closing `</head>` tag.
*/
"head.end": [DefaultThemeRenderContext];
/**
* Applied immediately after the opening `<body>` tag.
*/
"body.begin": [DefaultThemeRenderContext];
/**
* Applied immediately before the closing `</body>` tag.
*/
"body.end": [DefaultThemeRenderContext];
}
/**
* The renderer processes a {@link ProjectReflection} using a {@link Theme} instance and writes
* the emitted html documents to a output directory. You can specify which theme should be used
* using the `--theme <name>` command line argument.
*
* {@link Renderer} is a subclass of {@link EventDispatcher} and triggers a series of events while
* a project is being processed. You can listen to these events to control the flow or manipulate
* the output.
*
* * {@link Renderer.EVENT_BEGIN}<br>
* Triggered before the renderer starts rendering a project. The listener receives
* an instance of {@link RendererEvent}. By calling {@link RendererEvent.preventDefault} the entire
* render process can be canceled.
*
* * {@link Renderer.EVENT_BEGIN_PAGE}<br>
* Triggered before a document will be rendered. The listener receives an instance of
* {@link PageEvent}. By calling {@link PageEvent.preventDefault} the generation of the
* document can be canceled.
*
* * {@link Renderer.EVENT_END_PAGE}<br>
* Triggered after a document has been rendered, just before it is written to disc. The
* listener receives an instance of {@link PageEvent}. When calling
* {@link PageEvent.preventDefault} the the document will not be saved to disc.
*
* * {@link Renderer.EVENT_END}<br>
* Triggered after the renderer has written all documents. The listener receives
* an instance of {@link RendererEvent}.
*/
@Component({ name: "renderer", internal: true, childClass: RendererComponent })
export class Renderer extends ChildableComponent<
Application,
RendererComponent
> {
private themes = new Map<string, new (renderer: Renderer) => Theme>([
["default", DefaultTheme],
]);
private unknownSymbolResolvers = new Map<
string,
Array<(symbol: string) => string | undefined>
>();
/** @event */
static readonly EVENT_BEGIN_PAGE = PageEvent.BEGIN;
/** @event */
static readonly EVENT_END_PAGE = PageEvent.END;
/** @event */
static readonly EVENT_BEGIN = RendererEvent.BEGIN;
/** @event */
static readonly EVENT_END = RendererEvent.END;
/**
* The theme that is used to render the documentation.
*/
theme?: Theme;
/**
* Hooks which will be called when rendering pages.
* Note:
* - Hooks added during output will be discarded at the end of rendering.
* - Hooks added during a page render will be discarded at the end of that page's render.
*
* See {@link RendererHooks} for a description of each available hook, and when it will be called.
*/
hooks = new EventHooks<RendererHooks, JsxElement>();
/** @internal */
@BindOption("theme")
themeName!: string;
/** @internal */
@BindOption("cleanOutputDir")
cleanOutputDir!: boolean;
/** @internal */
@BindOption("gaID")
gaID!: string;
/** @internal */
@BindOption("gaSite")
gaSite!: string;
/** @internal */
@BindOption("githubPages")
githubPages!: boolean;
/** @internal */
@BindOption("hideGenerator")
hideGenerator!: boolean;
/** @internal */
@BindOption("lightHighlightTheme")
lightTheme!: ShikiTheme;
/** @internal */
@BindOption("darkHighlightTheme")
darkTheme!: ShikiTheme;
/**
* Define a new theme that can be used to render output.
* This API will likely be changing in TypeDoc 0.23.
* (sorry... changing as soon as it's introduced)
* As it is, it provides reasonable flexibility, but doesn't give users a sufficiently
* easy way to overwrite parts of a theme.
* @param name
* @param theme
*/
defineTheme(name: string, theme: new (renderer: Renderer) => Theme) {
if (this.themes.has(name)) {
throw new Error(`The theme "${name}" has already been defined.`);
}
this.themes.set(name, theme);
}
/**
* Adds a new resolver that the theme can used to try to figure out how to link to a symbol
* declared by a third-party library which is not included in the documentation.
* @param packageName the npm package name that this resolver can handle to limit which files it will be tried on.
* If the resolver will create links for Node builtin types, it should be set to `@types/node`.
* Links for builtin types live in the default lib files under `typescript`.
* @param resolver a function that will be called to create links for a given symbol name in the registered path.
* If the provided name is not contained within the docs, should return `undefined`.
* @since 0.22.0
*/
addUnknownSymbolResolver(
packageName: string,
resolver: (name: string) => string | undefined
) {
const existing = this.unknownSymbolResolvers.get(packageName);
if (existing) {
existing.push(resolver);
} else {
this.unknownSymbolResolvers.set(packageName, [resolver]);
}
}
/**
* Marked as internal for now. Using this requires the internal `getSymbol()` method on ReferenceType.
* Someday that needs to be fixed so that this can be made public. ReferenceTypes really shouldn't store
* symbols so that we don't need to keep the program around forever.
* @internal
*/
attemptExternalResolution(
symbol: ts.Symbol | undefined
): string | undefined {
const symbolPath = symbol?.declarations?.[0]
?.getSourceFile()
.fileName.replace(/\\/g, "/");
if (!symbolPath) {
return;
}
let startIndex = symbolPath.indexOf("node_modules/");
if (startIndex === -1) {
return;
}
startIndex += "node_modules/".length;
let stopIndex = symbolPath.indexOf("/", startIndex);
// Scoped package, e.g. `@types/node`
if (symbolPath[startIndex] === "@") {
stopIndex = symbolPath.indexOf("/", stopIndex + 1);
}
const packageName = symbolPath.substring(startIndex, stopIndex);
const resolvers = this.unknownSymbolResolvers.get(packageName);
for (const resolver of resolvers || []) {
const resolved = resolver(symbol!.name);
if (resolved) return resolved;
}
}
/**
* Render the given project reflection to the specified output directory.
*
* @param project The project that should be rendered.
* @param outputDirectory The path of the directory the documentation should be rendered to.
*/
async render(
project: ProjectReflection,
outputDirectory: string
): Promise<void> {
const momento = this.hooks.saveMomento();
const start = Date.now();
await loadHighlighter(this.lightTheme, this.darkTheme);
this.application.logger.verbose(
`Renderer: Loading highlighter took ${Date.now() - start}ms`
);
if (
!this.prepareTheme() ||
!(await this.prepareOutputDirectory(outputDirectory))
) {
return;
}
const output = new RendererEvent(
RendererEvent.BEGIN,
outputDirectory,
project
);
output.urls = this.theme!.getUrls(project);
this.trigger(output);
if (!output.isDefaultPrevented) {
output.urls.forEach((mapping: UrlMapping) => {
this.renderDocument(output.createPageEvent(mapping));
});
this.trigger(RendererEvent.END, output);
}
this.theme = void 0;
this.hooks.restoreMomento(momento);
}
/**
* Render a single page.
*
* @param page An event describing the current page.
* @return TRUE if the page has been saved to disc, otherwise FALSE.
*/
private renderDocument(page: PageEvent): boolean {
const momento = this.hooks.saveMomento();
this.trigger(PageEvent.BEGIN, page);
if (page.isDefaultPrevented) {
this.hooks.restoreMomento(momento);
return false;
}
if (page.model instanceof Reflection) {
page.contents = this.theme!.render(page as PageEvent<Reflection>);
} else {
throw new Error("Should be unreachable");
}
this.trigger(PageEvent.END, page);
this.hooks.restoreMomento(momento);
if (page.isDefaultPrevented) {
return false;
}
try {
writeFileSync(page.filename, page.contents);
} catch (error) {
this.application.logger.error(`Could not write ${page.filename}`);
return false;
}
return true;
}
/**
* Ensure that a theme has been setup.
*
* If a the user has set a theme we try to find and load it. If no theme has
* been specified we load the default theme.
*
* @returns TRUE if a theme has been setup, otherwise FALSE.
*/
private prepareTheme(): boolean {
if (!this.theme) {
const ctor = this.themes.get(this.themeName);
if (!ctor) {
this.application.logger.error(
`The theme '${
this.themeName
}' is not defined. The available themes are: ${[
...this.themes.keys(),
].join(", ")}`
);
return false;
} else {
this.theme = new ctor(this);
}
}
return true;
}
/**
* Prepare the output directory. If the directory does not exist, it will be
* created. If the directory exists, it will be emptied.
*
* @param directory The path to the directory that should be prepared.
* @returns TRUE if the directory could be prepared, otherwise FALSE.
*/
private async prepareOutputDirectory(directory: string): Promise<boolean> {
if (this.cleanOutputDir) {
try {
await remove(directory);
} catch (error) {
this.application.logger.warn(
"Could not empty the output directory."
);
return false;
}
}
try {
fs.mkdirSync(directory, { recursive: true });
} catch (error) {
this.application.logger.error(
`Could not create output directory ${directory}.`
);
return false;
}
if (this.githubPages) {
try {
const text =
"TypeDoc added this file to prevent GitHub Pages from " +
"using Jekyll. You can turn off this behavior by setting " +
"the `githubPages` option to false.";
fs.writeFileSync(path.join(directory, ".nojekyll"), text);
} catch (error) {
this.application.logger.warn(
"Could not create .nojekyll file."
);
return false;
}
}
return true;
}
}
// HACK: THIS HAS TO STAY DOWN HERE
// if you try to move it up to the top of the file, then you'll run into stuff being used before it has been defined.
import "./plugins";