Skip to content

Commit

Permalink
fix: do not initialize lunr multiple times, fixes #155
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jul 19, 2024
1 parent feea70b commit 0b323d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
55 changes: 33 additions & 22 deletions docusaurus-search-local/src/server/utils/buildIndex.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/* eslint @typescript-eslint/no-var-requires: 0 */
import lunr from "lunr";
import {
LunrWithMultiLanguage,
ProcessedPluginOptions,
SearchDocument,
WrappedIndex,
} from "../../shared/interfaces";

let pluginInitialized = false;
let plugin: lunr.Builder.Plugin | undefined;

export function buildIndex(
allDocuments: SearchDocument[][],
{
Expand All @@ -16,24 +20,33 @@ export function buildIndex(
zhUserDictPath,
}: ProcessedPluginOptions
): Omit<WrappedIndex, "type">[] {
if (language.length > 1 || language.some((item) => item !== "en")) {
require("lunr-languages/lunr.stemmer.support")(lunr);
}
if (language.includes("ja") || language.includes("jp")) {
require("lunr-languages/tinyseg")(lunr);
}
for (const lang of language.filter(
(item) => item !== "en" && item !== "zh"
)) {
require(`lunr-languages/lunr.${lang}`)(lunr);
}
if (language.includes("zh")) {
const { tokenizer, loadUserDict } = require("./tokenizer");
loadUserDict(zhUserDict, zhUserDictPath);
require("../../shared/lunrLanguageZh").lunrLanguageZh(lunr, tokenizer);
}
if (language.length > 1) {
require("lunr-languages/lunr.multi")(lunr);
if (!pluginInitialized) {
pluginInitialized = true;
if (language.length > 1 || language.some((item) => item !== "en")) {
require("lunr-languages/lunr.stemmer.support")(lunr);
}
if (language.includes("ja") || language.includes("jp")) {
require("lunr-languages/tinyseg")(lunr);
}
for (const lang of language.filter(
(item) => item !== "en" && item !== "zh"
)) {
require(`lunr-languages/lunr.${lang}`)(lunr);
}
if (language.includes("zh")) {
const { tokenizer, loadUserDict } = require("./tokenizer");
loadUserDict(zhUserDict, zhUserDictPath);
require("../../shared/lunrLanguageZh").lunrLanguageZh(lunr, tokenizer);
}
if (language.length > 1) {
require("lunr-languages/lunr.multi")(lunr);
}

if (language.length > 1) {
plugin = (lunr as LunrWithMultiLanguage).multiLanguage(...language);
} else if (language[0] !== "en") {
plugin = (lunr as LunrWithMultiLanguage)[language[0] as "zh"];
}
}

// Some documents may be empty (unset array item), which is not mapped.
Expand All @@ -43,10 +56,8 @@ export function buildIndex(
.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 (plugin) {
this.use(plugin);
}

if (removeDefaultStopWordFilter) {
Expand Down
7 changes: 7 additions & 0 deletions docusaurus-search-local/src/shared/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,10 @@ export interface DocusaurusContext {
};
};
}

type Lunr = typeof lunr;

export interface LunrWithMultiLanguage extends Lunr {
multiLanguage: (...languages: string[]) => lunr.Builder.Plugin;
zh: lunr.Builder.Plugin;
}

0 comments on commit 0b323d9

Please sign in to comment.