@easyops-cn/docusaurus-search-local 0.37.3 → 0.37.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/server/server/index.js +21 -16
- package/dist/server/server/utils/buildIndex.js +2 -4
- package/dist/server/server/utils/postBuildFactory.js +56 -53
- package/dist/server/server/utils/processDocInfos.js +1 -1
- package/dist/server/server/utils/processPluginOptions.js +3 -2
- package/dist/server/server/utils/scanDocuments.js +41 -38
- package/dist/server/server/utils/tokenizer.js +3 -15
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [0.37.5](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.37.4...v0.37.5) (2023-11-24)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* set typescript target back to ES6 ([c05151f](https://github.com/easyops-cn/docusaurus-search-local/commit/c05151f6b9c02c585f588fb451bf2fd512261e73))
|
|
11
|
+
|
|
12
|
+
## [0.37.4](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.37.3...v0.37.4) (2023-11-12)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* fix peer deps of react 16/17/18 ([700d640](https://github.com/easyops-cn/docusaurus-search-local/commit/700d64068e2e77ec8aca8dc51666d73d25d165fe))
|
|
18
|
+
|
|
5
19
|
## [0.37.3](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.37.2...v0.37.3) (2023-11-12)
|
|
6
20
|
|
|
7
21
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
1
2
|
import path from "path";
|
|
2
3
|
import fs from "fs-extra";
|
|
3
4
|
import { normalizeUrl } from "@docusaurus/utils";
|
|
@@ -22,24 +23,28 @@ export default function DocusaurusSearchLocalPlugin(context, options) {
|
|
|
22
23
|
getPathsToWatch() {
|
|
23
24
|
return [pagePath];
|
|
24
25
|
},
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
getDefaultCodeTranslationMessages() {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const dirPath = path.join(__dirname, "../../locales");
|
|
29
|
+
const localesToTry = codeTranslationLocalesToTry(context.i18n.currentLocale);
|
|
30
|
+
// Return the content of the first file that match
|
|
31
|
+
// fr_FR.json => fr.json => nothing
|
|
32
|
+
for (const locale of localesToTry) {
|
|
33
|
+
const filePath = path.resolve(dirPath, `${locale}.json`);
|
|
34
|
+
if (yield fs.pathExists(filePath)) {
|
|
35
|
+
return fs.readJSON(filePath);
|
|
36
|
+
}
|
|
34
37
|
}
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
return {};
|
|
39
|
+
});
|
|
37
40
|
},
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
contentLoaded({ actions: { addRoute } }) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
addRoute({
|
|
44
|
+
path: normalizeUrl([context.baseUrl, "search"]),
|
|
45
|
+
component: "@theme/SearchPage",
|
|
46
|
+
exact: true,
|
|
47
|
+
});
|
|
43
48
|
});
|
|
44
49
|
},
|
|
45
50
|
};
|
|
@@ -43,11 +43,9 @@ export function buildIndex(allDocuments, { language, removeDefaultStopWordFilter
|
|
|
43
43
|
this.field("t");
|
|
44
44
|
this.metadataWhitelist = ["position"];
|
|
45
45
|
documents.forEach((doc) => {
|
|
46
|
-
this.add({
|
|
47
|
-
...doc,
|
|
46
|
+
this.add(Object.assign(Object.assign({}, doc), {
|
|
48
47
|
// The ref must be a string.
|
|
49
|
-
i: doc.i.toString()
|
|
50
|
-
});
|
|
48
|
+
i: doc.i.toString() }));
|
|
51
49
|
});
|
|
52
50
|
}),
|
|
53
51
|
}));
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
1
2
|
import fs from "fs";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import util from "util";
|
|
@@ -7,65 +8,67 @@ import { processDocInfos } from "./processDocInfos";
|
|
|
7
8
|
import { scanDocuments } from "./scanDocuments";
|
|
8
9
|
const writeFileAsync = util.promisify(fs.writeFile);
|
|
9
10
|
export function postBuildFactory(config, searchIndexFilename) {
|
|
10
|
-
return
|
|
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
|
-
dirAllDocs
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
11
|
+
return function postBuild(buildData) {
|
|
12
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
+
debugInfo("gathering documents");
|
|
14
|
+
const data = processDocInfos(buildData, config);
|
|
15
|
+
debugInfo("parsing documents");
|
|
16
|
+
for (const versionData of data) {
|
|
17
|
+
// Give every index entry a unique id so that the index does not need to store long URLs.
|
|
18
|
+
const allDocuments = yield scanDocuments(versionData.paths, config);
|
|
19
|
+
debugInfo("building index");
|
|
20
|
+
const docsByDirMap = new Map();
|
|
21
|
+
const { searchContextByPaths, hideSearchBarWithNoSearchContext, useAllContextsWithNoSearchContext, } = config;
|
|
22
|
+
if (searchContextByPaths) {
|
|
23
|
+
const { baseUrl } = buildData;
|
|
24
|
+
const rootAllDocs = [];
|
|
25
|
+
if (!hideSearchBarWithNoSearchContext) {
|
|
26
|
+
docsByDirMap.set("", rootAllDocs);
|
|
27
|
+
}
|
|
28
|
+
let docIndex = 0;
|
|
29
|
+
for (const documents of allDocuments) {
|
|
30
|
+
rootAllDocs[docIndex] = [];
|
|
31
|
+
for (const doc of documents) {
|
|
32
|
+
if (doc.u.startsWith(baseUrl)) {
|
|
33
|
+
const uri = doc.u.substring(baseUrl.length);
|
|
34
|
+
const matchedPath = searchContextByPaths.find((path) => uri === path || uri.startsWith(`${path}/`));
|
|
35
|
+
if (matchedPath) {
|
|
36
|
+
let dirAllDocs = docsByDirMap.get(matchedPath);
|
|
37
|
+
if (!dirAllDocs) {
|
|
38
|
+
dirAllDocs = [];
|
|
39
|
+
docsByDirMap.set(matchedPath, dirAllDocs);
|
|
40
|
+
}
|
|
41
|
+
let dirDocs = dirAllDocs[docIndex];
|
|
42
|
+
if (!dirDocs) {
|
|
43
|
+
dirAllDocs[docIndex] = dirDocs = [];
|
|
44
|
+
}
|
|
45
|
+
dirDocs.push(doc);
|
|
46
|
+
if (!useAllContextsWithNoSearchContext) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
46
49
|
}
|
|
47
50
|
}
|
|
51
|
+
rootAllDocs[docIndex].push(doc);
|
|
48
52
|
}
|
|
49
|
-
|
|
53
|
+
docIndex++;
|
|
50
54
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
docsByDirMap.set(k, docsNotEmpty);
|
|
55
|
+
for (const [k, v] of docsByDirMap) {
|
|
56
|
+
const docsNotEmpty = v.filter((d) => !!d);
|
|
57
|
+
if (docsNotEmpty.length < v.length) {
|
|
58
|
+
docsByDirMap.set(k, docsNotEmpty);
|
|
59
|
+
}
|
|
57
60
|
}
|
|
58
61
|
}
|
|
62
|
+
else {
|
|
63
|
+
docsByDirMap.set("", allDocuments);
|
|
64
|
+
}
|
|
65
|
+
for (const [k, allDocs] of docsByDirMap) {
|
|
66
|
+
const searchIndex = buildIndex(allDocs, config);
|
|
67
|
+
debugInfo(`writing index (/${k}) to disk`);
|
|
68
|
+
yield writeFileAsync(path.join(versionData.outDir, searchIndexFilename.replace("{dir}", k === "" ? "" : `-${k.replace(/\//g, "-")}`)), JSON.stringify(searchIndex), { encoding: "utf8" });
|
|
69
|
+
debugInfo(`index (/${k}) written to disk successfully!`);
|
|
70
|
+
}
|
|
59
71
|
}
|
|
60
|
-
|
|
61
|
-
docsByDirMap.set("", allDocuments);
|
|
62
|
-
}
|
|
63
|
-
for (const [k, allDocs] of docsByDirMap) {
|
|
64
|
-
const searchIndex = buildIndex(allDocs, config);
|
|
65
|
-
debugInfo(`writing index (/${k}) to disk`);
|
|
66
|
-
await writeFileAsync(path.join(versionData.outDir, searchIndexFilename.replace("{dir}", k === "" ? "" : `-${k.replace(/\//g, "-")}`)), JSON.stringify(searchIndex), { encoding: "utf8" });
|
|
67
|
-
debugInfo(`index (/${k}) written to disk successfully!`);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
72
|
+
});
|
|
70
73
|
};
|
|
71
74
|
}
|
|
@@ -48,7 +48,7 @@ export function processDocInfos({ routesPaths, outDir, baseUrl, siteConfig, plug
|
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
50
50
|
// ignore files
|
|
51
|
-
if (ignoreFiles
|
|
51
|
+
if (ignoreFiles === null || ignoreFiles === void 0 ? void 0 : ignoreFiles.some((reg) => {
|
|
52
52
|
if (typeof reg === "string") {
|
|
53
53
|
return route === reg;
|
|
54
54
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
export function processPluginOptions(options, { siteDir, siteConfig: { themeConfig }, }) {
|
|
3
|
-
|
|
3
|
+
var _a, _b;
|
|
4
|
+
const config = Object.assign({}, options);
|
|
4
5
|
ensureArray(config, "docsRouteBasePath");
|
|
5
6
|
ensureArray(config, "blogRouteBasePath");
|
|
6
7
|
ensureArray(config, "language");
|
|
@@ -13,7 +14,7 @@ export function processPluginOptions(options, { siteDir, siteConfig: { themeConf
|
|
|
13
14
|
config.docsDir = config.docsDir.map((dir) => path.resolve(siteDir, dir));
|
|
14
15
|
config.blogDir = config.blogDir.map((dir) => path.resolve(siteDir, dir));
|
|
15
16
|
if (config.searchBarPosition === "auto") {
|
|
16
|
-
const search = themeConfig.navbar
|
|
17
|
+
const search = (_b = (_a = themeConfig.navbar) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b.find((item) => item.type === "search");
|
|
17
18
|
config.searchBarPosition =
|
|
18
19
|
search && search.position === "left" ? "left" : "right";
|
|
19
20
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
1
2
|
import fs from "fs";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import util from "util";
|
|
@@ -8,43 +9,45 @@ let nextDocId = 0;
|
|
|
8
9
|
const getNextDocId = () => {
|
|
9
10
|
return (nextDocId += 1);
|
|
10
11
|
};
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
12
|
+
export function scanDocuments(DocInfoWithFilePathList, config) {
|
|
13
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
+
const titleDocuments = [];
|
|
15
|
+
const headingDocuments = [];
|
|
16
|
+
const contentDocuments = [];
|
|
17
|
+
const allDocuments = [titleDocuments, headingDocuments, contentDocuments];
|
|
18
|
+
yield Promise.all(DocInfoWithFilePathList.map(({ filePath, url, type }) => __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
debugVerbose(`parsing %s file %o of %o`, type, path.relative(process.cwd(), filePath), url);
|
|
20
|
+
const html = yield readFileAsync(filePath, { encoding: "utf8" });
|
|
21
|
+
const { pageTitle, sections, breadcrumb } = parse(html, type, url, config);
|
|
22
|
+
const titleId = getNextDocId();
|
|
23
|
+
titleDocuments.push({
|
|
24
|
+
i: titleId,
|
|
25
|
+
t: pageTitle,
|
|
26
|
+
u: url,
|
|
27
|
+
b: breadcrumb,
|
|
28
|
+
});
|
|
29
|
+
for (const section of sections) {
|
|
30
|
+
if (section.title !== pageTitle) {
|
|
31
|
+
headingDocuments.push({
|
|
32
|
+
i: getNextDocId(),
|
|
33
|
+
t: section.title,
|
|
34
|
+
u: url,
|
|
35
|
+
h: section.hash,
|
|
36
|
+
p: titleId,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (section.content) {
|
|
40
|
+
contentDocuments.push({
|
|
41
|
+
i: getNextDocId(),
|
|
42
|
+
t: section.content,
|
|
43
|
+
s: section.title || pageTitle,
|
|
44
|
+
u: url,
|
|
45
|
+
h: section.hash,
|
|
46
|
+
p: titleId,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
36
49
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
t: section.content,
|
|
41
|
-
s: section.title || pageTitle,
|
|
42
|
-
u: url,
|
|
43
|
-
h: section.hash,
|
|
44
|
-
p: titleId,
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}));
|
|
49
|
-
return allDocuments;
|
|
50
|
+
})));
|
|
51
|
+
return allDocuments;
|
|
52
|
+
});
|
|
50
53
|
}
|
|
@@ -38,22 +38,14 @@ export function tokenizer(input, metadata) {
|
|
|
38
38
|
const word = match[0];
|
|
39
39
|
start += match.index;
|
|
40
40
|
if (/\w/.test(word[0])) {
|
|
41
|
-
tokens.push(new lunr.Token(word, {
|
|
42
|
-
...lunr.utils.clone(metadata),
|
|
43
|
-
position: [start, word.length],
|
|
44
|
-
index: tokens.length,
|
|
45
|
-
}));
|
|
41
|
+
tokens.push(new lunr.Token(word, Object.assign(Object.assign({}, lunr.utils.clone(metadata)), { position: [start, word.length], index: tokens.length })));
|
|
46
42
|
// Try to cut `api_gateway` to `api` and `gateway`.
|
|
47
43
|
const subWords = cutWordByUnderscore(word);
|
|
48
44
|
if (subWords.length > 1) {
|
|
49
45
|
let i = 0;
|
|
50
46
|
for (const subWord of subWords) {
|
|
51
47
|
if (subWord[0] !== "_") {
|
|
52
|
-
tokens.push(new lunr.Token(subWord, {
|
|
53
|
-
...lunr.utils.clone(metadata),
|
|
54
|
-
position: [start + i, subWord.length],
|
|
55
|
-
index: tokens.length,
|
|
56
|
-
}));
|
|
48
|
+
tokens.push(new lunr.Token(subWord, Object.assign(Object.assign({}, lunr.utils.clone(metadata)), { position: [start + i, subWord.length], index: tokens.length })));
|
|
57
49
|
}
|
|
58
50
|
i += subWord.length;
|
|
59
51
|
}
|
|
@@ -62,11 +54,7 @@ export function tokenizer(input, metadata) {
|
|
|
62
54
|
}
|
|
63
55
|
else {
|
|
64
56
|
for (const zhWord of jieba.cut(word)) {
|
|
65
|
-
tokens.push(new lunr.Token(zhWord, {
|
|
66
|
-
...lunr.utils.clone(metadata),
|
|
67
|
-
position: [start, zhWord.length],
|
|
68
|
-
index: tokens.length,
|
|
69
|
-
}));
|
|
57
|
+
tokens.push(new lunr.Token(zhWord, Object.assign(Object.assign({}, lunr.utils.clone(metadata)), { position: [start, zhWord.length], index: tokens.length })));
|
|
70
58
|
start += zhWord.length;
|
|
71
59
|
}
|
|
72
60
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easyops-cn/docusaurus-search-local",
|
|
3
|
-
"version": "0.37.
|
|
3
|
+
"version": "0.37.5",
|
|
4
4
|
"description": "An offline/local search plugin for Docusaurus v3",
|
|
5
5
|
"repository": "https://github.com/easyops-cn/docusaurus-search-local",
|
|
6
6
|
"homepage": "https://github.com/easyops-cn/docusaurus-search-local",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
67
|
"@docusaurus/theme-common": "^2 || ^3",
|
|
68
|
-
"react": "^
|
|
69
|
-
"react-dom": "^
|
|
68
|
+
"react": "^16.14.0 || ^17 || ^18",
|
|
69
|
+
"react-dom": "^16.14.0 || 17 || ^18"
|
|
70
70
|
}
|
|
71
71
|
}
|