Skip to content

Commit c73eaaf

Browse files
sgrishchenkosgrishchenko
andauthored
feat: Generate search index before rendering theme (#1252)
* generate json file for index search * serialize search index for lunr Co-authored-by: sgrishchenko <[email protected]>
1 parent c296503 commit c73eaaf

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"handlebars": "^4.7.3",
2525
"highlight.js": "^9.18.1",
2626
"lodash": "^4.17.15",
27+
"lunr": "^2.3.8",
2728
"marked": "0.8.0",
2829
"minimatch": "^3.0.0",
2930
"progress": "^2.0.3",
@@ -36,6 +37,7 @@
3637
"devDependencies": {
3738
"@types/fs-extra": "^8.1.0",
3839
"@types/lodash": "^4.14.149",
40+
"@types/lunr": "^2.3.3",
3941
"@types/marked": "^0.7.3",
4042
"@types/minimatch": "3.0.3",
4143
"@types/mocha": "^7.0.2",

src/lib/output/plugins/JavascriptIndexPlugin.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Path from 'path';
2+
import { Builder, trimmer } from 'lunr';
23

34
import { DeclarationReflection, ProjectReflection } from '../../models/reflections/index';
45
import { GroupPlugin } from '../../converter/plugins/GroupPlugin';
@@ -66,12 +67,24 @@ export class JavascriptIndexPlugin extends RendererComponent {
6667
rows.push(row);
6768
}
6869

69-
const fileName = Path.join(event.outputDirectory, 'assets', 'js', 'search.js');
70-
const data =
71-
`var typedoc = typedoc || {};
72-
typedoc.search = typedoc.search || {};
73-
typedoc.search.data = ${JSON.stringify({kinds: kinds, rows: rows})};`;
70+
const builder = new Builder();
71+
builder.pipeline.add(trimmer);
7472

75-
writeFile(fileName, data, false);
73+
builder.ref('id');
74+
builder.field('name', {boost: 10});
75+
builder.field('parent');
76+
77+
rows.forEach(row => builder.add(row));
78+
79+
const index = builder.build();
80+
81+
const jsonFileName = Path.join(event.outputDirectory, 'assets', 'js', 'search.json');
82+
const jsonData = JSON.stringify({
83+
kinds,
84+
rows,
85+
index
86+
});
87+
88+
writeFile(jsonFileName, jsonData, false);
7689
}
7790
}

0 commit comments

Comments
 (0)