@@ -24,7 +24,7 @@ lunr.Pipeline.registerFunction(docTrimmerFunction, 'docTrimmer')
24
24
25
25
window . addEventListener ( 'exdoc:loaded' , initialize )
26
26
27
- function initialize ( ) {
27
+ function initialize ( ) {
28
28
const pathname = window . location . pathname
29
29
if ( pathname . endsWith ( '/search.html' ) || pathname . endsWith ( '/search' ) ) {
30
30
const query = getQueryParamByName ( 'q' )
@@ -33,7 +33,7 @@ function initialize() {
33
33
}
34
34
}
35
35
36
- async function search ( value , queryType ) {
36
+ async function search ( value , queryType ) {
37
37
if ( isBlank ( value ) ) {
38
38
renderResults ( { value } )
39
39
} else {
@@ -44,6 +44,8 @@ async function search(value, queryType) {
44
44
const searchNodes = getSearchNodes ( )
45
45
46
46
if ( [ 'related' , 'latest' ] . includes ( queryType ) && searchNodes . length > 0 ) {
47
+ setSearchType ( queryType )
48
+
47
49
results = await remoteSearch ( value , queryType , searchNodes )
48
50
} else {
49
51
results = await localSearch ( value )
@@ -56,7 +58,7 @@ async function search(value, queryType) {
56
58
}
57
59
}
58
60
59
- async function localSearch ( value ) {
61
+ async function localSearch ( value ) {
60
62
const index = await getIndex ( )
61
63
62
64
// We cannot match on atoms :foo because that would be considered
@@ -65,7 +67,7 @@ async function localSearch(value) {
65
67
return searchResultsToDecoratedSearchItems ( index . search ( fixedValue ) )
66
68
}
67
69
68
- async function remoteSearch ( value , queryType , searchNodes ) {
70
+ async function remoteSearch ( value , queryType , searchNodes ) {
69
71
let filterNodes = searchNodes
70
72
71
73
if ( queryType === 'latest' ) {
@@ -107,13 +109,21 @@ async function remoteSearch(value, queryType, searchNodes) {
107
109
}
108
110
}
109
111
110
- function renderResults ( { value, results, errorMessage } ) {
112
+ function setSearchType ( value ) {
113
+ const searchTypes = Array . from ( document . getElementsByClassName ( 'search-type' ) )
114
+
115
+ searchTypes . forEach ( element => {
116
+ element . value = value
117
+ } )
118
+ }
119
+
120
+ function renderResults ( { value, results, errorMessage } ) {
111
121
const searchContainer = qs ( SEARCH_CONTAINER_SELECTOR )
112
122
const resultsHtml = searchResultsTemplate ( { value, results, errorMessage } )
113
123
searchContainer . innerHTML = resultsHtml
114
124
}
115
125
116
- async function getIndex ( ) {
126
+ async function getIndex ( ) {
117
127
const cachedIndex = await loadIndex ( )
118
128
if ( cachedIndex ) { return cachedIndex }
119
129
@@ -122,7 +132,7 @@ async function getIndex() {
122
132
return index
123
133
}
124
134
125
- async function loadIndex ( ) {
135
+ async function loadIndex ( ) {
126
136
try {
127
137
const serializedIndex = sessionStorage . getItem ( indexStorageKey ( ) )
128
138
if ( serializedIndex ) {
@@ -137,7 +147,7 @@ async function loadIndex() {
137
147
}
138
148
}
139
149
140
- async function saveIndex ( index ) {
150
+ async function saveIndex ( index ) {
141
151
try {
142
152
const serializedIndex = await compress ( index )
143
153
sessionStorage . setItem ( indexStorageKey ( ) , serializedIndex )
@@ -146,7 +156,7 @@ async function saveIndex(index) {
146
156
}
147
157
}
148
158
149
- async function compress ( index ) {
159
+ async function compress ( index ) {
150
160
const stream = new Blob ( [ JSON . stringify ( index ) ] , {
151
161
type : 'application/json'
152
162
} ) . stream ( ) . pipeThrough ( new window . CompressionStream ( 'gzip' ) )
@@ -156,7 +166,7 @@ async function compress(index) {
156
166
return b64encode ( buffer )
157
167
}
158
168
159
- async function decompress ( index ) {
169
+ async function decompress ( index ) {
160
170
const stream = new Blob ( [ b64decode ( index ) ] , {
161
171
type : 'application/json'
162
172
} ) . stream ( ) . pipeThrough ( new window . DecompressionStream ( 'gzip' ) )
@@ -165,7 +175,7 @@ async function decompress(index) {
165
175
return JSON . parse ( blob )
166
176
}
167
177
168
- function b64encode ( buffer ) {
178
+ function b64encode ( buffer ) {
169
179
let binary = ''
170
180
const bytes = new Uint8Array ( buffer )
171
181
const len = bytes . byteLength
@@ -175,7 +185,7 @@ function b64encode(buffer) {
175
185
return window . btoa ( binary )
176
186
}
177
187
178
- function b64decode ( str ) {
188
+ function b64decode ( str ) {
179
189
const binaryString = window . atob ( str )
180
190
const len = binaryString . length
181
191
const bytes = new Uint8Array ( new ArrayBuffer ( len ) )
@@ -185,11 +195,11 @@ function b64decode(str) {
185
195
return bytes
186
196
}
187
197
188
- function indexStorageKey ( ) {
198
+ function indexStorageKey ( ) {
189
199
return `idv5:${ getProjectNameAndVersion ( ) } `
190
200
}
191
201
192
- function createIndex ( ) {
202
+ function createIndex ( ) {
193
203
return lunr ( function ( ) {
194
204
this . ref ( 'ref' )
195
205
this . field ( 'title' , { boost : 3 } )
@@ -207,11 +217,11 @@ function createIndex() {
207
217
} )
208
218
}
209
219
210
- function docTokenSplitter ( builder ) {
220
+ function docTokenSplitter ( builder ) {
211
221
builder . pipeline . before ( lunr . stemmer , docTokenFunction )
212
222
}
213
223
214
- function docTokenFunction ( token ) {
224
+ function docTokenFunction ( token ) {
215
225
// If we have something with an arity, we split on : . to make partial
216
226
// matches easier. We split only when tokenizing, not when searching.
217
227
// Below we use ExDoc.Markdown.to_ast/2 as an example.
@@ -275,11 +285,11 @@ function docTokenFunction(token) {
275
285
return tokens
276
286
}
277
287
278
- function docTrimmer ( builder ) {
288
+ function docTrimmer ( builder ) {
279
289
builder . pipeline . before ( lunr . stemmer , docTrimmerFunction )
280
290
}
281
291
282
- function docTrimmerFunction ( token ) {
292
+ function docTrimmerFunction ( token ) {
283
293
// Preserve @ and : at the beginning of tokens,
284
294
// and ? and ! at the end of tokens. It needs to
285
295
// be done before stemming, otherwise search and
@@ -289,7 +299,7 @@ function docTrimmerFunction(token) {
289
299
} )
290
300
}
291
301
292
- function searchResultsToDecoratedSearchItems ( results ) {
302
+ function searchResultsToDecoratedSearchItems ( results ) {
293
303
return results
294
304
// If the docs are regenerated without changing its version,
295
305
// a reference may have been doc'ed false in the code but
@@ -306,11 +316,11 @@ function searchResultsToDecoratedSearchItems(results) {
306
316
} )
307
317
}
308
318
309
- function getSearchItemByRef ( ref ) {
319
+ function getSearchItemByRef ( ref ) {
310
320
return searchData . items . find ( searchItem => searchItem . ref === ref ) || null
311
321
}
312
322
313
- function getExcerpts ( searchItem , metadata ) {
323
+ function getExcerpts ( searchItem , metadata ) {
314
324
const { doc } = searchItem
315
325
const searchTerms = Object . keys ( metadata )
316
326
@@ -331,7 +341,7 @@ function getExcerpts(searchItem, metadata) {
331
341
return excerpts . slice ( 0 , 1 )
332
342
}
333
343
334
- function excerpt ( doc , sliceStart , sliceLength ) {
344
+ function excerpt ( doc , sliceStart , sliceLength ) {
335
345
const startPos = Math . max ( sliceStart - EXCERPT_RADIUS , 0 )
336
346
const endPos = Math . min ( sliceStart + sliceLength + EXCERPT_RADIUS , doc . length )
337
347
return [
0 commit comments