/
DefaultTheme.ts
581 lines (529 loc) · 18.8 KB
/
DefaultTheme.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import * as Path from "path";
import * as FS from "fs";
import { Theme } from "../theme";
import { Renderer } from "../renderer";
import {
Reflection,
ReflectionKind,
ProjectReflection,
ContainerReflection,
DeclarationReflection,
} from "../../models/reflections/index";
import { ReflectionGroup } from "../../models/ReflectionGroup";
import { UrlMapping } from "../models/UrlMapping";
import { NavigationItem } from "../models/NavigationItem";
import { RendererEvent } from "../events";
/**
* Defines a mapping of a [[Models.Kind]] to a template file.
*
* Used by [[DefaultTheme]] to map reflections to output files.
*/
export interface TemplateMapping {
/**
* [[DeclarationReflection.kind]] this rule applies to.
*/
kind: ReflectionKind[];
/**
* Can this mapping have children or should all further reflections be rendered
* to the defined output page?
*/
isLeaf: boolean;
/**
* The name of the directory the output files should be written to.
*/
directory: string;
/**
* The name of the template that should be used to render the reflection.
*/
template: string;
}
/**
* Default theme implementation of TypeDoc. If a theme does not provide a custom
* [[BaseTheme]] implementation, this theme class will be used.
*/
export class DefaultTheme extends Theme {
/**
* Mappings of reflections kinds to templates used by this theme.
*/
static MAPPINGS: TemplateMapping[] = [
{
kind: [ReflectionKind.Class],
isLeaf: false,
directory: "classes",
template: "reflection.hbs",
},
{
kind: [ReflectionKind.Interface],
isLeaf: false,
directory: "interfaces",
template: "reflection.hbs",
},
{
kind: [ReflectionKind.Enum],
isLeaf: false,
directory: "enums",
template: "reflection.hbs",
},
{
kind: [ReflectionKind.Namespace, ReflectionKind.Module],
isLeaf: false,
directory: "modules",
template: "reflection.hbs",
},
];
static URL_PREFIX = /^(http|ftp)s?:\/\//;
/**
* Create a new DefaultTheme instance.
*
* @param renderer The renderer this theme is attached to.
* @param basePath The base path of this theme.
*/
constructor(renderer: Renderer, basePath: string) {
super(renderer, basePath);
this.listenTo(
renderer,
RendererEvent.BEGIN,
this.onRendererBegin,
1024
);
}
/**
* Test whether the given path contains a documentation generated by this theme.
*
* @param path The path of the directory that should be tested.
* @returns TRUE if the given path seems to be a previous output directory,
* otherwise FALSE.
*/
isOutputDirectory(path: string): boolean {
if (!FS.existsSync(Path.join(path, "index.html"))) {
return false;
}
if (!FS.existsSync(Path.join(path, "assets"))) {
return false;
}
if (!FS.existsSync(Path.join(path, "assets", "js", "main.js"))) {
return false;
}
if (!FS.existsSync(Path.join(path, "assets", "images", "icons.png"))) {
return false;
}
return true;
}
/**
* Map the models of the given project to the desired output files.
*
* @param project The project whose urls should be generated.
* @returns A list of [[UrlMapping]] instances defining which models
* should be rendered to which files.
*/
getUrls(project: ProjectReflection): UrlMapping[] {
const urls: UrlMapping[] = [];
if (this.application.options.getValue("readme") === "none") {
project.url = "index.html";
urls.push(new UrlMapping("index.html", project, "reflection.hbs"));
} else {
project.url = "modules.html";
urls.push(
new UrlMapping("modules.html", project, "reflection.hbs")
);
urls.push(new UrlMapping("index.html", project, "index.hbs"));
}
project.children?.forEach((child: Reflection) => {
if (child instanceof DeclarationReflection) {
DefaultTheme.buildUrls(child, urls);
}
});
return urls;
}
/**
* Create a navigation structure for the given project.
*
* @param project The project whose navigation should be generated.
* @returns The root navigation item.
*/
getNavigation(project: ProjectReflection): NavigationItem {
const builder = new NavigationBuilder(
project,
project,
this.application.options.getValue("entryPoints").length > 1
);
return builder.build(
this.application.options.getValue("readme") !== "none"
);
}
/**
* Triggered before the renderer starts rendering a project.
*
* @param event An event object describing the current render operation.
*/
private onRendererBegin(event: RendererEvent) {
if (event.project.groups) {
event.project.groups.forEach(DefaultTheme.applyGroupClasses);
}
for (const id in event.project.reflections) {
const reflection = event.project.reflections[id];
if (reflection instanceof DeclarationReflection) {
DefaultTheme.applyReflectionClasses(reflection);
}
if (
reflection instanceof ContainerReflection &&
reflection.groups
) {
reflection.groups.forEach(DefaultTheme.applyGroupClasses);
}
}
}
/**
* Return a url for the given reflection.
*
* @param reflection The reflection the url should be generated for.
* @param relative The parent reflection the url generation should stop on.
* @param separator The separator used to generate the url.
* @returns The generated url.
*/
static getUrl(
reflection: Reflection,
relative?: Reflection,
separator = "."
): string {
let url = reflection.getAlias();
if (
reflection.parent &&
reflection.parent !== relative &&
!(reflection.parent instanceof ProjectReflection)
) {
url =
DefaultTheme.getUrl(reflection.parent, relative, separator) +
separator +
url;
}
return url;
}
/**
* Return the template mapping for the given reflection.
*
* @param reflection The reflection whose mapping should be resolved.
* @returns The found mapping or undefined if no mapping could be found.
*/
static getMapping(
reflection: DeclarationReflection
): TemplateMapping | undefined {
return DefaultTheme.MAPPINGS.find((mapping) =>
reflection.kindOf(mapping.kind)
);
}
/**
* Build the url for the the given reflection and all of its children.
*
* @param reflection The reflection the url should be created for.
* @param urls The array the url should be appended to.
* @returns The altered urls array.
*/
static buildUrls(
reflection: DeclarationReflection,
urls: UrlMapping[]
): UrlMapping[] {
const mapping = DefaultTheme.getMapping(reflection);
if (mapping) {
if (
!reflection.url ||
!DefaultTheme.URL_PREFIX.test(reflection.url)
) {
const url = [
mapping.directory,
DefaultTheme.getUrl(reflection) + ".html",
].join("/");
urls.push(new UrlMapping(url, reflection, mapping.template));
reflection.url = url;
reflection.hasOwnDocument = true;
}
for (const child of reflection.children || []) {
if (mapping.isLeaf) {
DefaultTheme.applyAnchorUrl(child, reflection);
} else {
DefaultTheme.buildUrls(child, urls);
}
}
} else if (reflection.parent) {
DefaultTheme.applyAnchorUrl(reflection, reflection.parent);
}
return urls;
}
/**
* Generate an anchor url for the given reflection and all of its children.
*
* @param reflection The reflection an anchor url should be created for.
* @param container The nearest reflection having an own document.
*/
static applyAnchorUrl(reflection: Reflection, container: Reflection) {
if (!reflection.url || !DefaultTheme.URL_PREFIX.test(reflection.url)) {
const anchor = DefaultTheme.getUrl(reflection, container, ".");
reflection.url = container.url + "#" + anchor;
reflection.anchor = anchor;
reflection.hasOwnDocument = false;
}
reflection.traverse((child) => {
if (child instanceof DeclarationReflection) {
DefaultTheme.applyAnchorUrl(child, container);
}
});
}
/**
* Generate the css classes for the given reflection and apply them to the
* [[DeclarationReflection.cssClasses]] property.
*
* @param reflection The reflection whose cssClasses property should be generated.
*/
static applyReflectionClasses(reflection: DeclarationReflection) {
const classes: string[] = [];
let kind: string;
if (reflection.kind === ReflectionKind.Accessor) {
if (!reflection.getSignature) {
classes.push("tsd-kind-set-signature");
} else if (!reflection.setSignature) {
classes.push("tsd-kind-get-signature");
} else {
classes.push("tsd-kind-accessor");
}
} else {
kind = ReflectionKind[reflection.kind];
classes.push(DefaultTheme.toStyleClass("tsd-kind-" + kind));
}
if (
reflection.parent &&
reflection.parent instanceof DeclarationReflection
) {
kind = ReflectionKind[reflection.parent.kind];
classes.push(DefaultTheme.toStyleClass(`tsd-parent-kind-${kind}`));
}
let hasTypeParameters = !!reflection.typeParameters;
reflection.getAllSignatures().forEach((signature) => {
hasTypeParameters = hasTypeParameters || !!signature.typeParameters;
});
if (hasTypeParameters) {
classes.push("tsd-has-type-parameter");
}
if (reflection.overwrites) {
classes.push("tsd-is-overwrite");
}
if (reflection.inheritedFrom) {
classes.push("tsd-is-inherited");
}
if (reflection.flags.isPrivate) {
classes.push("tsd-is-private");
}
if (reflection.flags.isProtected) {
classes.push("tsd-is-protected");
}
if (reflection.flags.isStatic) {
classes.push("tsd-is-static");
}
if (reflection.flags.isExternal) {
classes.push("tsd-is-external");
}
reflection.cssClasses = classes.join(" ");
}
/**
* Generate the css classes for the given reflection group and apply them to the
* [[ReflectionGroup.cssClasses]] property.
*
* @param group The reflection group whose cssClasses property should be generated.
*/
static applyGroupClasses(group: ReflectionGroup) {
const classes: string[] = [];
if (group.allChildrenAreInherited) {
classes.push("tsd-is-inherited");
}
if (group.allChildrenArePrivate) {
classes.push("tsd-is-private");
}
if (group.allChildrenAreProtectedOrPrivate) {
classes.push("tsd-is-private-protected");
}
if (group.allChildrenAreExternal) {
classes.push("tsd-is-external");
}
group.cssClasses = classes.join(" ");
}
/**
* Transform a space separated string into a string suitable to be used as a
* css class, e.g. "constructor method" > "Constructor-method".
*/
static toStyleClass(str: string) {
return str
.replace(/(\w)([A-Z])/g, (_m, m1, m2) => m1 + "-" + m2)
.toLowerCase();
}
}
export class NavigationBuilder {
constructor(
private project: ProjectReflection,
private entryPoint: ContainerReflection,
private multipleEntryPoints: boolean
) {}
/**
* Build the navigation structure.
*
* @param hasReadmeFile True if the project has a readme
* @returns The root node of the generated navigation structure.
*/
build(hasReadmeFile: boolean): NavigationItem {
const root = new NavigationItem("Index", "index.html");
const sidebarRoot = new NavigationItem(
this.multipleEntryPoints ? "Modules" : "Exports",
hasReadmeFile ? "modules.html" : "index.html",
root
);
sidebarRoot.isModules = true;
const modules: DeclarationReflection[] = [];
this.project
.getReflectionsByKind(ReflectionKind.SomeModule)
.forEach((someModule) => {
let target = someModule.parent;
let inScope = someModule === this.entryPoint;
while (target) {
if (target.kindOf(ReflectionKind.Module)) {
return;
}
if (this.entryPoint === target) {
inScope = true;
}
target = target.parent;
}
if (inScope && someModule instanceof DeclarationReflection) {
modules.push(someModule);
}
});
if (modules.length < 10) {
this.buildGroups(modules, root);
} else {
this.buildGroups(
this.entryPoint.getChildrenByKind(ReflectionKind.SomeModule),
root,
true
);
}
return root;
}
/**
* Create navigation nodes for the given list of reflections. The resulting nodes will be grouped into
* an "internal" and an "external" section when applicable.
*
* @param reflections The list of reflections which should be transformed into navigation nodes.
* @param parent The parent NavigationItem of the newly created nodes.
* @param buildChildren Whether navigation nodes should also be built for the children of each reflection.
*/
protected buildGroups(
reflections: DeclarationReflection[],
parent: NavigationItem,
buildChildren = false
) {
let state = -1;
const hasExternals = this.containsExternals(reflections);
this.sortReflections(reflections);
reflections.forEach((reflection) => {
if (hasExternals && !reflection.flags.isExternal && state !== 1) {
new NavigationItem(
"Internals",
undefined,
parent,
"tsd-is-external"
);
state = 1;
} else if (
hasExternals &&
reflection.flags.isExternal &&
state !== 2
) {
new NavigationItem(
"Externals",
undefined,
parent,
"tsd-is-external"
);
state = 2;
}
const item = NavigationItem.create(reflection, parent);
this.includeDedicatedUrls(reflection, item);
if (buildChildren) {
this.buildChildren(reflection, item);
}
});
}
/**
* Create navigation nodes for all container children of the given reflection.
*
* @param reflection The reflection whose children modules should be transformed into navigation nodes.
* @param parent The parent NavigationItem of the newly created nodes.
*/
protected buildChildren(
reflection: DeclarationReflection,
parent: NavigationItem
) {
const modules = reflection.getChildrenByKind(ReflectionKind.SomeModule);
modules.sort((a: DeclarationReflection, b: DeclarationReflection) => {
return a.getFullName() < b.getFullName() ? -1 : 1;
});
modules.forEach((reflection) => {
const item = NavigationItem.create(reflection, parent);
this.includeDedicatedUrls(reflection, item);
this.buildChildren(reflection, item);
});
}
/**
* Test whether the given list of modules contains an external module.
*
* @param modules The list of modules to test.
* @returns TRUE if any of the modules is marked as being external.
*/
protected containsExternals(modules: DeclarationReflection[]): boolean {
for (let index = 0, length = modules.length; index < length; index++) {
if (modules[index].flags.isExternal) {
return true;
}
}
return false;
}
/**
* Sort the given list of modules by name, groups external modules at the bottom.
*
* @param modules The list of modules that should be sorted.
*/
protected sortReflections(modules: DeclarationReflection[]) {
modules.sort((a: DeclarationReflection, b: DeclarationReflection) => {
if (a.flags.isExternal && !b.flags.isExternal) {
return 1;
}
if (!a.flags.isExternal && b.flags.isExternal) {
return -1;
}
return a.getFullName() < b.getFullName() ? -1 : 1;
});
}
/**
* Find the urls of all children of the given reflection and store them as dedicated urls
* of the given NavigationItem.
*
* @param reflection The reflection whose children urls should be included.
* @param item The navigation node whose dedicated urls should be set.
*/
protected includeDedicatedUrls(
reflection: DeclarationReflection,
item: NavigationItem
) {
(function walk(reflection: DeclarationReflection) {
for (const child of reflection.children || []) {
if (
child.hasOwnDocument &&
!child.kindOf(ReflectionKind.SomeModule)
) {
if (!item.dedicatedUrls) {
item.dedicatedUrls = [];
}
item.dedicatedUrls.push(child.url!);
walk(child);
}
}
})(reflection);
}
}