Skip to content

Commit

Permalink
feat: Generate search index before rendering theme (#1252)
Browse files Browse the repository at this point in the history
* generate json file for index search
* serialize search index for lunr

Co-authored-by: sgrishchenko <sgrishchenko@spotware.com>
  • Loading branch information
sgrishchenko and sgrishchenko committed Apr 5, 2020
1 parent c296503 commit c73eaaf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -24,6 +24,7 @@
"handlebars": "^4.7.3",
"highlight.js": "^9.18.1",
"lodash": "^4.17.15",
"lunr": "^2.3.8",
"marked": "0.8.0",
"minimatch": "^3.0.0",
"progress": "^2.0.3",
Expand All @@ -36,6 +37,7 @@
"devDependencies": {
"@types/fs-extra": "^8.1.0",
"@types/lodash": "^4.14.149",
"@types/lunr": "^2.3.3",
"@types/marked": "^0.7.3",
"@types/minimatch": "3.0.3",
"@types/mocha": "^7.0.2",
Expand Down
25 changes: 19 additions & 6 deletions src/lib/output/plugins/JavascriptIndexPlugin.ts
@@ -1,4 +1,5 @@
import * as Path from 'path';
import { Builder, trimmer } from 'lunr';

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

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

writeFile(fileName, data, false);
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);
}
}

0 comments on commit c73eaaf

Please sign in to comment.