File tree Expand file tree Collapse file tree 4 files changed +31
-17
lines changed Expand file tree Collapse file tree 4 files changed +31
-17
lines changed Original file line number Diff line number Diff line change @@ -2,9 +2,11 @@ const fs = require('fs');
22const lunr = require ( 'lunr' ) ;
33
44const args = process . argv . slice ( 2 ) ;
5- const destination = args [ 0 ] ?? "." ;
5+ const destination = args [ 0 ] === undefined
6+ ? "./docs"
7+ : args [ 0 ] ;
68
7- const data = JSON . parse ( fs . readFileSync ( `${ destination } /docs/ offline-search-index.json` ) ) ;
9+ const data = JSON . parse ( fs . readFileSync ( `${ destination } /offline-search-index.json` ) ) ;
810
911const idx = lunr ( function ( ) {
1012 this . ref ( 'ref' ) ;
@@ -14,7 +16,7 @@ const idx = lunr(function () {
1416 this . field ( 'description' , { boost : 2 } ) ;
1517 this . field ( 'body' ) ;
1618
17- data . forEach ( ( doc ) => { let docToAdd ;
19+ data . forEach ( ( doc ) => {
1820 if ( doc
1921 && doc . ref !== undefined
2022 && ! doc . ref . includes ( '/_shared/' )
@@ -24,10 +26,10 @@ const idx = lunr(function () {
2426 } ) ;
2527} ) ;
2628
27- fs . writeFileSync ( `${ destination } /assets/json/ lunr-index.json` , JSON . stringify ( idx ) ) ;
29+ fs . writeFileSync ( `${ destination } /lunr-index.json` , JSON . stringify ( idx ) ) ;
2830
2931// check if file got created
30- if ( ! fs . existsSync ( `${ destination } /assets/json/ lunr-index.json` ) ) {
32+ if ( ! fs . existsSync ( `${ destination } /lunr-index.json` ) ) {
3133 console . error ( 'Failed to create lunr index' ) ;
3234 process . exit ( 1 ) ;
3335}
Original file line number Diff line number Diff line change 2828
2929 if ( window . Worker ) {
3030 worker = new Worker ( '/js/worker.js' ) ;
31- const url = '/json/ lunr-index.json' ;
31+ const url = '/lunr-index.json' ;
3232
33- worker . postMessage ( {
34- type : 'init' ,
35- lunrIndexUrl : url ,
36- rawIndexUrl : $searchInput . data ( 'offline-search-index-json-src' )
33+ worker . postMessage ( {
34+ type : 'init' ,
35+ currentPath : window . location . pathname ,
36+ lunrIndexUrl : url ,
37+ rawIndexUrl : $searchInput . data ( 'offline-search-index-json-src' )
3738 } ) ;
3839
3940 worker . onerror = function ( error ) {
142143 return ;
143144 }
144145
145- worker . postMessage ( { type : 'search' , query : searchQuery , maxResults : $targetSearchInput . data ( 'offline-search-max-results' ) } ) ;
146+ worker . postMessage ( {
147+ type : 'search' ,
148+ query : searchQuery ,
149+ maxResults : $targetSearchInput . data ( 'offline-search-max-results' )
150+ } ) ;
146151 } ;
147152
148153 //
Original file line number Diff line number Diff line change @@ -3,22 +3,29 @@ if (typeof importScripts === 'function') {
33}
44
55let idx ;
6+ const versionRegex = new RegExp ( "^\/docs\/([0-9\.]*|latest)\/" ) ;
67const resultDetails = new Map ( ) ; // Will hold the data for the search results (titles and summaries)
78let indexReadyPromise ;
89
910// Initialize the index
1011self . onmessage = async function ( event ) {
1112 if ( event . data . type === 'init' ) {
13+ const regexResults = versionRegex . exec ( event . data . currentPath ) ;
14+ const version = regexResults
15+ ? regexResults [ 1 ]
16+ : undefined ;
1217 indexReadyPromise = new Promise ( async ( resolve , reject ) => {
1318 try {
1419 const rawIndex = await fetch ( event . data . rawIndexUrl ) ;
1520 let json = await rawIndex . json ( ) ;
1621 json . forEach ( ( doc ) => {
17- resultDetails . set ( doc . ref , {
18- version : doc . version ,
19- title : doc . title ,
20- excerpt : doc . excerpt ,
21- } ) ;
22+ if ( version === undefined || doc . ref . startsWith ( version ) ) {
23+ resultDetails . set ( doc . ref , {
24+ version : doc . version ,
25+ title : doc . title ,
26+ excerpt : doc . excerpt ,
27+ } ) ;
28+ }
2229 } ) ;
2330
2431 const lunrIndex = await fetch ( event . data . lunrIndexUrl ) ;
@@ -51,6 +58,7 @@ self.onmessage = async function (event) {
5158 } ) ;
5259 } ) ;
5360 } )
61+ . filter ( ( result ) => resultDetails . has ( result . ref ) )
5462 . slice ( 0 , event . data . maxResults ) ;
5563
5664 const docs = new Map ( ) ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments