From f744f93d7d0f9bf4338e72f3a9b0b90830f18403 Mon Sep 17 00:00:00 2001 From: sgrishchenko Date: Sun, 29 Mar 2020 18:00:15 +0300 Subject: [PATCH 1/2] generate json file for index search --- src/lib/output/plugins/JavascriptIndexPlugin.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib/output/plugins/JavascriptIndexPlugin.ts b/src/lib/output/plugins/JavascriptIndexPlugin.ts index 05032ed7f..fd6595833 100644 --- a/src/lib/output/plugins/JavascriptIndexPlugin.ts +++ b/src/lib/output/plugins/JavascriptIndexPlugin.ts @@ -66,12 +66,18 @@ export class JavascriptIndexPlugin extends RendererComponent { rows.push(row); } - const fileName = Path.join(event.outputDirectory, 'assets', 'js', 'search.js'); - const data = - `var typedoc = typedoc || {}; + const jsFileName = Path.join(event.outputDirectory, 'assets', 'js', 'search.js'); + const jsData = + `console.warn('[typedoc] search.js file is deprecated. Please, use search.json file in your custom theme instead.'); + var typedoc = typedoc || {}; typedoc.search = typedoc.search || {}; typedoc.search.data = ${JSON.stringify({kinds: kinds, rows: rows})};`; - writeFile(fileName, data, false); + writeFile(jsFileName, jsData, false); + + const jsonFileName = Path.join(event.outputDirectory, 'assets', 'js', 'search.json'); + const jsonData = JSON.stringify({kinds: kinds, rows: rows}); + + writeFile(jsonFileName, jsonData, false); } } From eb42e86542a9c2dc33527ba53c61cbf8b4bfeb94 Mon Sep 17 00:00:00 2001 From: sgrishchenko Date: Wed, 1 Apr 2020 19:14:51 +0300 Subject: [PATCH 2/2] serialize search index for lunr --- package-lock.json | 6 +++++ package.json | 2 ++ .../output/plugins/JavascriptIndexPlugin.ts | 23 ++++++++++++------- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index caa755bb7..c5549c6a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -343,6 +343,12 @@ "integrity": "sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ==", "dev": true }, + "@types/lunr": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/@types/lunr/-/lunr-2.3.3.tgz", + "integrity": "sha512-09sXZZVsB3Ib41U0fC+O1O+4UOZT1bl/e+/QubPxpqDWHNEchvx/DEb1KJMOwq6K3MTNzZFoNSzVdR++o1DVnw==", + "dev": true + }, "@types/marked": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/@types/marked/-/marked-0.7.3.tgz", diff --git a/package.json b/package.json index 3dc7d8d92..f95cf6709 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/lib/output/plugins/JavascriptIndexPlugin.ts b/src/lib/output/plugins/JavascriptIndexPlugin.ts index fd6595833..62a232580 100644 --- a/src/lib/output/plugins/JavascriptIndexPlugin.ts +++ b/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'; @@ -66,17 +67,23 @@ export class JavascriptIndexPlugin extends RendererComponent { rows.push(row); } - const jsFileName = Path.join(event.outputDirectory, 'assets', 'js', 'search.js'); - const jsData = - `console.warn('[typedoc] search.js file is deprecated. Please, use search.json file in your custom theme instead.'); - 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(jsFileName, jsData, 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: kinds, rows: rows}); + const jsonData = JSON.stringify({ + kinds, + rows, + index + }); writeFile(jsonFileName, jsonData, false); }