diff --git a/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.spec.ts b/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.spec.ts
index e154c8b0..6520c159 100644
--- a/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.spec.ts
+++ b/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.spec.ts
@@ -1,3 +1,4 @@
+import { SearchDocumentType } from "../../../shared/interfaces";
import { SuggestionTemplate } from "./SuggestionTemplate";
jest.mock("./icons");
@@ -12,7 +13,7 @@ describe("SuggestionTemplate", () => {
t: "Hello world",
u: "/docs/a",
},
- type: 0,
+ type: SearchDocumentType.Title,
page: false,
metadata: {
hello: {
@@ -65,7 +66,7 @@ describe("SuggestionTemplate", () => {
t: "Hello fruits.",
u: "/docs/b",
},
- type: 1,
+ type: SearchDocumentType.Heading,
page: {
i: 1,
t: "Hello world",
@@ -134,7 +135,7 @@ describe("SuggestionTemplate", () => {
t: "Goodbye fruits.",
u: "/docs/c",
},
- type: 2,
+ type: SearchDocumentType.Content,
page: {
i: 1,
t: "Hello world",
diff --git a/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.ts b/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.ts
index a8bbc3b8..d8a37a89 100644
--- a/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.ts
+++ b/docusaurus-search-local/src/client/theme/SearchBar/SuggestionTemplate.ts
@@ -1,4 +1,8 @@
-import { SearchDocument, SearchResult } from "../../../shared/interfaces";
+import {
+ SearchDocument,
+ SearchDocumentType,
+ SearchResult,
+} from "../../../shared/interfaces";
import { concatDocumentPath } from "../../utils/concatDocumentPath";
import { getStemmedPositions } from "../../utils/getStemmedPositions";
import { highlight } from "../../utils/highlight";
@@ -23,8 +27,10 @@ export function SuggestionTemplate({
isInterOfTree,
isLastOfTree,
}: Omit
{ p: 1, }, ]; + const documentsOfDescriptions: SearchDocument[] = [ + { + i: 1, + t: "First description", + u: "/1", + p: 1, + }, + ]; + const documentsOfKeywords: SearchDocument[] = [ + { + i: 1, + t: "First keywords", + u: "/1", + p: 1, + }, + ]; const documentsOfContents: SearchDocument[] = [ { i: 3, @@ -60,17 +76,27 @@ describe("SearchSourceFactory", () => { { documents: documentsOfTitles, index: getIndex(documentsOfTitles), - type: 0, + type: SearchDocumentType.Title, }, { documents: documentsOfHeadings, index: getIndex(documentsOfHeadings), - type: 1, + type: SearchDocumentType.Heading, + }, + { + documents: documentsOfDescriptions, + index: getIndex(documentsOfDescriptions), + type: SearchDocumentType.Description, + }, + { + documents: documentsOfKeywords, + index: getIndex(documentsOfKeywords), + type: SearchDocumentType.Keywords, }, { documents: documentsOfContents, index: getIndex(documentsOfContents), - type: 2, + type: SearchDocumentType.Content, }, ], [], @@ -82,6 +108,9 @@ describe("SearchSourceFactory", () => { [",", []], ["nothing", []], ["peace", [4, 2]], + ["description", [1]], + ["keywords", [1]], + ["first", [1, 2]], ])( "SearchSourceFactory('%s', zhDictionary) should return %j", (input, results) => { diff --git a/docusaurus-search-local/src/client/utils/SearchSourceFactory.ts b/docusaurus-search-local/src/client/utils/SearchSourceFactory.ts index e2dfa37b..8312f188 100644 --- a/docusaurus-search-local/src/client/utils/SearchSourceFactory.ts +++ b/docusaurus-search-local/src/client/utils/SearchSourceFactory.ts @@ -6,6 +6,7 @@ import { SearchResult, SearchDocument, InitialSearchResult, + SearchDocumentType, } from "../../shared/interfaces"; import { sortSearchResults } from "./sortSearchResults"; import { processTreeStatusOfSearchResults } from "./processTreeStatusOfSearchResults"; @@ -58,7 +59,7 @@ export function SearchSourceFactory( document, type, page: - type !== 0 && + type !== SearchDocumentType.Title && wrappedIndexes[0].documents.find( (doc) => doc.i === document.p ), diff --git a/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.spec.ts b/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.spec.ts index 019aa844..f017c0e3 100644 --- a/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.spec.ts +++ b/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.spec.ts @@ -1,4 +1,7 @@ -import { InitialSearchResult } from "../../shared/interfaces"; +import { + InitialSearchResult, + SearchDocumentType, +} from "../../shared/interfaces"; import { processTreeStatusOfSearchResults } from "./processTreeStatusOfSearchResults"; describe("processTreeStatusOfSearchResults", () => { @@ -8,14 +11,21 @@ describe("processTreeStatusOfSearchResults", () => { document: { i: 100, }, - type: 0, + type: SearchDocumentType.Title, page: undefined, }, { document: { i: 200, }, - type: 0, + type: SearchDocumentType.Title, + page: undefined, + }, + { + document: { + i: 300, + }, + type: SearchDocumentType.Title, page: undefined, }, ] as InitialSearchResult[]; @@ -24,14 +34,14 @@ describe("processTreeStatusOfSearchResults", () => { document: { i: 1, }, - type: 2, + type: SearchDocumentType.Content, page: {}, }, { document: { i: 2, }, - type: 1, + type: SearchDocumentType.Heading, page: {}, }, pageTitles[0], @@ -39,14 +49,14 @@ describe("processTreeStatusOfSearchResults", () => { document: { i: 101, }, - type: 2, + type: SearchDocumentType.Content, page: pageTitles[0].document, }, { document: { i: 3, }, - type: 1, + type: SearchDocumentType.Heading, page: {}, }, pageTitles[1], @@ -54,19 +64,33 @@ describe("processTreeStatusOfSearchResults", () => { document: { i: 201, }, - type: 1, + type: SearchDocumentType.Heading, page: pageTitles[1].document, }, { document: { i: 202, }, - type: 2, + type: SearchDocumentType.Content, page: pageTitles[1].document, }, + { + document: { + i: 301, + }, + type: SearchDocumentType.Keywords, + page: pageTitles[2].document, + }, + { + document: { + i: 302, + }, + type: SearchDocumentType.Description, + page: pageTitles[2].document, + }, ] as InitialSearchResult[]; processTreeStatusOfSearchResults(results); - const statuses: [boolean, boolean][] = [ + const statuses: [boolean | undefined, boolean | undefined][] = [ [undefined, undefined], [undefined, undefined], [undefined, undefined], @@ -75,6 +99,8 @@ describe("processTreeStatusOfSearchResults", () => { [undefined, undefined], [true, undefined], [undefined, true], + [undefined, undefined], + [undefined, true], ]; results.forEach((item, i) => { expect([item.isInterOfTree, item.isLastOfTree]).toEqual(statuses[i]); diff --git a/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.ts b/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.ts index 9cd29603..2035601c 100644 --- a/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.ts +++ b/docusaurus-search-local/src/client/utils/processTreeStatusOfSearchResults.ts @@ -1,4 +1,7 @@ -import { InitialSearchResult } from "../../shared/interfaces"; +import { + InitialSearchResult, + SearchDocumentType, +} from "../../shared/interfaces"; export function processTreeStatusOfSearchResults( results: InitialSearchResult[] @@ -7,7 +10,14 @@ export function processTreeStatusOfSearchResults( if ( i > 0 && item.page && - results.some((prev) => prev.document === item.page) + results + .slice(0, i) + .some( + (prev) => + (prev.type === SearchDocumentType.Keywords + ? prev.page + : prev.document) === item.page + ) ) { if (i < results.length - 1 && results[i + 1].page === item.page) { item.isInterOfTree = true; diff --git a/docusaurus-search-local/src/client/utils/sortSearchResults.spec.ts b/docusaurus-search-local/src/client/utils/sortSearchResults.spec.ts index 952ce296..9476dc61 100644 --- a/docusaurus-search-local/src/client/utils/sortSearchResults.spec.ts +++ b/docusaurus-search-local/src/client/utils/sortSearchResults.spec.ts @@ -1,4 +1,7 @@ -import { InitialSearchResult } from "../../shared/interfaces"; +import { + InitialSearchResult, + SearchDocumentType, +} from "../../shared/interfaces"; import { sortSearchResults } from "./sortSearchResults"; describe("sortSearchResults", () => { @@ -8,14 +11,14 @@ describe("sortSearchResults", () => { document: { i: 100, }, - type: 0, + type: SearchDocumentType.Title, page: undefined, }, { document: { i: 200, }, - type: 0, + type: SearchDocumentType.Title, page: undefined, }, ] as InitialSearchResult[]; @@ -24,14 +27,14 @@ describe("sortSearchResults", () => { document: { i: 1, }, - type: 2, + type: SearchDocumentType.Content, page: {}, }, { document: { i: 2, }, - type: 1, + type: SearchDocumentType.Heading, page: {}, }, pageTitles[0], @@ -39,21 +42,21 @@ describe("sortSearchResults", () => { document: { i: 3, }, - type: 1, + type: SearchDocumentType.Heading, page: {}, }, { document: { i: 201, }, - type: 1, + type: SearchDocumentType.Heading, page: pageTitles[1].document, }, { document: { i: 202, }, - type: 2, + type: SearchDocumentType.Content, page: pageTitles[1].document, }, pageTitles[1], @@ -61,7 +64,7 @@ describe("sortSearchResults", () => { document: { i: 101, }, - type: 2, + type: SearchDocumentType.Description, page: pageTitles[0].document, }, ] as InitialSearchResult[]; diff --git a/docusaurus-search-local/src/client/utils/sortSearchResults.ts b/docusaurus-search-local/src/client/utils/sortSearchResults.ts index 41509cc8..7c41bdc6 100644 --- a/docusaurus-search-local/src/client/utils/sortSearchResults.ts +++ b/docusaurus-search-local/src/client/utils/sortSearchResults.ts @@ -1,20 +1,30 @@ -import { InitialSearchResult, SearchResult } from "../../shared/interfaces"; +import { + InitialSearchResult, + SearchDocumentType, + SearchResult, +} from "../../shared/interfaces"; export function sortSearchResults(results: InitialSearchResult[]): void { results.forEach((item, index) => { item.index = index; }); - // Put search results of headings and contents just after + // Put search results of headings/contents/descriptions just after // their belonged page's title, if existed. (results as SearchResult[]).sort((a, b) => { let aPageIndex = - a.type > 0 && a.page + (a.type === SearchDocumentType.Heading || + a.type === SearchDocumentType.Content || + a.type === SearchDocumentType.Description) && + a.page ? results.findIndex((item) => item.document === a.page) : a.index; let bPageIndex = - b.type > 0 && b.page + (b.type === SearchDocumentType.Heading || + b.type === SearchDocumentType.Content || + b.type === SearchDocumentType.Description) && + b.page ? results.findIndex((item) => item.document === b.page) : b.index; @@ -27,14 +37,10 @@ export function sortSearchResults(results: InitialSearchResult[]): void { } if (aPageIndex === bPageIndex) { - if (a.type === 0) { - return -1; - } - if (b.type === 0) { - return 1; - } - return a.index - b.index; + const diff = (b.type === 0 ? 1 : 0) - (a.type === 0 ? 1 : 0); + return diff === 0 ? a.index - b.index : diff; } + return aPageIndex - bPageIndex; }); } diff --git a/docusaurus-search-local/src/server/utils/parse.spec.ts b/docusaurus-search-local/src/server/utils/parse.spec.ts index 53d221f7..69eed58f 100644 --- a/docusaurus-search-local/src/server/utils/parse.spec.ts +++ b/docusaurus-search-local/src/server/utils/parse.spec.ts @@ -7,7 +7,11 @@ import { parse } from "./parse"; describe("parse", () => { test.each<[string, "docs" | "blog" | "page", ParsedDocument | null]>([ [ - `
+ ` + + + +