@@ -49,10 +49,18 @@ if (typeof Scorer === "undefined") {
49
49
50
50
// Global search result kind enum, used by themes to style search results.
51
51
class SearchResultContext {
52
- static get index ( ) { return "index" ; }
53
- static get object ( ) { return "object" ; }
54
- static get text ( ) { return "text" ; }
55
- static get title ( ) { return "title" ; }
52
+ static get index ( ) {
53
+ return "index" ;
54
+ }
55
+ static get object ( ) {
56
+ return "object" ;
57
+ }
58
+ static get text ( ) {
59
+ return "text" ;
60
+ }
61
+ static get title ( ) {
62
+ return "title" ;
63
+ }
56
64
}
57
65
58
66
const _removeChildren = ( element ) => {
@@ -62,8 +70,7 @@ const _removeChildren = (element) => {
62
70
/**
63
71
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
64
72
*/
65
- const _escapeRegExp = ( string ) =>
66
- string . replace ( / [ . * + \- ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
73
+ const _escapeRegExp = ( string ) => string . replace ( / [ . * + \- ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
67
74
68
75
const _displayItem = ( item , searchTerms , highlightTerms ) => {
69
76
const docBuilder = DOCUMENTATION_OPTIONS . BUILDER ;
@@ -84,8 +91,7 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
84
91
if ( docBuilder === "dirhtml" ) {
85
92
// dirhtml builder
86
93
let dirname = docName + "/" ;
87
- if ( dirname . match ( / \/ i n d e x \/ $ / ) )
88
- dirname = dirname . substring ( 0 , dirname . length - 6 ) ;
94
+ if ( dirname . match ( / \/ i n d e x \/ $ / ) ) dirname = dirname . substring ( 0 , dirname . length - 6 ) ;
89
95
else if ( dirname === "index/" ) dirname = "" ;
90
96
requestUrl = contentRoot + dirname ;
91
97
linkUrl = requestUrl ;
@@ -99,23 +105,23 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
99
105
linkEl . dataset . score = score ;
100
106
linkEl . innerHTML = title ;
101
107
if ( descr ) {
102
- listItem . appendChild ( document . createElement ( "span" ) ) . innerHTML =
103
- " (" + descr + ")" ;
108
+ listItem . appendChild ( document . createElement ( "span" ) ) . innerHTML = " (" + descr + ")" ;
104
109
// highlight search terms in the description
105
- if ( SPHINX_HIGHLIGHT_ENABLED ) // set in sphinx_highlight.js
110
+ if ( SPHINX_HIGHLIGHT_ENABLED )
111
+ // set in sphinx_highlight.js
106
112
highlightTerms . forEach ( ( term ) => _highlightText ( listItem , term , "highlighted" ) ) ;
107
- }
108
- else if ( showSearchSummary )
113
+ } else if ( showSearchSummary )
109
114
fetch ( requestUrl )
110
115
. then ( ( responseData ) => responseData . text ( ) )
111
116
. then ( ( data ) => {
112
117
if ( data )
113
- listItem . appendChild (
114
- Search . makeSearchSummary ( data , searchTerms , anchor )
115
- ) ;
118
+ listItem . appendChild ( Search . makeSearchSummary ( data , searchTerms , anchor ) ) ;
116
119
// highlight search terms in the summary
117
- if ( SPHINX_HIGHLIGHT_ENABLED ) // set in sphinx_highlight.js
118
- highlightTerms . forEach ( ( term ) => _highlightText ( listItem , term , "highlighted" ) ) ;
120
+ if ( SPHINX_HIGHLIGHT_ENABLED )
121
+ // set in sphinx_highlight.js
122
+ highlightTerms . forEach ( ( term ) =>
123
+ _highlightText ( listItem , term , "highlighted" ) ,
124
+ ) ;
119
125
} ) ;
120
126
Search . output . appendChild ( listItem ) ;
121
127
} ;
@@ -124,28 +130,23 @@ const _finishSearch = (resultCount) => {
124
130
Search . title . innerText = _ ( "Search Results" ) ;
125
131
if ( ! resultCount )
126
132
Search . status . innerText = Documentation . gettext (
127
- "Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories."
133
+ "Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories." ,
128
134
) ;
129
135
else
130
136
Search . status . innerText = Documentation . ngettext (
131
137
"Search finished, found one page matching the search query." ,
132
138
"Search finished, found ${resultCount} pages matching the search query." ,
133
139
resultCount ,
134
- ) . replace ( ' ${resultCount}' , resultCount ) ;
140
+ ) . replace ( " ${resultCount}" , resultCount ) ;
135
141
} ;
136
- const _displayNextItem = (
137
- results ,
138
- resultCount ,
139
- searchTerms ,
140
- highlightTerms ,
141
- ) => {
142
+ const _displayNextItem = ( results , resultCount , searchTerms , highlightTerms ) => {
142
143
// results left, load the summary and display it
143
144
// this is intended to be dynamic (don't sub resultsCount)
144
145
if ( results . length ) {
145
146
_displayItem ( results . pop ( ) , searchTerms , highlightTerms ) ;
146
147
setTimeout (
147
148
( ) => _displayNextItem ( results , resultCount , searchTerms , highlightTerms ) ,
148
- 5
149
+ 5 ,
149
150
) ;
150
151
}
151
152
// search finished, update title and status message
@@ -177,9 +178,10 @@ const _orderResultsByScoreThenName = (a, b) => {
177
178
* This is the same as ``\W+`` in Python, preserving the surrogate pair area.
178
179
*/
179
180
if ( typeof splitQuery === "undefined" ) {
180
- var splitQuery = ( query ) => query
181
+ var splitQuery = ( query ) =>
182
+ query
181
183
. split ( / [ ^ \p{ Letter} \p{ Number} _ \p{ Emoji_Presentation} ] + / gu)
182
- . filter ( term => term ) // remove remaining empty strings
184
+ . filter ( ( term ) => term ) ; // remove remaining empty strings
183
185
}
184
186
185
187
/**
@@ -191,16 +193,18 @@ const Search = {
191
193
_pulse_status : - 1 ,
192
194
193
195
htmlToText : ( htmlString , anchor ) => {
194
- const htmlElement = new DOMParser ( ) . parseFromString ( htmlString , ' text/html' ) ;
196
+ const htmlElement = new DOMParser ( ) . parseFromString ( htmlString , " text/html" ) ;
195
197
for ( const removalQuery of [ ".headerlink" , "script" , "style" ] ) {
196
- htmlElement . querySelectorAll ( removalQuery ) . forEach ( ( el ) => { el . remove ( ) } ) ;
198
+ htmlElement . querySelectorAll ( removalQuery ) . forEach ( ( el ) => {
199
+ el . remove ( ) ;
200
+ } ) ;
197
201
}
198
202
if ( anchor ) {
199
203
const anchorContent = htmlElement . querySelector ( `[role="main"] ${ anchor } ` ) ;
200
204
if ( anchorContent ) return anchorContent . textContent ;
201
205
202
206
console . warn (
203
- `Anchored content block not found. Sphinx search tries to obtain it via DOM query '[role=main] ${ anchor } '. Check your theme or template.`
207
+ `Anchored content block not found. Sphinx search tries to obtain it via DOM query '[role=main] ${ anchor } '. Check your theme or template.` ,
204
208
) ;
205
209
}
206
210
@@ -209,16 +213,14 @@ const Search = {
209
213
if ( docContent ) return docContent . textContent ;
210
214
211
215
console . warn (
212
- "Content block not found. Sphinx search tries to obtain it via DOM query '[role=main]'. Check your theme or template."
216
+ "Content block not found. Sphinx search tries to obtain it via DOM query '[role=main]'. Check your theme or template." ,
213
217
) ;
214
218
return "" ;
215
219
} ,
216
220
217
221
init : ( ) => {
218
222
const query = new URLSearchParams ( window . location . search ) . get ( "q" ) ;
219
- document
220
- . querySelectorAll ( 'input[name="q"]' )
221
- . forEach ( ( el ) => ( el . value = query ) ) ;
223
+ document . querySelectorAll ( 'input[name="q"]' ) . forEach ( ( el ) => ( el . value = query ) ) ;
222
224
if ( query ) Search . performSearch ( query ) ;
223
225
} ,
224
226
@@ -295,11 +297,7 @@ const Search = {
295
297
296
298
// maybe skip this "word"
297
299
// stopwords array is from language_data.js
298
- if (
299
- stopwords . indexOf ( queryTermLower ) !== - 1 ||
300
- queryTerm . match ( / ^ \d + $ / )
301
- )
302
- return ;
300
+ if ( stopwords . indexOf ( queryTermLower ) !== - 1 || queryTerm . match ( / ^ \d + $ / ) ) return ;
303
301
304
302
// stem the word
305
303
let word = stemmer . stemWord ( queryTermLower ) ;
@@ -311,8 +309,9 @@ const Search = {
311
309
}
312
310
} ) ;
313
311
314
- if ( SPHINX_HIGHLIGHT_ENABLED ) { // set in sphinx_highlight.js
315
- localStorage . setItem ( "sphinx_highlight_terms" , [ ...highlightTerms ] . join ( " " ) )
312
+ if ( SPHINX_HIGHLIGHT_ENABLED ) {
313
+ // set in sphinx_highlight.js
314
+ localStorage . setItem ( "sphinx_highlight_terms" , [ ...highlightTerms ] . join ( " " ) ) ;
316
315
}
317
316
318
317
// console.debug("SEARCH: searching for:");
@@ -341,10 +340,13 @@ const Search = {
341
340
342
341
const queryLower = query . toLowerCase ( ) . trim ( ) ;
343
342
for ( const [ title , foundTitles ] of Object . entries ( allTitles ) ) {
344
- if ( title . toLowerCase ( ) . trim ( ) . includes ( queryLower ) && ( queryLower . length >= title . length / 2 ) ) {
343
+ if (
344
+ title . toLowerCase ( ) . trim ( ) . includes ( queryLower ) &&
345
+ queryLower . length >= title . length / 2
346
+ ) {
345
347
for ( const [ file , id ] of foundTitles ) {
346
- const score = Math . round ( Scorer . title * queryLower . length / title . length ) ;
347
- const boost = titles [ file ] === title ? 1 : 0 ; // add a boost for document titles
348
+ const score = Math . round ( ( Scorer . title * queryLower . length ) / title . length ) ;
349
+ const boost = titles [ file ] === title ? 1 : 0 ; // add a boost for document titles
348
350
normalResults . push ( [
349
351
docNames [ file ] ,
350
352
titles [ file ] !== title ? `${ titles [ file ] } > ${ title } ` : title ,
@@ -360,9 +362,9 @@ const Search = {
360
362
361
363
// search for explicit entries in index directives
362
364
for ( const [ entry , foundEntries ] of Object . entries ( indexEntries ) ) {
363
- if ( entry . includes ( queryLower ) && ( queryLower . length >= entry . length / 2 ) ) {
365
+ if ( entry . includes ( queryLower ) && queryLower . length >= entry . length / 2 ) {
364
366
for ( const [ file , id , isMain ] of foundEntries ) {
365
- const score = Math . round ( 100 * queryLower . length / entry . length ) ;
367
+ const score = Math . round ( ( 100 * queryLower . length ) / entry . length ) ;
366
368
const result = [
367
369
docNames [ file ] ,
368
370
titles [ file ] ,
@@ -383,7 +385,7 @@ const Search = {
383
385
384
386
// lookup as object
385
387
objectTerms . forEach ( ( term ) =>
386
- normalResults . push ( ...Search . performObjectSearch ( term , objectTerms ) )
388
+ normalResults . push ( ...Search . performObjectSearch ( term , objectTerms ) ) ,
387
389
) ;
388
390
389
391
// lookup as search terms in fulltext
@@ -408,7 +410,11 @@ const Search = {
408
410
// note the reversing of results, so that in the case of duplicates, the highest-scoring entry is kept
409
411
let seen = new Set ( ) ;
410
412
results = results . reverse ( ) . reduce ( ( acc , result ) => {
411
- let resultStr = result . slice ( 0 , 4 ) . concat ( [ result [ 5 ] ] ) . map ( v => String ( v ) ) . join ( ',' ) ;
413
+ let resultStr = result
414
+ . slice ( 0 , 4 )
415
+ . concat ( [ result [ 5 ] ] )
416
+ . map ( ( v ) => String ( v ) )
417
+ . join ( "," ) ;
412
418
if ( ! seen . has ( resultStr ) ) {
413
419
acc . push ( result ) ;
414
420
seen . add ( resultStr ) ;
@@ -420,8 +426,15 @@ const Search = {
420
426
} ,
421
427
422
428
query : ( query ) => {
423
- const [ searchQuery , searchTerms , excludedTerms , highlightTerms , objectTerms ] = Search . _parseQuery ( query ) ;
424
- const results = Search . _performSearch ( searchQuery , searchTerms , excludedTerms , highlightTerms , objectTerms ) ;
429
+ const [ searchQuery , searchTerms , excludedTerms , highlightTerms , objectTerms ] =
430
+ Search . _parseQuery ( query ) ;
431
+ const results = Search . _performSearch (
432
+ searchQuery ,
433
+ searchTerms ,
434
+ excludedTerms ,
435
+ highlightTerms ,
436
+ objectTerms ,
437
+ ) ;
425
438
426
439
// for debugging
427
440
//Search.lastresults = results.slice(); // a copy
@@ -444,7 +457,7 @@ const Search = {
444
457
const results = [ ] ;
445
458
446
459
const objectSearchCallback = ( prefix , match ) => {
447
- const name = match [ 4 ]
460
+ const name = match [ 4 ] ;
448
461
const fullname = ( prefix ? prefix + "." : "" ) + name ;
449
462
const fullnameLower = fullname . toLowerCase ( ) ;
450
463
if ( fullnameLower . indexOf ( object ) < 0 ) return ;
@@ -456,8 +469,7 @@ const Search = {
456
469
// "last name" (i.e. last dotted part)
457
470
if ( fullnameLower === object || parts . slice ( - 1 ) [ 0 ] === object )
458
471
score += Scorer . objNameMatch ;
459
- else if ( parts . slice ( - 1 ) [ 0 ] . indexOf ( object ) > - 1 )
460
- score += Scorer . objPartialMatch ; // matches in last name
472
+ else if ( parts . slice ( - 1 ) [ 0 ] . indexOf ( object ) > - 1 ) score += Scorer . objPartialMatch ; // matches in last name
461
473
462
474
const objName = objNames [ match [ 1 ] ] [ 2 ] ;
463
475
const title = titles [ match [ 0 ] ] ;
@@ -468,9 +480,7 @@ const Search = {
468
480
otherTerms . delete ( object ) ;
469
481
if ( otherTerms . size > 0 ) {
470
482
const haystack = `${ prefix } ${ name } ${ objName } ${ title } ` . toLowerCase ( ) ;
471
- if (
472
- [ ...otherTerms ] . some ( ( otherTerm ) => haystack . indexOf ( otherTerm ) < 0 )
473
- )
483
+ if ( [ ...otherTerms ] . some ( ( otherTerm ) => haystack . indexOf ( otherTerm ) < 0 ) )
474
484
return ;
475
485
}
476
486
@@ -481,8 +491,7 @@ const Search = {
481
491
const descr = objName + _ ( ", in " ) + title ;
482
492
483
493
// add custom score for some objects according to scorer
484
- if ( Scorer . objPrio . hasOwnProperty ( match [ 2 ] ) )
485
- score += Scorer . objPrio [ match [ 2 ] ] ;
494
+ if ( Scorer . objPrio . hasOwnProperty ( match [ 2 ] ) ) score += Scorer . objPrio [ match [ 2 ] ] ;
486
495
else score += Scorer . objPrioDefault ;
487
496
488
497
results . push ( [
@@ -496,9 +505,7 @@ const Search = {
496
505
] ) ;
497
506
} ;
498
507
Object . keys ( objects ) . forEach ( ( prefix ) =>
499
- objects [ prefix ] . forEach ( ( array ) =>
500
- objectSearchCallback ( prefix , array )
501
- )
508
+ objects [ prefix ] . forEach ( ( array ) => objectSearchCallback ( prefix , array ) ) ,
502
509
) ;
503
510
return results ;
504
511
} ,
@@ -573,12 +580,9 @@ const Search = {
573
580
574
581
// as search terms with length < 3 are discarded
575
582
const filteredTermCount = [ ...searchTerms ] . filter (
576
- ( term ) => term . length > 2
583
+ ( term ) => term . length > 2 ,
577
584
) . length ;
578
- if (
579
- wordList . length !== searchTerms . size &&
580
- wordList . length !== filteredTermCount
581
- )
585
+ if ( wordList . length !== searchTerms . size && wordList . length !== filteredTermCount )
582
586
continue ;
583
587
584
588
// ensure that none of the excluded terms is in the search result
@@ -588,7 +592,7 @@ const Search = {
588
592
terms [ term ] === file ||
589
593
titleTerms [ term ] === file ||
590
594
( terms [ term ] || [ ] ) . includes ( file ) ||
591
- ( titleTerms [ term ] || [ ] ) . includes ( file )
595
+ ( titleTerms [ term ] || [ ] ) . includes ( file ) ,
592
596
)
593
597
)
594
598
break ;
0 commit comments