@@ -14,63 +14,59 @@ type SearchHit = {
1414
1515let search : ( q : string ) => SearchHit [ ]
1616
17- async function loadIndex ( ) {
17+ async function loadIndex ( { lunrLanguages } : { lunrLanguages ?: string [ ] } ) {
1818 const { index, data } = await loadIndexCore ( )
1919 search = q => index . search ( q ) . map ( ( { ref } ) => data [ ref ] )
2020 postMessage ( { e : 'index-ready' } )
21- }
22-
23- async function loadIndexCore ( ) {
24- const res = await fetch ( '../index.json' )
25- const etag = res . headers . get ( 'etag' )
26- const data = await res . json ( ) as { [ key : string ] : SearchHit }
27- const cache = createStore ( 'docfx' , 'lunr' )
2821
29- const { lunrLanguages, configureLunr } = await import ( './main.js' ) . then ( m => m . default ) as DocfxOptions
22+ async function loadIndexCore ( ) {
23+ const res = await fetch ( '../index.json' )
24+ const etag = res . headers . get ( 'etag' )
25+ const data = await res . json ( ) as { [ key : string ] : SearchHit }
26+ const cache = createStore ( 'docfx' , 'lunr' )
3027
31- if ( lunrLanguages && lunrLanguages . length > 0 ) {
32- multi ( lunr )
33- stemmer ( lunr )
34- await Promise . all ( lunrLanguages . map ( initLanguage ) )
35- }
28+ if ( lunrLanguages && lunrLanguages . length > 0 ) {
29+ multi ( lunr )
30+ stemmer ( lunr )
31+ await Promise . all ( lunrLanguages . map ( initLanguage ) )
32+ }
3633
37- if ( etag ) {
38- const value = JSON . parse ( await get ( 'index' , cache ) || '{}' )
39- if ( value && value . etag === etag ) {
40- return { index : lunr . Index . load ( value ) , data }
34+ if ( etag ) {
35+ const value = JSON . parse ( await get ( 'index' , cache ) || '{}' )
36+ if ( value && value . etag === etag ) {
37+ return { index : lunr . Index . load ( value ) , data }
38+ }
4139 }
42- }
4340
44- const index = lunr ( function ( ) {
45- lunr . tokenizer . separator = / [ \s \- . ( ) ] + /
41+ const index = lunr ( function ( ) {
42+ lunr . tokenizer . separator = / [ \s \- . ( ) ] + /
4643
47- this . ref ( 'href' )
48- this . field ( 'title' , { boost : 50 } )
49- this . field ( 'keywords' , { boost : 20 } )
44+ this . ref ( 'href' )
45+ this . field ( 'title' , { boost : 50 } )
46+ this . field ( 'keywords' , { boost : 20 } )
5047
51- if ( lunrLanguages && lunrLanguages . length > 0 ) {
52- this . use ( lunr . multiLanguage ( ...lunrLanguages ) )
53- }
48+ if ( lunrLanguages && lunrLanguages . length > 0 ) {
49+ this . use ( lunr . multiLanguage ( ...lunrLanguages ) )
50+ }
5451
55- configureLunr ?.( this )
52+ for ( const key in data ) {
53+ this . add ( data [ key ] )
54+ }
55+ } )
5656
57- for ( const key in data ) {
58- this . add ( data [ key ] )
57+ if ( etag ) {
58+ await set ( 'index' , JSON . stringify ( Object . assign ( index . toJSON ( ) , { etag } ) ) , cache )
5959 }
60- } )
6160
62- if ( etag ) {
63- await set ( 'index' , JSON . stringify ( Object . assign ( index . toJSON ( ) , { etag } ) ) , cache )
61+ return { index, data }
6462 }
65-
66- return { index, data }
6763}
6864
69- loadIndex ( ) . catch ( console . error )
70-
7165onmessage = function ( e ) {
72- if ( search ) {
66+ if ( e . data . q && search ) {
7367 postMessage ( { e : 'query-ready' , d : search ( e . data . q ) } )
68+ } else if ( e . data . init ) {
69+ loadIndex ( e . data . init ) . catch ( console . error )
7470 }
7571}
7672
0 commit comments