Skip to content

Commit

Permalink
fix: fix searching within context closes #424
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jun 19, 2024
1 parent d489e05 commit bbec667
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
66 changes: 35 additions & 31 deletions docusaurus-search-local/src/server/utils/buildIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SearchDocument[] | null>(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(),
});
});
});
}),
}));
}),
}));
}
19 changes: 8 additions & 11 deletions docusaurus-search-local/src/server/utils/postBuildFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -67,20 +67,17 @@ export function postBuildFactory(
}
dirDocs.push(doc);
}
if (matchedPathes.length > 0 && !useAllContextsWithNoSearchContext) {
if (
matchedPaths.length > 0 &&
!useAllContextsWithNoSearchContext
) {
continue;
}
}
rootAllDocs[docIndex].push(doc);
}
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);
}
Expand Down

0 comments on commit bbec667

Please sign in to comment.