@easyops-cn/docusaurus-search-local 0.33.1 → 0.33.3

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,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.33.3](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.33.2...v0.33.3) (2022-10-18)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * pass version instead of detect version in search page ([8ca32f0](https://github.com/easyops-cn/docusaurus-search-local/commit/8ca32f07ca4d36e061dd67f72a0fc1b477d2c9f6)), closes [#285](https://github.com/easyops-cn/docusaurus-search-local/issues/285)
11
+
12
+ ## [0.33.2](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.33.1...v0.33.2) (2022-10-17)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * fix search terms highlighting ([aacc610](https://github.com/easyops-cn/docusaurus-search-local/commit/aacc610db70f60585d883f977ae2da699dde6f29)), closes [#266](https://github.com/easyops-cn/docusaurus-search-local/issues/266)
18
+
5
19
  ## [0.33.1](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.33.0...v0.33.1) (2022-10-17)
6
20
 
7
21
 
@@ -131,9 +131,18 @@ export default function SearchBar({ handleSearchBarToggle, }) {
131
131
  return;
132
132
  }
133
133
  const a = document.createElement("a");
134
- const url = `${baseUrl}search?q=${encodeURIComponent(query)}${Array.isArray(searchContextByPaths)
135
- ? `&ctx=${encodeURIComponent(searchContext)}`
136
- : ""}`;
134
+ const params = new URLSearchParams();
135
+ params.set("q", encodeURIComponent(query));
136
+ if (Array.isArray(searchContextByPaths)) {
137
+ params.set("ctx", searchContext);
138
+ }
139
+ if (versionUrl !== baseUrl) {
140
+ if (!versionUrl.startsWith(baseUrl)) {
141
+ throw new Error(`Version url '${versionUrl}' does not start with base url '${baseUrl}', this is a bug of \`@easyops-cn/docusaurus-search-local\`, please report it.`);
142
+ }
143
+ params.set("version", versionUrl.substring(baseUrl.length));
144
+ }
145
+ const url = `${baseUrl}search?${params.toString()}`;
137
146
  a.href = url;
138
147
  a.textContent = translate({
139
148
  id: "theme.SearchBar.seeAll",
@@ -4,8 +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, ReactContextError, } from "@docusaurus/theme-common";
8
- import { useActivePlugin } from "@docusaurus/plugin-content-docs/client";
7
+ import { usePluralForm } from "@docusaurus/theme-common";
9
8
  import useSearchQuery from "../hooks/useSearchQuery";
10
9
  import { fetchIndexes } from "../SearchBar/fetchIndexes";
11
10
  import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
@@ -14,7 +13,7 @@ import { highlightStemmed } from "../../utils/highlightStemmed";
14
13
  import { getStemmedPositions } from "../../utils/getStemmedPositions";
15
14
  import LoadingRing from "../LoadingRing/LoadingRing";
16
15
  import { concatDocumentPath } from "../../utils/concatDocumentPath";
17
- import { docsPluginIdForPreferredVersion, indexDocs, } from "../../utils/proxiedGenerated";
16
+ import { Mark } from "../../utils/proxiedGenerated";
18
17
  import styles from "./SearchPage.module.css";
19
18
  export default function SearchPage() {
20
19
  return (<Layout>
@@ -23,35 +22,12 @@ export default function SearchPage() {
23
22
  }
24
23
  function SearchPageContent() {
25
24
  const { siteConfig: { baseUrl }, } = useDocusaurusContext();
26
- // It returns undefined for non-docs pages.
27
- const activePlugin = useActivePlugin();
28
- let versionUrl = baseUrl;
29
- // There is an issue, see `SearchBar.tsx`.
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 (indexDocs) {
42
- if (e instanceof ReactContextError) {
43
- /* ignore, happens when website doesn't use versions */
44
- }
45
- else {
46
- throw e;
47
- }
48
- }
49
- }
50
25
  const { selectMessage } = usePluralForm();
51
- const { searchValue, searchContext, updateSearchPath } = useSearchQuery();
26
+ const { searchValue, searchContext, searchVersion, updateSearchPath } = useSearchQuery();
52
27
  const [searchQuery, setSearchQuery] = useState(searchValue);
53
28
  const [searchSource, setSearchSource] = useState();
54
29
  const [searchResults, setSearchResults] = useState();
30
+ const versionUrl = `${baseUrl}${searchVersion}`;
55
31
  const pageTitle = useMemo(() => searchQuery
56
32
  ? translate({
57
33
  id: "theme.SearchPage.existingResultsTitle",
@@ -147,9 +123,17 @@ function SearchResultItem({ searchResult: { document, type, page, tokens, metada
147
123
  if (!isTitle) {
148
124
  pathItems.push(page.t);
149
125
  }
126
+ let search = "";
127
+ if (Mark && tokens.length > 0) {
128
+ const params = new URLSearchParams();
129
+ for (const token of tokens) {
130
+ params.append("_highlight", token);
131
+ }
132
+ search = `?${params.toString()}`;
133
+ }
150
134
  return (<article className={styles.searchResultItem}>
151
135
  <h2>
152
- <Link to={document.u + (document.h || "")} dangerouslySetInnerHTML={{
136
+ <Link to={document.u + search + (document.h || "")} dangerouslySetInnerHTML={{
153
137
  __html: isContent
154
138
  ? highlight(articleTitle, tokens)
155
139
  : highlightStemmed(articleTitle, getStemmedPositions(metadata, "t"), tokens, 100),
@@ -7,9 +7,9 @@
7
7
  import { useHistory, useLocation } from "@docusaurus/router";
8
8
  import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
9
9
  import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
10
- import { searchContextByPaths, } from "../../utils/proxiedGenerated";
11
10
  const SEARCH_PARAM_QUERY = "q";
12
11
  const SEARCH_PARAM_CONTEXT = "ctx";
12
+ const SEARCH_PARAM_VERSION = "version";
13
13
  function useSearchQuery() {
14
14
  const history = useHistory();
15
15
  const location = useLocation();
@@ -17,26 +17,31 @@ function useSearchQuery() {
17
17
  const params = ExecutionEnvironment.canUseDOM ? new URLSearchParams(location.search) : null;
18
18
  const searchValue = params?.get(SEARCH_PARAM_QUERY) || "";
19
19
  const searchContext = params?.get(SEARCH_PARAM_CONTEXT) || "";
20
+ const searchVersion = params?.get(SEARCH_PARAM_VERSION) || "";
21
+ const getSearchParams = (searchValue) => {
22
+ const searchParams = new URLSearchParams(location.search);
23
+ if (searchValue) {
24
+ searchParams.set(SEARCH_PARAM_QUERY, searchValue);
25
+ }
26
+ else {
27
+ searchParams.delete(SEARCH_PARAM_QUERY);
28
+ }
29
+ return searchParams;
30
+ };
20
31
  return {
21
32
  searchValue,
22
33
  searchContext,
34
+ searchVersion,
23
35
  updateSearchPath: (searchValue) => {
24
- const searchParams = new URLSearchParams(location.search);
25
- if (searchValue) {
26
- searchParams.set(SEARCH_PARAM_QUERY, searchValue);
27
- }
28
- else {
29
- searchParams.delete(SEARCH_PARAM_QUERY);
30
- }
36
+ const searchParams = getSearchParams(searchValue);
31
37
  history.replace({
32
38
  search: searchParams.toString(),
33
39
  });
34
40
  },
35
41
  generateSearchPageLink: (searchValue) => {
36
- const searchParams = new URLSearchParams(location.search);
37
- const searchContext = searchParams.get(SEARCH_PARAM_CONTEXT) || "";
42
+ const searchParams = getSearchParams(searchValue);
38
43
  // Refer to https://github.com/facebook/docusaurus/pull/2838
39
- return `${baseUrl}search?q=${encodeURIComponent(searchValue)}${Array.isArray(searchContextByPaths) ? `&ctx=${encodeURIComponent(searchContext)}` : ""}`;
44
+ return `${baseUrl}search?${searchParams.toString()}`;
40
45
  },
41
46
  };
42
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyops-cn/docusaurus-search-local",
3
- "version": "0.33.1",
3
+ "version": "0.33.3",
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",