-
Notifications
You must be signed in to change notification settings - Fork 84
/
generate.ts
57 lines (55 loc) · 1.83 KB
/
generate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import fs from "fs";
import path from "path";
import { ProcessedPluginOptions } from "../../shared/interfaces";
import { getIndexHash } from "./getIndexHash";
export function generate(config: ProcessedPluginOptions, dir: string): void {
const { language, searchResultLimits, searchResultContextMaxLength } = config;
const indexHash = getIndexHash(config);
const contents: string[] = [
`import lunr from ${JSON.stringify(require.resolve("lunr"))};`,
];
if (language.length > 1 || language.some((item) => item !== "en")) {
contents.push(
`require(${JSON.stringify(
require.resolve("lunr-languages/lunr.stemmer.support")
)})(lunr);`
);
}
if (language.includes("ja") || language.includes("jp")) {
contents.push(
`require(${JSON.stringify(
require.resolve("lunr-languages/tinyseg")
)})(lunr);`
);
}
for (const lang of language.filter(
(item) => item !== "en" && item !== "zh"
)) {
contents.push(
`require(${JSON.stringify(
require.resolve(`lunr-languages/lunr.${lang}`)
)})(lunr);`
);
}
if (language.includes("zh")) {
contents.push(
'require("@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh").lunrLanguageZh(lunr);'
);
}
if (language.length > 1) {
contents.push(
`require(${JSON.stringify(
require.resolve("lunr-languages/lunr.multi")
)})(lunr);`
);
}
contents.push(`export const language = ${JSON.stringify(language)};`);
contents.push(`export const indexHash = ${JSON.stringify(indexHash)};`);
contents.push(
`export const searchResultLimits = ${JSON.stringify(searchResultLimits)};`,
`export const searchResultContextMaxLength = ${JSON.stringify(
searchResultContextMaxLength
)};`
);
fs.writeFileSync(path.join(dir, "generated.js"), contents.join("\n"));
}