@@ -24,7 +24,7 @@ lunr.Pipeline.registerFunction(docTrimmerFunction, 'docTrimmer')
2424
2525window . addEventListener ( 'exdoc:loaded' , initialize )
2626
27- function initialize ( ) {
27+ function initialize ( ) {
2828 const pathname = window . location . pathname
2929 if ( pathname . endsWith ( '/search.html' ) || pathname . endsWith ( '/search' ) ) {
3030 const query = getQueryParamByName ( 'q' )
@@ -33,7 +33,7 @@ function initialize() {
3333 }
3434}
3535
36- async function search ( value , queryType ) {
36+ async function search ( value , queryType ) {
3737 if ( isBlank ( value ) ) {
3838 renderResults ( { value } )
3939 } else {
@@ -44,6 +44,8 @@ async function search(value, queryType) {
4444 const searchNodes = getSearchNodes ( )
4545
4646 if ( [ 'related' , 'latest' ] . includes ( queryType ) && searchNodes . length > 0 ) {
47+ setSearchType ( queryType )
48+
4749 results = await remoteSearch ( value , queryType , searchNodes )
4850 } else {
4951 results = await localSearch ( value )
@@ -56,7 +58,7 @@ async function search(value, queryType) {
5658 }
5759}
5860
59- async function localSearch ( value ) {
61+ async function localSearch ( value ) {
6062 const index = await getIndex ( )
6163
6264 // We cannot match on atoms :foo because that would be considered
@@ -65,7 +67,7 @@ async function localSearch(value) {
6567 return searchResultsToDecoratedSearchItems ( index . search ( fixedValue ) )
6668}
6769
68- async function remoteSearch ( value , queryType , searchNodes ) {
70+ async function remoteSearch ( value , queryType , searchNodes ) {
6971 let filterNodes = searchNodes
7072
7173 if ( queryType === 'latest' ) {
@@ -107,13 +109,21 @@ async function remoteSearch(value, queryType, searchNodes) {
107109 }
108110}
109111
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 } ) {
111121 const searchContainer = qs ( SEARCH_CONTAINER_SELECTOR )
112122 const resultsHtml = searchResultsTemplate ( { value, results, errorMessage } )
113123 searchContainer . innerHTML = resultsHtml
114124}
115125
116- async function getIndex ( ) {
126+ async function getIndex ( ) {
117127 const cachedIndex = await loadIndex ( )
118128 if ( cachedIndex ) { return cachedIndex }
119129
@@ -122,7 +132,7 @@ async function getIndex() {
122132 return index
123133}
124134
125- async function loadIndex ( ) {
135+ async function loadIndex ( ) {
126136 try {
127137 const serializedIndex = sessionStorage . getItem ( indexStorageKey ( ) )
128138 if ( serializedIndex ) {
@@ -137,7 +147,7 @@ async function loadIndex() {
137147 }
138148}
139149
140- async function saveIndex ( index ) {
150+ async function saveIndex ( index ) {
141151 try {
142152 const serializedIndex = await compress ( index )
143153 sessionStorage . setItem ( indexStorageKey ( ) , serializedIndex )
@@ -146,7 +156,7 @@ async function saveIndex(index) {
146156 }
147157}
148158
149- async function compress ( index ) {
159+ async function compress ( index ) {
150160 const stream = new Blob ( [ JSON . stringify ( index ) ] , {
151161 type : 'application/json'
152162 } ) . stream ( ) . pipeThrough ( new window . CompressionStream ( 'gzip' ) )
@@ -156,7 +166,7 @@ async function compress(index) {
156166 return b64encode ( buffer )
157167}
158168
159- async function decompress ( index ) {
169+ async function decompress ( index ) {
160170 const stream = new Blob ( [ b64decode ( index ) ] , {
161171 type : 'application/json'
162172 } ) . stream ( ) . pipeThrough ( new window . DecompressionStream ( 'gzip' ) )
@@ -165,7 +175,7 @@ async function decompress(index) {
165175 return JSON . parse ( blob )
166176}
167177
168- function b64encode ( buffer ) {
178+ function b64encode ( buffer ) {
169179 let binary = ''
170180 const bytes = new Uint8Array ( buffer )
171181 const len = bytes . byteLength
@@ -175,7 +185,7 @@ function b64encode(buffer) {
175185 return window . btoa ( binary )
176186}
177187
178- function b64decode ( str ) {
188+ function b64decode ( str ) {
179189 const binaryString = window . atob ( str )
180190 const len = binaryString . length
181191 const bytes = new Uint8Array ( new ArrayBuffer ( len ) )
@@ -185,11 +195,11 @@ function b64decode(str) {
185195 return bytes
186196}
187197
188- function indexStorageKey ( ) {
198+ function indexStorageKey ( ) {
189199 return `idv5:${ getProjectNameAndVersion ( ) } `
190200}
191201
192- function createIndex ( ) {
202+ function createIndex ( ) {
193203 return lunr ( function ( ) {
194204 this . ref ( 'ref' )
195205 this . field ( 'title' , { boost : 3 } )
@@ -207,11 +217,11 @@ function createIndex() {
207217 } )
208218}
209219
210- function docTokenSplitter ( builder ) {
220+ function docTokenSplitter ( builder ) {
211221 builder . pipeline . before ( lunr . stemmer , docTokenFunction )
212222}
213223
214- function docTokenFunction ( token ) {
224+ function docTokenFunction ( token ) {
215225 // If we have something with an arity, we split on : . to make partial
216226 // matches easier. We split only when tokenizing, not when searching.
217227 // Below we use ExDoc.Markdown.to_ast/2 as an example.
@@ -275,11 +285,11 @@ function docTokenFunction(token) {
275285 return tokens
276286}
277287
278- function docTrimmer ( builder ) {
288+ function docTrimmer ( builder ) {
279289 builder . pipeline . before ( lunr . stemmer , docTrimmerFunction )
280290}
281291
282- function docTrimmerFunction ( token ) {
292+ function docTrimmerFunction ( token ) {
283293 // Preserve @ and : at the beginning of tokens,
284294 // and ? and ! at the end of tokens. It needs to
285295 // be done before stemming, otherwise search and
@@ -289,7 +299,7 @@ function docTrimmerFunction(token) {
289299 } )
290300}
291301
292- function searchResultsToDecoratedSearchItems ( results ) {
302+ function searchResultsToDecoratedSearchItems ( results ) {
293303 return results
294304 // If the docs are regenerated without changing its version,
295305 // a reference may have been doc'ed false in the code but
@@ -306,11 +316,11 @@ function searchResultsToDecoratedSearchItems(results) {
306316 } )
307317}
308318
309- function getSearchItemByRef ( ref ) {
319+ function getSearchItemByRef ( ref ) {
310320 return searchData . items . find ( searchItem => searchItem . ref === ref ) || null
311321}
312322
313- function getExcerpts ( searchItem , metadata ) {
323+ function getExcerpts ( searchItem , metadata ) {
314324 const { doc } = searchItem
315325 const searchTerms = Object . keys ( metadata )
316326
@@ -331,7 +341,7 @@ function getExcerpts(searchItem, metadata) {
331341 return excerpts . slice ( 0 , 1 )
332342}
333343
334- function excerpt ( doc , sliceStart , sliceLength ) {
344+ function excerpt ( doc , sliceStart , sliceLength ) {
335345 const startPos = Math . max ( sliceStart - EXCERPT_RADIUS , 0 )
336346 const endPos = Math . min ( sliceStart + sliceLength + EXCERPT_RADIUS , doc . length )
337347 return [
0 commit comments