diff --git a/src/documentation/search/extracts.test.ts b/src/documentation/search/extracts.test.ts index 92ec5daee..1db30fc2f 100644 --- a/src/documentation/search/extracts.test.ts +++ b/src/documentation/search/extracts.test.ts @@ -127,6 +127,14 @@ describe("contextExtracts", () => { // Previous bug was an empty match here. ]); }); + it("returns the first sentence without matches if no positions are provided", () => { + expect(contextExtracts([], "First sentence. Second sentence.")).toEqual([ + { + type: "text", + extract: "First sentence.", + }, + ]); + }); }); describe("sortByStart", () => { diff --git a/src/documentation/search/extracts.ts b/src/documentation/search/extracts.ts index b1e1c4c9e..a70d1c2a7 100644 --- a/src/documentation/search/extracts.ts +++ b/src/documentation/search/extracts.ts @@ -64,7 +64,14 @@ export const contextExtracts = ( text: string ): Extract[] => { if (positions.length === 0) { - return []; + // Fallback if only text in the title (or id for Reference section) is matched. + const end = forward(text, 1); + return [ + { + type: "text", + extract: text.slice(0, end + 1), + }, + ]; } // Find the text around the first match. // Highlight all positions within it. diff --git a/src/documentation/search/search.worker.ts b/src/documentation/search/search.worker.ts index bf51579b6..52fac6fa0 100644 --- a/src/documentation/search/search.worker.ts +++ b/src/documentation/search/search.worker.ts @@ -129,7 +129,6 @@ const getExtracts = ( return { title: fullStringExtracts(allTitlePositions, content.title), - // TODO: consider a fallback if only text in the title is matched. content: contextExtracts(allContentPositions, content.content), }; }; @@ -234,6 +233,16 @@ export const buildSearchIndex = ( let customTokenizer: TokenizerFunction | undefined; const index = lunr(function () { this.ref("id"); + this.field("id", { + boost: 10, + extractor: (doc: object) => { + // Ensure we match a search query like 'microbit.display.scroll' or 'display.scroll' + // to the correct API section. + return `${(doc as SearchableContent).id} ${( + doc as SearchableContent + ).id.replaceAll("microbit.", "")}`; + }, + }); this.field("title", { boost: 10 }); this.field("content"); this.use(languagePlugin);