File tree Expand file tree Collapse file tree 3 files changed +26
-2
lines changed
Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -127,6 +127,14 @@ describe("contextExtracts", () => {
127127 // Previous bug was an empty match here.
128128 ] ) ;
129129 } ) ;
130+ it ( "returns the first sentence without matches if no positions are provided" , ( ) => {
131+ expect ( contextExtracts ( [ ] , "First sentence. Second sentence." ) ) . toEqual ( [
132+ {
133+ type : "text" ,
134+ extract : "First sentence." ,
135+ } ,
136+ ] ) ;
137+ } ) ;
130138} ) ;
131139
132140describe ( "sortByStart" , ( ) => {
Original file line number Diff line number Diff line change @@ -64,7 +64,14 @@ export const contextExtracts = (
6464 text : string
6565) : Extract [ ] => {
6666 if ( positions . length === 0 ) {
67- return [ ] ;
67+ // Fallback if only text in the title (or id for Reference section) is matched.
68+ const end = forward ( text , 1 ) ;
69+ return [
70+ {
71+ type : "text" ,
72+ extract : text . slice ( 0 , end + 1 ) ,
73+ } ,
74+ ] ;
6875 }
6976 // Find the text around the first match.
7077 // Highlight all positions within it.
Original file line number Diff line number Diff line change @@ -129,7 +129,6 @@ const getExtracts = (
129129
130130 return {
131131 title : fullStringExtracts ( allTitlePositions , content . title ) ,
132- // TODO: consider a fallback if only text in the title is matched.
133132 content : contextExtracts ( allContentPositions , content . content ) ,
134133 } ;
135134} ;
@@ -234,6 +233,16 @@ export const buildSearchIndex = (
234233 let customTokenizer : TokenizerFunction | undefined ;
235234 const index = lunr ( function ( ) {
236235 this . ref ( "id" ) ;
236+ this . field ( "id" , {
237+ boost : 10 ,
238+ extractor : ( doc : object ) => {
239+ // Ensure we match a search query like 'microbit.display.scroll' or 'display.scroll'
240+ // to the correct API section.
241+ return `${ ( doc as SearchableContent ) . id } ${ (
242+ doc as SearchableContent
243+ ) . id . replaceAll ( "microbit." , "" ) } `;
244+ } ,
245+ } ) ;
237246 this . field ( "title" , { boost : 10 } ) ;
238247 this . field ( "content" ) ;
239248 this . use ( languagePlugin ) ;
You can’t perform that action at this time.
0 commit comments