1
1
/*
2
- * searchtools.js
3
- * ~~~~~~~~~~~~~~~~
4
- *
5
2
* Sphinx JavaScript utilities for the full-text search.
6
- *
7
- * :copyright: Copyright 2007-2024 by the Sphinx team, see AUTHORS.
8
- * :license: BSD, see LICENSE for details.
9
- *
10
3
*/
11
4
"use strict" ;
12
5
@@ -20,7 +13,7 @@ if (typeof Scorer === "undefined") {
20
13
// and returns the new score.
21
14
/*
22
15
score: result => {
23
- const [docname, title, anchor, descr, score, filename] = result
16
+ const [docname, title, anchor, descr, score, filename, kind ] = result
24
17
return score
25
18
},
26
19
*/
@@ -47,6 +40,14 @@ if (typeof Scorer === "undefined") {
47
40
} ;
48
41
}
49
42
43
+ // Global search result kind enum, used by themes to style search results.
44
+ class SearchResultKind {
45
+ static get index ( ) { return "index" ; }
46
+ static get object ( ) { return "object" ; }
47
+ static get text ( ) { return "text" ; }
48
+ static get title ( ) { return "title" ; }
49
+ }
50
+
50
51
const _removeChildren = ( element ) => {
51
52
while ( element && element . lastChild ) element . removeChild ( element . lastChild ) ;
52
53
} ;
@@ -64,9 +65,13 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
64
65
const showSearchSummary = DOCUMENTATION_OPTIONS . SHOW_SEARCH_SUMMARY ;
65
66
const contentRoot = document . documentElement . dataset . content_root ;
66
67
67
- const [ docName , title , anchor , descr , score , _filename ] = item ;
68
+ const [ docName , title , anchor , descr , score , _filename , kind ] = item ;
68
69
69
70
let listItem = document . createElement ( "li" ) ;
71
+ // Add a class representing the item's type:
72
+ // can be used by a theme's CSS selector for styling
73
+ // See SearchResultKind for the class names.
74
+ listItem . classList . add ( `kind-${ kind } ` ) ;
70
75
let requestUrl ;
71
76
let linkUrl ;
72
77
if ( docBuilder === "dirhtml" ) {
@@ -115,8 +120,10 @@ const _finishSearch = (resultCount) => {
115
120
"Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories."
116
121
) ;
117
122
else
118
- Search . status . innerText = _ (
119
- "Search finished, found ${resultCount} page(s) matching the search query."
123
+ Search . status . innerText = Documentation . ngettext (
124
+ "Search finished, found one page matching the search query." ,
125
+ "Search finished, found ${resultCount} pages matching the search query." ,
126
+ resultCount ,
120
127
) . replace ( '${resultCount}' , resultCount ) ;
121
128
} ;
122
129
const _displayNextItem = (
@@ -138,7 +145,7 @@ const _displayNextItem = (
138
145
else _finishSearch ( resultCount ) ;
139
146
} ;
140
147
// Helper function used by query() to order search results.
141
- // Each input is an array of [docname, title, anchor, descr, score, filename].
148
+ // Each input is an array of [docname, title, anchor, descr, score, filename, kind ].
142
149
// Order the results by score (in opposite order of appearance, since the
143
150
// `_displayNextItem` function uses pop() to retrieve items) and then alphabetically.
144
151
const _orderResultsByScoreThenName = ( a , b ) => {
@@ -248,6 +255,7 @@ const Search = {
248
255
searchSummary . classList . add ( "search-summary" ) ;
249
256
searchSummary . innerText = "" ;
250
257
const searchList = document . createElement ( "ul" ) ;
258
+ searchList . setAttribute ( "role" , "list" ) ;
251
259
searchList . classList . add ( "search" ) ;
252
260
253
261
const out = document . getElementById ( "search-results" ) ;
@@ -318,7 +326,7 @@ const Search = {
318
326
const indexEntries = Search . _index . indexentries ;
319
327
320
328
// Collect multiple result groups to be sorted separately and then ordered.
321
- // Each is an array of [docname, title, anchor, descr, score, filename].
329
+ // Each is an array of [docname, title, anchor, descr, score, filename, kind ].
322
330
const normalResults = [ ] ;
323
331
const nonMainIndexResults = [ ] ;
324
332
@@ -337,6 +345,7 @@ const Search = {
337
345
null ,
338
346
score + boost ,
339
347
filenames [ file ] ,
348
+ SearchResultKind . title ,
340
349
] ) ;
341
350
}
342
351
}
@@ -354,6 +363,7 @@ const Search = {
354
363
null ,
355
364
score ,
356
365
filenames [ file ] ,
366
+ SearchResultKind . index ,
357
367
] ;
358
368
if ( isMain ) {
359
369
normalResults . push ( result ) ;
@@ -475,6 +485,7 @@ const Search = {
475
485
descr ,
476
486
score ,
477
487
filenames [ match [ 0 ] ] ,
488
+ SearchResultKind . object ,
478
489
] ) ;
479
490
} ;
480
491
Object . keys ( objects ) . forEach ( ( prefix ) =>
@@ -585,6 +596,7 @@ const Search = {
585
596
null ,
586
597
score ,
587
598
filenames [ file ] ,
599
+ SearchResultKind . text ,
588
600
] ) ;
589
601
}
590
602
return results ;
0 commit comments