/
JavascriptIndexPlugin.ts
90 lines (74 loc) · 2.73 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
import * as Path from 'path';
import { Builder, trimmer } from 'lunr';
import { DeclarationReflection, ProjectReflection } from '../../models/reflections/index';
import { GroupPlugin } from '../../converter/plugins/GroupPlugin';
import { Component, RendererComponent } from '../components';
import { writeFile } from '../../utils/fs';
import { RendererEvent } from '../events';
/**
* 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.
*/
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) {
const rows: any[] = [];
const kinds = {};
for (let key in event.project.reflections) {
const reflection: DeclarationReflection = <DeclarationReflection> event.project.reflections[key];
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: any = {
id: rows.length,
kind: reflection.kind,
name: reflection.name,
url: reflection.url,
classes: reflection.cssClasses
};
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', 'js', 'search.json');
const jsonData = JSON.stringify({
kinds,
rows,
index
});
writeFile(jsonFileName, jsonData, false);
}
}