diff --git a/docusaurus-search-local/src/server/utils/buildIndex.ts b/docusaurus-search-local/src/server/utils/buildIndex.ts index 1c16be47..a18319f2 100644 --- a/docusaurus-search-local/src/server/utils/buildIndex.ts +++ b/docusaurus-search-local/src/server/utils/buildIndex.ts @@ -36,41 +36,45 @@ export function buildIndex( require("lunr-languages/lunr.multi")(lunr); } - return allDocuments.map((documents) => ({ - documents, - index: lunr(function () { - if (language.length > 1) { - this.use((lunr as any).multiLanguage(...language)); - } else if (language[0] !== "en") { - this.use((lunr as any)[language[0]]); - } + // Some documents may be empty (unset array item), which is not mapped. + return new Array(allDocuments.length) + .fill(null) + .map((_doc, index) => allDocuments[index] ?? []) + .map((documents) => ({ + documents, + index: lunr(function () { + if (language.length > 1) { + this.use((lunr as any).multiLanguage(...language)); + } else if (language[0] !== "en") { + this.use((lunr as any)[language[0]]); + } - if (removeDefaultStopWordFilter) { - // Sometimes we need no English stop words, - // since they are almost all programming code. - this.pipeline.remove(lunr.stopWordFilter); - } + if (removeDefaultStopWordFilter) { + // Sometimes we need no English stop words, + // since they are almost all programming code. + this.pipeline.remove(lunr.stopWordFilter); + } - if (removeDefaultStemmer) { - this.pipeline.remove(lunr.stemmer); - } + if (removeDefaultStemmer) { + this.pipeline.remove(lunr.stemmer); + } - // Override tokenizer when language `zh` is enabled. - if (language.includes("zh")) { - this.tokenizer = (lunr as any).zh.tokenizer; - } + // Override tokenizer when language `zh` is enabled. + if (language.includes("zh")) { + this.tokenizer = (lunr as any).zh.tokenizer; + } - this.ref("i"); - this.field("t"); - this.metadataWhitelist = ["position"]; + this.ref("i"); + this.field("t"); + this.metadataWhitelist = ["position"]; - documents.forEach((doc) => { - this.add({ - ...doc, - // The ref must be a string. - i: doc.i.toString(), + documents.forEach((doc) => { + this.add({ + ...doc, + // The ref must be a string. + i: doc.i.toString(), + }); }); - }); - }), - })); + }), + })); } diff --git a/docusaurus-search-local/src/server/utils/postBuildFactory.ts b/docusaurus-search-local/src/server/utils/postBuildFactory.ts index ab4bc917..689118c1 100644 --- a/docusaurus-search-local/src/server/utils/postBuildFactory.ts +++ b/docusaurus-search-local/src/server/utils/postBuildFactory.ts @@ -48,17 +48,17 @@ export function postBuildFactory( for (const doc of documents) { if (doc.u.startsWith(baseUrl)) { const uri = doc.u.substring(baseUrl.length); - let matchedPathes: string[] = []; + const matchedPaths: string[] = []; for (const _path of searchContextByPaths) { const path = typeof _path === "string" ? _path : _path.path; if (uri === path || uri.startsWith(`${path}/`)) { - matchedPathes.push(path); + matchedPaths.push(path); } } - for (const matchedPath of matchedPathes) { + for (const matchedPath of matchedPaths) { let dirAllDocs = docsByDirMap.get(matchedPath); if (!dirAllDocs) { - dirAllDocs = []; + dirAllDocs = new Array(allDocuments.length); docsByDirMap.set(matchedPath, dirAllDocs); } let dirDocs = dirAllDocs[docIndex]; @@ -67,7 +67,10 @@ export function postBuildFactory( } dirDocs.push(doc); } - if (matchedPathes.length > 0 && !useAllContextsWithNoSearchContext) { + if ( + matchedPaths.length > 0 && + !useAllContextsWithNoSearchContext + ) { continue; } } @@ -75,12 +78,6 @@ export function postBuildFactory( } docIndex++; } - for (const [k, v] of docsByDirMap) { - const docsNotEmpty = v.filter((d) => !!d); - if (docsNotEmpty.length < v.length) { - docsByDirMap.set(k, docsNotEmpty); - } - } } else { docsByDirMap.set("", allDocuments); }