/
JavascriptIndexPlugin.ts
122 lines (103 loc) · 3.45 KB
/
JavascriptIndexPlugin.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
import * as Path from "path";
import { Builder, trimmer } from "lunr";
import {
DeclarationReflection,
ProjectReflection,
ReflectionKind,
} from "../../models";
import { GroupPlugin } from "../../converter/plugins";
import { Component, RendererComponent } from "../components";
import { RendererEvent } from "../events";
import { writeFileSync } from "../../utils";
import { DefaultTheme } from "../themes/default/DefaultTheme";
import type { IDocument } from "../themes/default/assets/typedoc/components/Search";
/**
* A plugin that exports an index of the project to a javascript file.
*
* The resulting javascript file can be used to build a simple search function.
*/
@Component({ name: "javascript-index" })
export class JavascriptIndexPlugin extends RendererComponent {
/**
* Create a new JavascriptIndexPlugin instance.
*/
override initialize() {
this.listenTo(this.owner, RendererEvent.BEGIN, this.onRendererBegin);
}
/**
* Triggered after a document has been rendered, just before it is written to disc.
*
* @param event An event object describing the current render operation.
*/
private onRendererBegin(event: RendererEvent) {
if (!(this.owner.theme instanceof DefaultTheme)) {
return;
}
if (event.isDefaultPrevented) {
return;
}
const rows: any[] = [];
const kinds: { [K in ReflectionKind]?: string } = {};
for (const reflection of event.project.getReflectionsByKind(
ReflectionKind.All
)) {
if (!(reflection instanceof DeclarationReflection)) {
continue;
}
if (
!reflection.url ||
!reflection.name ||
reflection.flags.isExternal ||
reflection.name === ""
) {
continue;
}
let parent = reflection.parent;
if (parent instanceof ProjectReflection) {
parent = undefined;
}
const row: IDocument = {
id: rows.length,
kind: reflection.kind,
name: reflection.name,
url: reflection.url,
classes: reflection.cssClasses,
categories: (reflection.categories ?? []).map(
(category) => category.title
),
};
if (parent) {
row.parent = parent.getFullName();
}
if (!kinds[reflection.kind]) {
kinds[reflection.kind] = GroupPlugin.getKindSingular(
reflection.kind
);
}
rows.push(row);
}
const builder = new Builder();
builder.pipeline.add(trimmer);
builder.ref("id");
builder.field("name", { boost: 10 });
builder.field("parent");
rows.forEach((row) => builder.add(row));
const index = builder.build();
const jsonFileName = Path.join(
event.outputDirectory,
"assets",
"search.js"
);
const searchConfig = this.application.options.getValue("search");
const jsonData = JSON.stringify({
searchConfig,
kinds,
rows,
index,
});
writeFileSync(
jsonFileName,
`window.searchData = JSON.parse(${JSON.stringify(jsonData)});`
);
}
}