1+ const fs = require ( 'fs' ) ;
2+ const lunr = require ( 'lunr' ) ;
3+
4+ /**
5+ * This script is used to generate a lunr index from the offline-search-index.json file.
6+ * Hugo must be built before running this script, as jt requires the offline-search-index.json file to have been generated.
7+ *
8+ * The script will output a lunr-index.json file in the content/static directory and the docs directory.
9+ */
10+
11+ const args = process . argv . slice ( 2 ) ;
12+
13+ // Arguments should only be provided from a pipeline build.
14+ const isFromPipeline = args [ 0 ] !== undefined ;
15+
16+ const source = isFromPipeline
17+ ? args [ 0 ]
18+ : "./docs" ;
19+
20+ const destination = isFromPipeline
21+ ? `${ args [ 0 ] } `
22+ : "./docs" ;
23+
24+ const data = JSON . parse ( fs . readFileSync ( `${ source } /offline-search-index.json` ) ) ;
25+
26+ const idx = lunr ( function ( ) {
27+ this . ref ( 'ref' ) ;
28+ this . field ( 'title' , { boost : 5 } ) ;
29+ this . field ( 'categories' , { boost : 3 } ) ;
30+ this . field ( 'tags' , { boost : 3 } ) ;
31+ this . field ( 'description' , { boost : 2 } ) ;
32+ this . field ( 'body' ) ;
33+
34+ data . forEach ( ( doc ) => {
35+ if ( doc
36+ && doc . ref !== undefined
37+ && ! doc . ref . includes ( '/_shared/' )
38+ ) {
39+ this . add ( doc ) ;
40+ }
41+ } ) ;
42+ } ) ;
43+
44+ if ( ! isFromPipeline ) {
45+ fs . writeFileSync ( `./content/static/lunr-index.json` , JSON . stringify ( idx ) ) ;
46+ }
47+
48+ fs . writeFileSync ( `${ destination } /lunr-index.json` , JSON . stringify ( idx ) ) ;
49+
50+ // check if file got created
51+ if ( ! fs . existsSync ( `${ destination } /lunr-index.json` ) ) {
52+ console . error ( 'Failed to create lunr index, hugo must be build using `hugo` command before running this script.' ) ;
53+ process . exit ( 1 ) ;
54+ }
0 commit comments