@@ -99,7 +99,7 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
9999 . then ( ( data ) => {
100100 if ( data )
101101 listItem . appendChild (
102- Search . makeSearchSummary ( data , searchTerms )
102+ Search . makeSearchSummary ( data , searchTerms , anchor )
103103 ) ;
104104 // highlight search terms in the summary
105105 if ( SPHINX_HIGHLIGHT_ENABLED ) // set in sphinx_highlight.js
@@ -160,11 +160,22 @@ const Search = {
160160 _queued_query : null ,
161161 _pulse_status : - 1 ,
162162
163- htmlToText : ( htmlString ) => {
163+ htmlToText : ( htmlString , anchor ) => {
164164 const htmlElement = new DOMParser ( ) . parseFromString ( htmlString , 'text/html' ) ;
165165 htmlElement . querySelectorAll ( ".headerlink" ) . forEach ( ( el ) => { el . remove ( ) } ) ;
166+ if ( anchor ) {
167+ const anchorContent = htmlElement . querySelector ( anchor ) ;
168+ if ( anchorContent ) return anchorContent . textContent ;
169+
170+ console . warn (
171+ `Anchor block not found. Sphinx search tries to obtain it via '${ anchor } '. Check your theme or template.`
172+ ) ;
173+ }
174+
175+ // if anchor not specified or not found, fall back to main content
166176 const docContent = htmlElement . querySelector ( '[role="main"]' ) ;
167177 if ( docContent ) return docContent . textContent ;
178+
168179 console . warn (
169180 "Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template."
170181 ) ;
@@ -549,8 +560,8 @@ const Search = {
549560 * search summary for a given text. keywords is a list
550561 * of stemmed words.
551562 */
552- makeSearchSummary : ( htmlText , keywords ) => {
553- const text = Search . htmlToText ( htmlText ) ;
563+ makeSearchSummary : ( htmlText , keywords , anchor ) => {
564+ const text = Search . htmlToText ( htmlText , anchor ) ;
554565 if ( text === "" ) return null ;
555566
556567 const textLower = text . toLowerCase ( ) ;
0 commit comments