@easyops-cn/docusaurus-search-local 0.30.1 → 0.32.0

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 CHANGED
@@ -2,6 +2,28 @@
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.32.0](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.31.0...v0.32.0) (2022-09-05)
6
+
7
+
8
+ ### Features
9
+
10
+ * support setting searchBarPosition ([60b27d4](https://github.com/easyops-cn/docusaurus-search-local/commit/60b27d4a6e1f3342ac7afda586202fc5d7b8a3bc)), closes [#263](https://github.com/easyops-cn/docusaurus-search-local/issues/263)
11
+
12
+ ## [0.31.0](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.30.2...v0.31.0) (2022-08-08)
13
+
14
+
15
+ ### Features
16
+
17
+ * support `zhUserDict` and `zhUserDictPath` ([33f1ea4](https://github.com/easyops-cn/docusaurus-search-local/commit/33f1ea4765b0e134689fd65aa655c2bc1b4e6f0b)), closes [#246](https://github.com/easyops-cn/docusaurus-search-local/issues/246)
18
+
19
+ ## [0.30.2](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.30.1...v0.30.2) (2022-07-23)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * remove useDocsPreferredVersion error log entirely ([56f5ab5](https://github.com/easyops-cn/docusaurus-search-local/commit/56f5ab51a77f682c565d357bcbf7a37567c2b21e))
25
+ * use debug instead of console.error for useDocsPreferredVersion message ([3a09e39](https://github.com/easyops-cn/docusaurus-search-local/commit/3a09e39ef569a9dd1d99b1e251fdc79f909cff0f))
26
+
5
27
  ## [0.30.1](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.30.0...v0.30.1) (2022-07-22)
6
28
 
7
29
 
@@ -10,7 +10,7 @@ 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, searchBarShortcut, searchBarShortcutHint, docsPluginIdForPreferredVersion, indexDocs, } from "../../utils/proxiedGenerated";
13
+ import { searchResultLimits, Mark, searchBarShortcut, searchBarShortcutHint, searchBarPosition, docsPluginIdForPreferredVersion, indexDocs, } from "../../utils/proxiedGenerated";
14
14
  import LoadingRing from "../LoadingRing/LoadingRing";
15
15
  import styles from "./SearchBar.module.css";
16
16
  async function fetchAutoCompleteJS() {
@@ -49,7 +49,6 @@ export default function SearchBar({ handleSearchBarToggle, }) {
49
49
  catch (e) {
50
50
  if (indexDocs) {
51
51
  if (e instanceof ReactContextError) {
52
- console.error("useDocsPreferredVersion", e);
53
52
  /* ignore, happens when website doesn't use versions */
54
53
  }
55
54
  else {
@@ -83,7 +82,9 @@ export default function SearchBar({ handleSearchBarToggle, }) {
83
82
  autoselect: true,
84
83
  openOnFocus: true,
85
84
  cssClasses: {
86
- root: styles.searchBar,
85
+ root: clsx(styles.searchBar, {
86
+ [styles.searchBarLeft]: searchBarPosition === "left",
87
+ }),
87
88
  noPrefix: true,
88
89
  dropdownMenu: styles.dropdownMenu,
89
90
  input: styles.input,
@@ -16,6 +16,13 @@
16
16
  padding: var(--search-local-spacing, 12px);
17
17
  }
18
18
 
19
+ @media not (max-width: 996px) {
20
+ .searchBar.searchBarLeft .dropdownMenu {
21
+ left: 0 !important;
22
+ right: auto !important;
23
+ }
24
+ }
25
+
19
26
  @media (max-width: 576px) {
20
27
  :global(.navbar__search-input):not(:focus) {
21
28
  width: 2rem;
@@ -40,7 +40,6 @@ function SearchPageContent() {
40
40
  catch (e) {
41
41
  if (indexDocs) {
42
42
  if (e instanceof ReactContextError) {
43
- console.error("useDocsPreferredVersion", e);
44
43
  /* ignore, happens when website doesn't use versions */
45
44
  }
46
45
  else {
@@ -11,7 +11,7 @@ const postBuildFactory_1 = require("./utils/postBuildFactory");
11
11
  const generate_1 = require("./utils/generate");
12
12
  const PLUGIN_NAME = "@easyops-cn/docusaurus-search-local";
13
13
  function DocusaurusSearchLocalPlugin(context, options) {
14
- const config = (0, processPluginOptions_1.processPluginOptions)(options, context.siteDir);
14
+ const config = (0, processPluginOptions_1.processPluginOptions)(options, context);
15
15
  const dir = path_1.default.join(context.generatedFilesDir, PLUGIN_NAME, "default");
16
16
  fs_extra_1.default.ensureDirSync(dir);
17
17
  const searchIndexFilename = (0, generate_1.generate)(config, dir);
@@ -2,28 +2,24 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildIndex = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ /* eslint @typescript-eslint/no-var-requires: 0 */
5
6
  const lunr_1 = tslib_1.__importDefault(require("lunr"));
6
- function buildIndex(allDocuments, { language, removeDefaultStopWordFilter, removeDefaultStemmer, }) {
7
+ function buildIndex(allDocuments, { language, removeDefaultStopWordFilter, removeDefaultStemmer, zhUserDict, zhUserDictPath, }) {
7
8
  if (language.length > 1 || language.some((item) => item !== "en")) {
8
- // eslint-disable-next-line @typescript-eslint/no-var-requires
9
9
  require("lunr-languages/lunr.stemmer.support")(lunr_1.default);
10
10
  }
11
11
  if (language.includes("ja") || language.includes("jp")) {
12
- // eslint-disable-next-line @typescript-eslint/no-var-requires
13
12
  require("lunr-languages/tinyseg")(lunr_1.default);
14
13
  }
15
14
  for (const lang of language.filter((item) => item !== "en" && item !== "zh")) {
16
- // eslint-disable-next-line @typescript-eslint/no-var-requires
17
15
  require(`lunr-languages/lunr.${lang}`)(lunr_1.default);
18
16
  }
19
17
  if (language.includes("zh")) {
20
- // eslint-disable-next-line @typescript-eslint/no-var-requires
21
- require("../../shared/lunrLanguageZh").lunrLanguageZh(lunr_1.default,
22
- // eslint-disable-next-line @typescript-eslint/no-var-requires
23
- require("./tokenizer").tokenizer);
18
+ const { tokenizer, loadUserDict } = require("./tokenizer");
19
+ loadUserDict(zhUserDict, zhUserDictPath);
20
+ require("../../shared/lunrLanguageZh").lunrLanguageZh(lunr_1.default, tokenizer);
24
21
  }
25
22
  if (language.length > 1) {
26
- // eslint-disable-next-line @typescript-eslint/no-var-requires
27
23
  require("lunr-languages/lunr.multi")(lunr_1.default);
28
24
  }
29
25
  return allDocuments.map((documents) => ({
@@ -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, searchBarShortcut, searchBarShortcutHint, docsPluginIdForPreferredVersion, indexDocs, } = config;
9
+ const { language, removeDefaultStopWordFilter, removeDefaultStemmer, highlightSearchTermsOnTargetPage, searchResultLimits, searchResultContextMaxLength, explicitSearchResultPath, searchBarShortcut, searchBarShortcutHint, searchBarPosition, docsPluginIdForPreferredVersion, indexDocs, } = config;
10
10
  const indexHash = (0, getIndexHash_1.getIndexHash)(config);
11
11
  const contents = [
12
12
  `import lunr from ${JSON.stringify(require.resolve("lunr"))};`,
@@ -51,6 +51,7 @@ function generate(config, dir) {
51
51
  contents.push(`export const explicitSearchResultPath = ${JSON.stringify(explicitSearchResultPath)};`);
52
52
  contents.push(`export const searchBarShortcut = ${JSON.stringify(searchBarShortcut)};`);
53
53
  contents.push(`export const searchBarShortcutHint = ${JSON.stringify(searchBarShortcutHint)};`);
54
+ contents.push(`export const searchBarPosition = ${JSON.stringify(searchBarPosition)};`);
54
55
  contents.push(`export const docsPluginIdForPreferredVersion = ${docsPluginIdForPreferredVersion === undefined
55
56
  ? "undefined"
56
57
  : JSON.stringify(docsPluginIdForPreferredVersion)};`);
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processPluginOptions = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const path_1 = tslib_1.__importDefault(require("path"));
6
- function processPluginOptions(options, siteDir) {
6
+ function processPluginOptions(options, { siteDir, siteConfig: { themeConfig }, }) {
7
+ var _a, _b;
7
8
  const config = Object.assign({}, options);
8
9
  ensureArray(config, "docsRouteBasePath");
9
10
  ensureArray(config, "blogRouteBasePath");
@@ -15,6 +16,11 @@ function processPluginOptions(options, siteDir) {
15
16
  config.blogRouteBasePath = config.blogRouteBasePath.map((basePath) => basePath.replace(/^\//, ""));
16
17
  config.docsDir = config.docsDir.map((dir) => path_1.default.resolve(siteDir, dir));
17
18
  config.blogDir = config.blogDir.map((dir) => path_1.default.resolve(siteDir, dir));
19
+ if (config.searchBarPosition === "auto") {
20
+ 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");
21
+ config.searchBarPosition =
22
+ search && search.position === "left" ? "left" : "right";
23
+ }
18
24
  return config;
19
25
  }
20
26
  exports.processPluginOptions = processPluginOptions;
@@ -1,12 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tokenizer = void 0;
3
+ exports.tokenizer = exports.loadUserDict = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const fs_1 = tslib_1.__importDefault(require("fs"));
5
6
  const lunr_1 = tslib_1.__importDefault(require("lunr"));
6
7
  const jieba_1 = tslib_1.__importDefault(require("@node-rs/jieba"));
7
8
  const cutWordByUnderscore_1 = require("./cutWordByUnderscore");
8
9
  // https://zhuanlan.zhihu.com/p/33335629
9
10
  const RegExpConsecutiveWord = /\w+|\p{Unified_Ideograph}+/u;
11
+ let userDictLoaded = false;
12
+ function loadUserDict(zhUserDict, zhUserDictPath) {
13
+ if (userDictLoaded) {
14
+ return;
15
+ }
16
+ if (zhUserDict) {
17
+ jieba_1.default.loadDict(Buffer.from(zhUserDict));
18
+ }
19
+ else if (zhUserDictPath) {
20
+ jieba_1.default.loadDict(fs_1.default.readFileSync(zhUserDictPath));
21
+ }
22
+ userDictLoaded = true;
23
+ }
24
+ exports.loadUserDict = loadUserDict;
10
25
  function tokenizer(input, metadata) {
11
26
  if (input == null) {
12
27
  return [];
@@ -24,7 +24,10 @@ const schema = utils_validation_1.Joi.object({
24
24
  ignoreFiles: isArrayOfStringsOrRegExpsOrStringOrRegExp.default([]),
25
25
  searchBarShortcut: utils_validation_1.Joi.boolean().default(true),
26
26
  searchBarShortcutHint: utils_validation_1.Joi.boolean().default(true),
27
+ searchBarPosition: utils_validation_1.Joi.string().default("auto"),
27
28
  docsPluginIdForPreferredVersion: utils_validation_1.Joi.string(),
29
+ zhUserDict: utils_validation_1.Joi.string(),
30
+ zhUserDictPath: utils_validation_1.Joi.string(),
28
31
  });
29
32
  function validateOptions({ options, validate, }) {
30
33
  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.30.1",
3
+ "version": "0.32.0",
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",