@@ -57,14 +57,14 @@ const _removeChildren = (element) => {
5757const _escapeRegExp = ( string ) =>
5858 string . replace ( / [ . * + \- ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
5959
60- const _displayItem = ( item , highlightTerms , searchTerms ) => {
60+ const _displayItem = ( item , searchTerms ) => {
6161 const docBuilder = DOCUMENTATION_OPTIONS . BUILDER ;
6262 const docUrlRoot = DOCUMENTATION_OPTIONS . URL_ROOT ;
6363 const docFileSuffix = DOCUMENTATION_OPTIONS . FILE_SUFFIX ;
6464 const docLinkSuffix = DOCUMENTATION_OPTIONS . LINK_SUFFIX ;
6565 const showSearchSummary = DOCUMENTATION_OPTIONS . SHOW_SEARCH_SUMMARY ;
6666
67- const [ docName , title , anchor , descr ] = item ;
67+ const [ docName , title , anchor , descr , score , _filename ] = item ;
6868
6969 let listItem = document . createElement ( "li" ) ;
7070 let requestUrl ;
@@ -82,10 +82,9 @@ const _displayItem = (item, highlightTerms, searchTerms) => {
8282 requestUrl = docUrlRoot + docName + docFileSuffix ;
8383 linkUrl = docName + docLinkSuffix ;
8484 }
85- const params = new URLSearchParams ( ) ;
86- params . set ( "highlight" , [ ...highlightTerms ] . join ( " " ) ) ;
8785 let linkEl = listItem . appendChild ( document . createElement ( "a" ) ) ;
88- linkEl . href = linkUrl + "?" + params . toString ( ) + anchor ;
86+ linkEl . href = linkUrl + anchor ;
87+ linkEl . dataset . score = score ;
8988 linkEl . innerHTML = title ;
9089 if ( descr )
9190 listItem . appendChild ( document . createElement ( "span" ) ) . innerHTML =
@@ -96,7 +95,7 @@ const _displayItem = (item, highlightTerms, searchTerms) => {
9695 . then ( ( data ) => {
9796 if ( data )
9897 listItem . appendChild (
99- Search . makeSearchSummary ( data , searchTerms , highlightTerms )
98+ Search . makeSearchSummary ( data , searchTerms )
10099 ) ;
101100 } ) ;
102101 Search . output . appendChild ( listItem ) ;
@@ -116,15 +115,14 @@ const _finishSearch = (resultCount) => {
116115const _displayNextItem = (
117116 results ,
118117 resultCount ,
119- highlightTerms ,
120118 searchTerms
121119) => {
122120 // results left, load the summary and display it
123121 // this is intended to be dynamic (don't sub resultsCount)
124122 if ( results . length ) {
125- _displayItem ( results . pop ( ) , highlightTerms , searchTerms ) ;
123+ _displayItem ( results . pop ( ) , searchTerms ) ;
126124 setTimeout (
127- ( ) => _displayNextItem ( results , resultCount , highlightTerms , searchTerms ) ,
125+ ( ) => _displayNextItem ( results , resultCount , searchTerms ) ,
128126 5
129127 ) ;
130128 }
@@ -237,6 +235,12 @@ const Search = {
237235 * execute search (requires search index to be loaded)
238236 */
239237 query : ( query ) => {
238+ const filenames = Search . _index . filenames ;
239+ const docNames = Search . _index . docnames ;
240+ const titles = Search . _index . titles ;
241+ const allTitles = Search . _index . alltitles ;
242+ const indexEntries = Search . _index . indexentries ;
243+
240244 // stem the search terms and add them to the correct list
241245 const stemmer = new Stemmer ( ) ;
242246 const searchTerms = new Set ( ) ;
@@ -264,6 +268,10 @@ const Search = {
264268 }
265269 } ) ;
266270
271+ if ( SPHINX_HIGHLIGHT_ENABLED ) { // set in sphinx_highlight.js
272+ localStorage . setItem ( "sphinx_highlight_terms" , [ ...highlightTerms ] . join ( " " ) )
273+ }
274+
267275 // console.debug("SEARCH: searching for:");
268276 // console.info("required: ", [...searchTerms]);
269277 // console.info("excluded: ", [...excludedTerms]);
@@ -272,6 +280,40 @@ const Search = {
272280 let results = [ ] ;
273281 _removeChildren ( document . getElementById ( "search-progress" ) ) ;
274282
283+ const queryLower = query . toLowerCase ( ) ;
284+ for ( const [ title , foundTitles ] of Object . entries ( allTitles ) ) {
285+ if ( title . toLowerCase ( ) . includes ( queryLower ) && ( queryLower . length >= title . length / 2 ) ) {
286+ for ( const [ file , id ] of foundTitles ) {
287+ let score = Math . round ( 100 * queryLower . length / title . length )
288+ results . push ( [
289+ docNames [ file ] ,
290+ titles [ file ] !== title ? `${ titles [ file ] } > ${ title } ` : title ,
291+ id !== null ? "#" + id : "" ,
292+ null ,
293+ score ,
294+ filenames [ file ] ,
295+ ] ) ;
296+ }
297+ }
298+ }
299+
300+ // search for explicit entries in index directives
301+ for ( const [ entry , foundEntries ] of Object . entries ( indexEntries ) ) {
302+ if ( entry . includes ( queryLower ) && ( queryLower . length >= entry . length / 2 ) ) {
303+ for ( const [ file , id ] of foundEntries ) {
304+ let score = Math . round ( 100 * queryLower . length / entry . length )
305+ results . push ( [
306+ docNames [ file ] ,
307+ titles [ file ] ,
308+ id ? "#" + id : "" ,
309+ null ,
310+ score ,
311+ filenames [ file ] ,
312+ ] ) ;
313+ }
314+ }
315+ }
316+
275317 // lookup as object
276318 objectTerms . forEach ( ( term ) =>
277319 results . push ( ...Search . performObjectSearch ( term , objectTerms ) )
@@ -318,7 +360,7 @@ const Search = {
318360 // console.info("search results:", Search.lastresults);
319361
320362 // print the results
321- _displayNextItem ( results , results . length , highlightTerms , searchTerms ) ;
363+ _displayNextItem ( results , results . length , searchTerms ) ;
322364 } ,
323365
324366 /**
@@ -399,8 +441,8 @@ const Search = {
399441 // prepare search
400442 const terms = Search . _index . terms ;
401443 const titleTerms = Search . _index . titleterms ;
402- const docNames = Search . _index . docnames ;
403444 const filenames = Search . _index . filenames ;
445+ const docNames = Search . _index . docnames ;
404446 const titles = Search . _index . titles ;
405447
406448 const scoreMap = new Map ( ) ;
@@ -497,11 +539,9 @@ const Search = {
497539 /**
498540 * helper function to return a node containing the
499541 * search summary for a given text. keywords is a list
500- * of stemmed words, highlightWords is the list of normal, unstemmed
501- * words. the first one is used to find the occurrence, the
502- * latter for highlighting it.
542+ * of stemmed words.
503543 */
504- makeSearchSummary : ( htmlText , keywords , highlightWords ) => {
544+ makeSearchSummary : ( htmlText , keywords ) => {
505545 const text = Search . htmlToText ( htmlText ) ;
506546 if ( text === "" ) return null ;
507547
@@ -519,10 +559,6 @@ const Search = {
519559 summary . classList . add ( "context" ) ;
520560 summary . textContent = top + text . substr ( startWithContext , 240 ) . trim ( ) + tail ;
521561
522- highlightWords . forEach ( ( highlightWord ) =>
523- _highlightText ( summary , highlightWord , "highlighted" )
524- ) ;
525-
526562 return summary ;
527563 } ,
528564} ;
0 commit comments