1
- /* eslint-env browser */
2
- window . antoraLunr = ( function ( lunr ) {
3
- const scriptAttrs = document . getElementById ( 'search-script' ) . dataset
4
- const basePath = scriptAttrs . basePath
5
- const pagePath = scriptAttrs . pagePath
1
+ ; ( globalThis || window ) . lunrSiteSearch = ( function ( ) {
2
+ /* eslint-disable no-var */
3
+ const config = document . getElementById ( 'search-script' ) . dataset
4
+ const siteRootPath = config . siteRootPath || ''
6
5
var searchInput = document . getElementById ( 'search-input' )
7
6
var searchResult = document . createElement ( 'div' )
8
7
searchResult . classList . add ( 'search-result-dropdown-menu' )
@@ -20,7 +19,7 @@ window.antoraLunr = (function (lunr) {
20
19
21
20
var end = start + length
22
21
var textEnd = text . length - 1
23
- var contextOffset = 15
22
+ var contextOffset = 100
24
23
var contextAfter = end + contextOffset > textEnd ? textEnd : end + contextOffset
25
24
var contextBefore = start - contextOffset < 0 ? 0 : start - contextOffset
26
25
if ( start === 0 && end === textEnd ) {
@@ -93,7 +92,7 @@ window.antoraLunr = (function (lunr) {
93
92
return hits
94
93
}
95
94
96
- function createSearchResult ( result , store , searchResultDataset ) {
95
+ function createSearchResult ( result , store , searchResultDataset ) {
97
96
result . forEach ( function ( item ) {
98
97
var url = item . ref
99
98
var hash
@@ -115,8 +114,7 @@ window.antoraLunr = (function (lunr) {
115
114
var documentHit = document . createElement ( 'div' )
116
115
documentHit . classList . add ( 'search-result-document-hit' )
117
116
var documentHitLink = document . createElement ( 'a' )
118
- var rootPath = basePath
119
- documentHitLink . href = rootPath + item . ref
117
+ documentHitLink . href = siteRootPath + item . ref
120
118
documentHit . appendChild ( documentHitLink )
121
119
hits . forEach ( function ( hit ) {
122
120
documentHitLink . appendChild ( hit )
@@ -194,13 +192,22 @@ window.antoraLunr = (function (lunr) {
194
192
}
195
193
}
196
194
197
- function init ( data ) {
198
- var index = Object . assign ( { index : lunr . Index . load ( data . index ) , store : data . store } )
195
+ function init ( lunr , data ) {
196
+ var index = Object . assign ( { index : lunr . Index . load ( data . index ) , store : data . store } )
197
+ var debug = 'URLSearchParams' in window && new URLSearchParams ( window . location . search ) . has ( 'lunr-debug' )
199
198
var search = debounce ( function ( ) {
200
- searchIndex ( index . index , index . store , searchInput . value )
199
+ try {
200
+ searchIndex ( index . index , index . store , searchInput . value )
201
+ } catch ( err ) {
202
+ if ( debug ) console . debug ( 'Invalid search query: ' + searchInput . value + ' (' + err . message + ')' )
203
+ }
201
204
} , 100 )
202
205
searchInput . addEventListener ( 'keydown' , search )
203
206
207
+ searchInput . addEventListener ( 'keydown' , function ( event ) {
208
+ if ( event . key === 'Escape' ) searchInput . value = ''
209
+ } )
210
+
204
211
// this is prevented in case of mousedown attached to SearchResultItem
205
212
searchInput . addEventListener ( 'blur' , function ( e ) {
206
213
while ( searchResult . firstChild ) {
@@ -209,7 +216,5 @@ window.antoraLunr = (function (lunr) {
209
216
} )
210
217
}
211
218
212
- return {
213
- init : init ,
214
- }
215
- } ) ( window . lunr )
219
+ return { init : init }
220
+ } ) ( )
0 commit comments