@easyops-cn/docusaurus-search-local 0.27.0 → 0.28.0-alpha.1

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.
@@ -4,7 +4,8 @@ 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 { useActiveVersion } from '@docusaurus/plugin-content-docs/client';
7
+ import { useDocsPreferredVersion } from '@docusaurus/theme-common';
8
+ import { useActivePlugin } from '@docusaurus/plugin-content-docs/client';
8
9
  import { fetchIndexes } from "./fetchIndexes";
9
10
  import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
10
11
  import { SuggestionTemplate } from "./SuggestionTemplate";
@@ -28,9 +29,16 @@ async function fetchAutoCompleteJS() {
28
29
  const SEARCH_PARAM_HIGHLIGHT = "_highlight";
29
30
  export default function SearchBar({ handleSearchBarToggle, }) {
30
31
  let { siteConfig: { baseUrl }, } = useDocusaurusContext();
31
- const activeVersion = useActiveVersion();
32
- if (activeVersion && !activeVersion.isLast) {
33
- baseUrl = activeVersion.path + "/";
32
+ // It returns undefined for non-docs pages
33
+ const activePlugin = useActivePlugin();
34
+ let versionUrl = baseUrl;
35
+ // For non-docs pages while using plugin-content-docs with custom ids,
36
+ // this will throw an error of:
37
+ // > Docusaurus plugin global data not found for "docusaurus-plugin-content-docs" plugin with id "default".
38
+ // It seems that we can not get the correct id for non-docs pages.
39
+ const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId);
40
+ if (preferredVersion && !preferredVersion.isLast) {
41
+ versionUrl = preferredVersion.path + "/";
34
42
  }
35
43
  const history = useHistory();
36
44
  const location = useLocation();
@@ -50,7 +58,7 @@ export default function SearchBar({ handleSearchBarToggle, }) {
50
58
  indexState.current = "loading";
51
59
  setLoading(true);
52
60
  const [{ wrappedIndexes, zhDictionary }, autoComplete] = await Promise.all([
53
- fetchIndexes(baseUrl),
61
+ fetchIndexes(versionUrl),
54
62
  fetchAutoCompleteJS(),
55
63
  ]);
56
64
  search.current = autoComplete(searchBarRef.current, {
@@ -128,7 +136,7 @@ export default function SearchBar({ handleSearchBarToggle, }) {
128
136
  }
129
137
  input.focus();
130
138
  }
131
- }, [baseUrl, history]);
139
+ }, [baseUrl, versionUrl, history]);
132
140
  useEffect(() => {
133
141
  if (!Mark) {
134
142
  return;
@@ -201,7 +209,7 @@ export default function SearchBar({ handleSearchBarToggle, }) {
201
209
  // We always clear these here because in case no match was selected the above history push wont happen
202
210
  setInputValue("");
203
211
  search.current?.autocomplete.setVal("");
204
- }, [location.pathname, location.search, location.hash]);
212
+ }, [location.pathname, location.search, location.hash, history]);
205
213
  return (<div className={clsx("navbar__search", styles.searchBarContainer, {
206
214
  [styles.searchIndexLoading]: loading && inputChanged,
207
215
  })}>
@@ -4,7 +4,8 @@ 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 } from "@docusaurus/theme-common";
7
+ import { usePluralForm, useDocsPreferredVersion } from "@docusaurus/theme-common";
8
+ import { useActivePlugin } from '@docusaurus/plugin-content-docs/client';
8
9
  import useSearchQuery from "../hooks/useSearchQuery";
9
10
  import { fetchIndexes } from "../SearchBar/fetchIndexes";
10
11
  import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
@@ -15,7 +16,20 @@ import LoadingRing from "../LoadingRing/LoadingRing";
15
16
  import styles from "./SearchPage.module.css";
16
17
  import { concatDocumentPath } from "../../utils/concatDocumentPath";
17
18
  export default function SearchPage() {
18
- const { siteConfig: { baseUrl }, } = useDocusaurusContext();
19
+ return (<Layout>
20
+ <SearchPageContent />
21
+ </Layout>);
22
+ }
23
+ function SearchPageContent() {
24
+ let { siteConfig: { baseUrl }, } = useDocusaurusContext();
25
+ // It returns undefined for non-docs pages.
26
+ const activePlugin = useActivePlugin();
27
+ let versionUrl = baseUrl;
28
+ // There is an issue, see `SearchBar.tsx`.
29
+ const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId);
30
+ if (preferredVersion && !preferredVersion.isLast) {
31
+ versionUrl = preferredVersion.path + "/";
32
+ }
19
33
  const { selectMessage } = usePluralForm();
20
34
  const { searchValue, updateSearchPath } = useSearchQuery();
21
35
  const [searchQuery, setSearchQuery] = useState(searchValue);
@@ -59,12 +73,12 @@ export default function SearchPage() {
59
73
  }, [searchValue]);
60
74
  useEffect(() => {
61
75
  async function doFetchIndexes() {
62
- const { wrappedIndexes, zhDictionary } = await fetchIndexes(baseUrl);
76
+ const { wrappedIndexes, zhDictionary } = await fetchIndexes(versionUrl);
63
77
  setSearchSource(() => SearchSourceFactory(wrappedIndexes, zhDictionary, 100));
64
78
  }
65
79
  doFetchIndexes();
66
- }, [baseUrl]);
67
- return (<Layout>
80
+ }, [versionUrl]);
81
+ return (<React.Fragment>
68
82
  <Head>
69
83
  {/*
70
84
  We should not index search pages
@@ -106,7 +120,7 @@ export default function SearchPage() {
106
120
  searchResults.map((item) => (<SearchResultItem key={item.document.i} searchResult={item}/>))}
107
121
  </section>
108
122
  </div>
109
- </Layout>);
123
+ </React.Fragment>);
110
124
  }
111
125
  function SearchResultItem({ searchResult: { document, type, page, tokens, metadata }, }) {
112
126
  const isTitle = type === 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyops-cn/docusaurus-search-local",
3
- "version": "0.27.0",
3
+ "version": "0.28.0-alpha.1",
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",
@@ -24,7 +24,6 @@
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
26
  "@docusaurus/plugin-content-docs": "^2.0.0-beta.20",
27
- "@docusaurus/theme-common": "^2.0.0-beta.20",
28
27
  "@docusaurus/theme-translations": "^2.0.0-beta.20",
29
28
  "@docusaurus/utils": "^2.0.0-beta.20",
30
29
  "@docusaurus/utils-common": "^2.0.0-beta.20",
@@ -43,6 +42,7 @@
43
42
  },
44
43
  "devDependencies": {
45
44
  "@docusaurus/module-type-aliases": "^2.0.0-beta.20",
45
+ "@docusaurus/theme-common": "^2.0.0-beta.20",
46
46
  "@docusaurus/types": "^2.0.0-beta.20",
47
47
  "@tsconfig/docusaurus": "^1.0.2",
48
48
  "@types/cheerio": "^0.22.31",
@@ -61,6 +61,8 @@
61
61
  "typescript": "^4.6.4"
62
62
  },
63
63
  "peerDependencies": {
64
- "react": "^16.14.0 || 17 || 18"
64
+ "@docusaurus/theme-common": "^2.0.0-beta.20",
65
+ "react": "^16.14.0 || ^17.0.0 || ^18.0.0",
66
+ "react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0"
65
67
  }
66
68
  }