@easyops-cn/docusaurus-search-local 0.28.0 → 0.29.2
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 +24 -0
- package/dist/client/client/theme/SearchBar/SearchBar.jsx +33 -13
- package/dist/client/client/theme/SearchBar/SearchBar.module.css +1 -1
- package/dist/client/client/theme/SearchPage/SearchPage.jsx +21 -5
- package/dist/client/client/utils/__mocks__/proxiedGenerated.js +1 -0
- package/dist/server/server/utils/generate.js +6 -1
- package/dist/server/server/utils/postBuildFactory.js +1 -1
- package/dist/server/server/utils/processDocInfos.js +19 -11
- package/dist/server/server/utils/validateOptions.js +3 -0
- package/package.json +11 -11
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,30 @@
|
|
|
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.29.2](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.29.1...v0.29.2) (2022-07-19)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* fix website with docs multi-instance ([f61304e](https://github.com/easyops-cn/docusaurus-search-local/commit/f61304e5f3318388d56c1ef791881cd3e3c6e331)), closes [#211](https://github.com/easyops-cn/docusaurus-search-local/issues/211)
|
|
11
|
+
|
|
12
|
+
## [0.29.1](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.29.0...v0.29.1) (2022-07-19)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* require docusaurus v2.0.0-rc.1 ([41a4e67](https://github.com/easyops-cn/docusaurus-search-local/commit/41a4e67543b03f4166589da84cac76a196488e3d))
|
|
18
|
+
* add hack for useDocsPreferredVersion crash in SearchPage ([02fdc42](https://github.com/easyops-cn/docusaurus-search-local/commit/02fdc42a043a4cc1d3fafe9705b478e883543d2b))
|
|
19
|
+
* fix error when not using versions ([f11293f](https://github.com/easyops-cn/docusaurus-search-local/commit/f11293f78c111948c3ea3b8c25a8bb782304ee93))
|
|
20
|
+
|
|
21
|
+
## [0.29.0](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.28.0...v0.29.0) (2022-07-07)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
* support disabling search bar shortcut ([e0ea5ae](https://github.com/easyops-cn/docusaurus-search-local/commit/e0ea5aeab027724083b0f51290d6ea7e4bc89fa6))
|
|
27
|
+
* support hiding search bar shortcut hint ([9344bd2](https://github.com/easyops-cn/docusaurus-search-local/commit/9344bd20e16c3dd60b3d59fc918dca10e8cfd48f))
|
|
28
|
+
|
|
5
29
|
## [0.28.0](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.27.2...v0.28.0) (2022-06-28)
|
|
6
30
|
|
|
7
31
|
|
|
@@ -4,13 +4,13 @@ import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
|
|
|
4
4
|
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
|
|
5
5
|
import { useHistory, useLocation } from "@docusaurus/router";
|
|
6
6
|
import { translate } from "@docusaurus/Translate";
|
|
7
|
-
import { useDocsPreferredVersion } from "@docusaurus/theme-common";
|
|
7
|
+
import { ReactContextError, useDocsPreferredVersion, } from "@docusaurus/theme-common";
|
|
8
8
|
import { useActivePlugin } from "@docusaurus/plugin-content-docs/client";
|
|
9
9
|
import { fetchIndexes } from "./fetchIndexes";
|
|
10
10
|
import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
|
|
11
11
|
import { SuggestionTemplate } from "./SuggestionTemplate";
|
|
12
12
|
import { EmptyTemplate } from "./EmptyTemplate";
|
|
13
|
-
import { searchResultLimits, Mark } from "../../utils/proxiedGenerated";
|
|
13
|
+
import { searchResultLimits, Mark, searchBarShortcut, searchBarShortcutHint, docsPluginIdForPreferredVersion, } from "../../utils/proxiedGenerated";
|
|
14
14
|
import LoadingRing from "../LoadingRing/LoadingRing";
|
|
15
15
|
import styles from "./SearchBar.module.css";
|
|
16
16
|
async function fetchAutoCompleteJS() {
|
|
@@ -36,9 +36,24 @@ export default function SearchBar({ handleSearchBarToggle, }) {
|
|
|
36
36
|
// this will throw an error of:
|
|
37
37
|
// > Docusaurus plugin global data not found for "docusaurus-plugin-content-docs" plugin with id "default".
|
|
38
38
|
// It seems that we can not get the correct id for non-docs pages.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
try {
|
|
40
|
+
// The try-catch is a hack because useDocsPreferredVersion just throws an
|
|
41
|
+
// exception when versions are not used.
|
|
42
|
+
// The same hack is used in SearchPage.tsx
|
|
43
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
44
|
+
const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId ?? docsPluginIdForPreferredVersion);
|
|
45
|
+
if (preferredVersion && !preferredVersion.isLast) {
|
|
46
|
+
versionUrl = preferredVersion.path + "/";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
if (e instanceof ReactContextError) {
|
|
51
|
+
console.error("useDocsPreferredVersion", e);
|
|
52
|
+
/* ignore, happens when website doesn't use versions */
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
42
57
|
}
|
|
43
58
|
const history = useHistory();
|
|
44
59
|
const location = useLocation();
|
|
@@ -185,14 +200,17 @@ export default function SearchBar({ handleSearchBarToggle, }) {
|
|
|
185
200
|
? /mac/i.test(navigator.userAgentData?.platform ?? navigator.platform)
|
|
186
201
|
: false;
|
|
187
202
|
useEffect(() => {
|
|
203
|
+
if (!searchBarShortcut) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
188
206
|
// Add shortcuts command/ctrl + K
|
|
189
|
-
|
|
207
|
+
const handleShortcut = (event) => {
|
|
190
208
|
if ((isMac ? event.metaKey : event.ctrlKey) && event.code === "KeyK") {
|
|
191
209
|
event.preventDefault();
|
|
192
210
|
searchBarRef.current?.focus();
|
|
193
211
|
onInputFocus();
|
|
194
212
|
}
|
|
195
|
-
}
|
|
213
|
+
};
|
|
196
214
|
document.addEventListener("keydown", handleShortcut);
|
|
197
215
|
return () => {
|
|
198
216
|
document.removeEventListener("keydown", handleShortcut);
|
|
@@ -221,11 +239,13 @@ export default function SearchBar({ handleSearchBarToggle, }) {
|
|
|
221
239
|
description: "The ARIA label and placeholder for search button",
|
|
222
240
|
})} aria-label="Search" className="navbar__search-input" onMouseEnter={onInputMouseEnter} onFocus={onInputFocus} onBlur={onInputBlur} onChange={onInputChange} ref={searchBarRef} value={inputValue}/>
|
|
223
241
|
<LoadingRing className={styles.searchBarLoadingRing}/>
|
|
224
|
-
{
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
<
|
|
229
|
-
|
|
242
|
+
{searchBarShortcut &&
|
|
243
|
+
searchBarShortcutHint &&
|
|
244
|
+
(inputValue !== "" ? (<button className={styles.searchClearButton} onClick={onClearSearch}>
|
|
245
|
+
✕
|
|
246
|
+
</button>) : (<div className={styles.searchHintContainer}>
|
|
247
|
+
<kbd className={styles.searchHint}>{isMac ? "⌘" : "ctrl"}</kbd>
|
|
248
|
+
<kbd className={styles.searchHint}>K</kbd>
|
|
249
|
+
</div>))}
|
|
230
250
|
</div>);
|
|
231
251
|
}
|
|
@@ -231,7 +231,7 @@ html[data-theme="dark"] .noResultsIcon {
|
|
|
231
231
|
|
|
232
232
|
.searchHint {
|
|
233
233
|
color: var(--ifm-navbar-search-input-placeholder-color);
|
|
234
|
-
background-color: var(--ifm-navbar-background-color);
|
|
234
|
+
background-color: var(--ifm-navbar-search-input-background-color);
|
|
235
235
|
border: 1px solid var(--ifm-color-emphasis-500);
|
|
236
236
|
box-shadow: inset 0 -1px 0 var(--ifm-color-emphasis-500);
|
|
237
237
|
}
|
|
@@ -4,7 +4,7 @@ import Layout from "@theme/Layout";
|
|
|
4
4
|
import Head from "@docusaurus/Head";
|
|
5
5
|
import Link from "@docusaurus/Link";
|
|
6
6
|
import { translate } from "@docusaurus/Translate";
|
|
7
|
-
import { usePluralForm, useDocsPreferredVersion, } from "@docusaurus/theme-common";
|
|
7
|
+
import { usePluralForm, useDocsPreferredVersion, ReactContextError, } from "@docusaurus/theme-common";
|
|
8
8
|
import { useActivePlugin } from "@docusaurus/plugin-content-docs/client";
|
|
9
9
|
import useSearchQuery from "../hooks/useSearchQuery";
|
|
10
10
|
import { fetchIndexes } from "../SearchBar/fetchIndexes";
|
|
@@ -13,8 +13,9 @@ import { highlight } from "../../utils/highlight";
|
|
|
13
13
|
import { highlightStemmed } from "../../utils/highlightStemmed";
|
|
14
14
|
import { getStemmedPositions } from "../../utils/getStemmedPositions";
|
|
15
15
|
import LoadingRing from "../LoadingRing/LoadingRing";
|
|
16
|
-
import styles from "./SearchPage.module.css";
|
|
17
16
|
import { concatDocumentPath } from "../../utils/concatDocumentPath";
|
|
17
|
+
import { docsPluginIdForPreferredVersion } from "../../utils/proxiedGenerated";
|
|
18
|
+
import styles from "./SearchPage.module.css";
|
|
18
19
|
export default function SearchPage() {
|
|
19
20
|
return (<Layout>
|
|
20
21
|
<SearchPageContent />
|
|
@@ -26,9 +27,24 @@ function SearchPageContent() {
|
|
|
26
27
|
const activePlugin = useActivePlugin();
|
|
27
28
|
let versionUrl = baseUrl;
|
|
28
29
|
// There is an issue, see `SearchBar.tsx`.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
try {
|
|
31
|
+
// The try-catch is a hack because useDocsPreferredVersion just throws an
|
|
32
|
+
// exception when versions are not used.
|
|
33
|
+
// The same hack is used in SearchBar.tsx
|
|
34
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
35
|
+
const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId ?? docsPluginIdForPreferredVersion);
|
|
36
|
+
if (preferredVersion && !preferredVersion.isLast) {
|
|
37
|
+
versionUrl = preferredVersion.path + "/";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
if (e instanceof ReactContextError) {
|
|
42
|
+
console.error("useDocsPreferredVersion", e);
|
|
43
|
+
/* ignore, happens when website doesn't use versions */
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
32
48
|
}
|
|
33
49
|
const { selectMessage } = usePluralForm();
|
|
34
50
|
const { searchValue, updateSearchPath } = useSearchQuery();
|
|
@@ -5,6 +5,7 @@ export const indexHash = "abc";
|
|
|
5
5
|
export const searchResultLimits = 8;
|
|
6
6
|
export const searchResultContextMaxLength = 50;
|
|
7
7
|
export const explicitSearchResultPath = false;
|
|
8
|
+
export const docsPluginIdForPreferredVersion = undefined;
|
|
8
9
|
export function __setLanguage(value) {
|
|
9
10
|
language = value;
|
|
10
11
|
}
|
|
@@ -6,7 +6,7 @@ const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
|
6
6
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
7
7
|
const getIndexHash_1 = require("./getIndexHash");
|
|
8
8
|
function generate(config, dir) {
|
|
9
|
-
const { language, removeDefaultStopWordFilter, removeDefaultStemmer, highlightSearchTermsOnTargetPage, searchResultLimits, searchResultContextMaxLength, explicitSearchResultPath, } = config;
|
|
9
|
+
const { language, removeDefaultStopWordFilter, removeDefaultStemmer, highlightSearchTermsOnTargetPage, searchResultLimits, searchResultContextMaxLength, explicitSearchResultPath, searchBarShortcut, searchBarShortcutHint, docsPluginIdForPreferredVersion, } = config;
|
|
10
10
|
const indexHash = (0, getIndexHash_1.getIndexHash)(config);
|
|
11
11
|
const contents = [
|
|
12
12
|
`import lunr from ${JSON.stringify(require.resolve("lunr"))};`,
|
|
@@ -38,6 +38,11 @@ function generate(config, dir) {
|
|
|
38
38
|
contents.push(`export const indexHash = ${JSON.stringify(indexHash)};`);
|
|
39
39
|
contents.push(`export const searchResultLimits = ${JSON.stringify(searchResultLimits)};`, `export const searchResultContextMaxLength = ${JSON.stringify(searchResultContextMaxLength)};`);
|
|
40
40
|
contents.push(`export const explicitSearchResultPath = ${JSON.stringify(explicitSearchResultPath)};`);
|
|
41
|
+
contents.push(`export const searchBarShortcut = ${JSON.stringify(searchBarShortcut)};`);
|
|
42
|
+
contents.push(`export const searchBarShortcutHint = ${JSON.stringify(searchBarShortcutHint)};`);
|
|
43
|
+
contents.push(`export const docsPluginIdForPreferredVersion = ${docsPluginIdForPreferredVersion === undefined
|
|
44
|
+
? "undefined"
|
|
45
|
+
: JSON.stringify(docsPluginIdForPreferredVersion)};`);
|
|
41
46
|
fs_1.default.writeFileSync(path_1.default.join(dir, "generated.js"), contents.join("\n"));
|
|
42
47
|
}
|
|
43
48
|
exports.generate = generate;
|
|
@@ -16,7 +16,7 @@ function postBuildFactory(config) {
|
|
|
16
16
|
(0, debug_1.debugInfo)("gathering documents");
|
|
17
17
|
const data = (0, processDocInfos_1.processDocInfos)(buildData, config);
|
|
18
18
|
(0, debug_1.debugInfo)("parsing documents");
|
|
19
|
-
for (
|
|
19
|
+
for (const versionData of data) {
|
|
20
20
|
// Give every index entry a unique id so that the index does not need to store long URLs.
|
|
21
21
|
const allDocuments = yield (0, scanDocuments_1.scanDocuments)(versionData.paths);
|
|
22
22
|
(0, debug_1.debugInfo)("building index");
|
|
@@ -3,32 +3,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.processDocInfos = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
6
|
+
const debug_1 = require("./debug");
|
|
6
7
|
function processDocInfos({ routesPaths, outDir, baseUrl, siteConfig, plugins }, { indexDocs, indexBlog, indexPages, docsRouteBasePath, blogRouteBasePath, ignoreFiles, }) {
|
|
7
8
|
const emptySet = new Set();
|
|
8
|
-
let versionData = [
|
|
9
|
+
let versionData = new Map([[outDir, emptySet]]);
|
|
9
10
|
if (plugins) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
// Handle docs multi-instance.
|
|
12
|
+
const docsPlugins = plugins.filter((item) => item.name === "docusaurus-plugin-content-docs");
|
|
13
|
+
const loadedVersions = docsPlugins.flatMap((plugin) => plugin.content.loadedVersions);
|
|
14
|
+
(0, debug_1.debugVerbose)("loadedVersions:", loadedVersions);
|
|
15
|
+
if (loadedVersions.length > 0) {
|
|
16
|
+
versionData = new Map();
|
|
14
17
|
for (const loadedVersion of loadedVersions) {
|
|
15
|
-
const docs = new Set();
|
|
16
|
-
for (const doc of loadedVersion.docs) {
|
|
17
|
-
docs.add(doc.permalink);
|
|
18
|
-
}
|
|
19
18
|
const route = loadedVersion.path.substr(baseUrl.length);
|
|
20
19
|
let versionOutDir = outDir;
|
|
21
20
|
// The last versions search-index should always be placed in the root since it is the one used from non-docs pages
|
|
22
21
|
if (!loadedVersion.isLast) {
|
|
23
22
|
versionOutDir = path_1.default.join(outDir, ...route.split("/").filter((i) => i));
|
|
24
23
|
}
|
|
25
|
-
|
|
24
|
+
// Merge docs which share the same `versionOutDir`.
|
|
25
|
+
let docs = versionData.get(versionOutDir);
|
|
26
|
+
if (!docs) {
|
|
27
|
+
docs = new Set();
|
|
28
|
+
versionData.set(versionOutDir, docs);
|
|
29
|
+
}
|
|
30
|
+
for (const doc of loadedVersion.docs) {
|
|
31
|
+
docs.add(doc.permalink);
|
|
32
|
+
}
|
|
26
33
|
}
|
|
34
|
+
(0, debug_1.debugVerbose)("versionData:", versionData);
|
|
27
35
|
}
|
|
28
36
|
}
|
|
29
37
|
// Create a list of files to index per document version. This will always include all pages and blogs.
|
|
30
38
|
const result = [];
|
|
31
|
-
for (const
|
|
39
|
+
for (const [versionOutDir, docs] of versionData.entries()) {
|
|
32
40
|
const versionPaths = routesPaths
|
|
33
41
|
.map((url) => {
|
|
34
42
|
// istanbul ignore next
|
|
@@ -21,6 +21,9 @@ const schema = utils_validation_1.Joi.object({
|
|
|
21
21
|
searchResultContextMaxLength: utils_validation_1.Joi.number().default(50),
|
|
22
22
|
explicitSearchResultPath: utils_validation_1.Joi.boolean().default(false),
|
|
23
23
|
ignoreFiles: isArrayOfStringsOrRegExpsOrStringOrRegExp.default([]),
|
|
24
|
+
searchBarShortcut: utils_validation_1.Joi.boolean().default(true),
|
|
25
|
+
searchBarShortcutHint: utils_validation_1.Joi.boolean().default(true),
|
|
26
|
+
docsPluginIdForPreferredVersion: utils_validation_1.Joi.string(),
|
|
24
27
|
});
|
|
25
28
|
function validateOptions({ options, validate, }) {
|
|
26
29
|
return validate(schema, options || {});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easyops-cn/docusaurus-search-local",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.29.2",
|
|
4
4
|
"description": "An offline/local search plugin for Docusaurus v2",
|
|
5
5
|
"repository": "https://github.com/easyops-cn/docusaurus-search-local",
|
|
6
6
|
"homepage": "https://github.com/easyops-cn/docusaurus-search-local",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@docusaurus/plugin-content-docs": "^2.0.0-
|
|
27
|
-
"@docusaurus/theme-translations": "^2.0.0-
|
|
28
|
-
"@docusaurus/utils": "^2.0.0-
|
|
29
|
-
"@docusaurus/utils-common": "^2.0.0-
|
|
30
|
-
"@docusaurus/utils-validation": "^2.0.0-
|
|
26
|
+
"@docusaurus/plugin-content-docs": "^2.0.0-rc.1",
|
|
27
|
+
"@docusaurus/theme-translations": "^2.0.0-rc.1",
|
|
28
|
+
"@docusaurus/utils": "^2.0.0-rc.1",
|
|
29
|
+
"@docusaurus/utils-common": "^2.0.0-rc.1",
|
|
30
|
+
"@docusaurus/utils-validation": "^2.0.0-rc.1",
|
|
31
31
|
"@easyops-cn/autocomplete.js": "^0.38.1",
|
|
32
32
|
"@node-rs/jieba": "^1.6.0",
|
|
33
33
|
"cheerio": "^1.0.0-rc.3",
|
|
@@ -41,9 +41,9 @@
|
|
|
41
41
|
"tslib": "^2.4.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@docusaurus/module-type-aliases": "^2.0.0-
|
|
45
|
-
"@docusaurus/theme-common": "^2.0.0-
|
|
46
|
-
"@docusaurus/types": "^2.0.0-
|
|
44
|
+
"@docusaurus/module-type-aliases": "^2.0.0-rc.1",
|
|
45
|
+
"@docusaurus/theme-common": "^2.0.0-rc.1",
|
|
46
|
+
"@docusaurus/types": "^2.0.0-rc.1",
|
|
47
47
|
"@tsconfig/docusaurus": "^1.0.2",
|
|
48
48
|
"@types/cheerio": "^0.22.31",
|
|
49
49
|
"@types/debug": "^4.1.5",
|
|
@@ -58,10 +58,10 @@
|
|
|
58
58
|
"concurrently": "^7.0.0",
|
|
59
59
|
"copyfiles": "^2.4.0",
|
|
60
60
|
"rimraf": "^3.0.2",
|
|
61
|
-
"typescript": "^4.
|
|
61
|
+
"typescript": "^4.7.4"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"@docusaurus/theme-common": "^2.0.0-
|
|
64
|
+
"@docusaurus/theme-common": "^2.0.0-rc.1",
|
|
65
65
|
"react": "^16.14.0 || ^17.0.0 || ^18.0.0",
|
|
66
66
|
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0"
|
|
67
67
|
}
|