/
DefaultTheme.tsx
315 lines (281 loc) · 11.2 KB
/
DefaultTheme.tsx
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
import { Theme } from "../../theme";
import type { Renderer } from "../../renderer";
import {
Reflection,
ReflectionKind,
ProjectReflection,
ContainerReflection,
DeclarationReflection,
SignatureReflection,
} from "../../../models";
import { RenderTemplate, UrlMapping } from "../../models/UrlMapping";
import { PageEvent, RendererEvent } from "../../events";
import type { MarkedPlugin } from "../../plugins";
import { DefaultThemeRenderContext } from "./DefaultThemeRenderContext";
import { JSX } from "../../../utils";
/**
* Defines a mapping of a {@link Models.Kind} to a template file.
*
* Used by {@link DefaultTheme} to map reflections to output files.
*/
interface TemplateMapping {
/**
* {@link DeclarationReflection.kind} this rule applies to.
*/
kind: ReflectionKind[];
/**
* 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: RenderTemplate<PageEvent<any>>;
}
/**
* Default theme implementation of TypeDoc. If a theme does not provide a custom
* {@link Theme} implementation, this theme class will be used.
*/
export class DefaultTheme extends Theme {
/** @internal */
markedPlugin: MarkedPlugin;
private _renderContext?: DefaultThemeRenderContext;
getRenderContext(_pageEvent: PageEvent<any>) {
if (!this._renderContext) {
this._renderContext = new DefaultThemeRenderContext(this, this.application.options);
}
return this._renderContext;
}
reflectionTemplate = (pageEvent: PageEvent<ContainerReflection>) => {
return this.getRenderContext(pageEvent).reflectionTemplate(pageEvent);
};
indexTemplate = (pageEvent: PageEvent<ProjectReflection>) => {
return this.getRenderContext(pageEvent).indexTemplate(pageEvent);
};
defaultLayoutTemplate = (pageEvent: PageEvent<Reflection>) => {
return this.getRenderContext(pageEvent).defaultLayout(pageEvent);
};
/**
* Mappings of reflections kinds to templates used by this theme.
*/
private mappings: TemplateMapping[] = [
{
kind: [ReflectionKind.Class],
directory: "classes",
template: this.reflectionTemplate,
},
{
kind: [ReflectionKind.Interface],
directory: "interfaces",
template: this.reflectionTemplate,
},
{
kind: [ReflectionKind.Enum],
directory: "enums",
template: this.reflectionTemplate,
},
{
kind: [ReflectionKind.Namespace, ReflectionKind.Module],
directory: "modules",
template: this.reflectionTemplate,
},
{
kind: [ReflectionKind.TypeAlias],
directory: "types",
template: this.reflectionTemplate,
},
{
kind: [ReflectionKind.Function],
directory: "functions",
template: this.reflectionTemplate,
},
{
kind: [ReflectionKind.Variable],
directory: "variables",
template: this.reflectionTemplate,
},
];
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) {
super(renderer);
this.markedPlugin = renderer.getComponent("marked") as MarkedPlugin;
this.listenTo(renderer, RendererEvent.BEGIN, this.onRendererBegin, 1024);
}
/**
* 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 {@link UrlMapping} instances defining which models
* should be rendered to which files.
*/
getUrls(project: ProjectReflection): UrlMapping[] {
const urls: UrlMapping[] = [];
if (false == hasReadme(this.application.options.getValue("readme"))) {
project.url = "index.html";
urls.push(new UrlMapping<ContainerReflection>("index.html", project, this.reflectionTemplate));
} else {
project.url = "modules.html";
urls.push(new UrlMapping<ContainerReflection>("modules.html", project, this.reflectionTemplate));
urls.push(new UrlMapping("index.html", project, this.indexTemplate));
}
project.children?.forEach((child: Reflection) => {
if (child instanceof DeclarationReflection) {
this.buildUrls(child, urls);
}
});
return urls;
}
/**
* Triggered before the renderer starts rendering a project.
*
* @param event An event object describing the current render operation.
*/
private onRendererBegin(event: RendererEvent) {
const filters = this.application.options.getValue("visibilityFilters") as Record<string, boolean>;
for (const reflection of Object.values(event.project.reflections)) {
if (reflection instanceof DeclarationReflection) {
DefaultTheme.applyReflectionClasses(reflection, filters);
}
}
}
/**
* 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.
*/
private getMapping(reflection: DeclarationReflection): TemplateMapping | undefined {
return this.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.
*/
buildUrls(reflection: DeclarationReflection, urls: UrlMapping[]): UrlMapping[] {
const mapping = this.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;
}
reflection.traverse((child) => {
if (child instanceof DeclarationReflection) {
this.buildUrls(child, urls);
} else {
DefaultTheme.applyAnchorUrl(child, reflection);
}
return true;
});
} else if (reflection.parent) {
DefaultTheme.applyAnchorUrl(reflection, reflection.parent);
}
return urls;
}
render(page: PageEvent<Reflection>): string {
const templateOutput = this.defaultLayoutTemplate(page);
return "<!DOCTYPE html>" + JSX.renderElement(templateOutput);
}
/**
* 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 instanceof DeclarationReflection) && !(reflection instanceof SignatureReflection)) {
return;
}
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) => {
DefaultTheme.applyAnchorUrl(child, container);
return true;
});
}
/**
* Generate the css classes for the given reflection and apply them to the
* {@link DeclarationReflection.cssClasses} property.
*
* @param reflection The reflection whose cssClasses property should be generated.
*/
static applyReflectionClasses(reflection: DeclarationReflection, filters: Record<string, boolean>) {
const classes: string[] = [];
classes.push(DefaultTheme.toStyleClass("tsd-kind-" + ReflectionKind[reflection.kind]));
if (reflection.parent && reflection.parent instanceof DeclarationReflection) {
classes.push(DefaultTheme.toStyleClass(`tsd-parent-kind-${ReflectionKind[reflection.parent.kind]}`));
}
// Filter classes should match up with the settings function in
// partials/navigation.tsx.
for (const key of Object.keys(filters)) {
if (key === "inherited") {
if (reflection.inheritedFrom) {
classes.push("tsd-is-inherited");
}
} else if (key === "protected") {
if (reflection.flags.isProtected) {
classes.push("tsd-is-protected");
}
} else if (key === "private") {
if (reflection.flags.isPrivate) {
classes.push("tsd-is-private");
}
} else if (key === "external") {
if (reflection.flags.isExternal) {
classes.push("tsd-is-external");
}
} else if (key.startsWith("@")) {
if (key === "@deprecated") {
if (reflection.isDeprecated()) {
classes.push(DefaultTheme.toStyleClass(`tsd-is-${key.substring(1)}`));
}
} else if (
reflection.comment?.hasModifier(key as `@${string}`) ||
reflection.comment?.getTag(key as `@${string}`)
) {
classes.push(DefaultTheme.toStyleClass(`tsd-is-${key.substring(1)}`));
}
}
}
reflection.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();
}
}
function hasReadme(readme: string) {
return !readme.endsWith("none");
}