@@ -6,12 +6,10 @@ import { useHistory, useLocation } from '@docusaurus/router';
6
6
import { translate } from '@docusaurus/Translate' ;
7
7
import { ReactContextError , useDocsPreferredVersion } from '@docusaurus/theme-common' ;
8
8
import { useActivePlugin } from '@docusaurus/plugin-content-docs/client' ;
9
- import { fetchIndexes } from './fetchIndexes' ;
10
- import { SearchSourceFactory } from '../../utils/SearchSourceFactory' ;
9
+ import { fetchIndexesByWorker , searchByWorker } from './searchByWorker' ;
11
10
import { SuggestionTemplate } from './SuggestionTemplate' ;
12
11
import { EmptyTemplate } from './EmptyTemplate' ;
13
12
import {
14
- searchResultLimits ,
15
13
Mark ,
16
14
searchBarShortcut ,
17
15
searchBarShortcutHint ,
@@ -26,6 +24,7 @@ import LoadingRing from '../LoadingRing/LoadingRing';
26
24
import { VERSIONS , DEFAULT_VERSION } from '@site/src/constant/common' ;
27
25
import styles from './SearchBar.module.css' ;
28
26
import { normalizeContextByPath } from '../../utils/normalizeContextByPath' ;
27
+ import useIsDocPage from '@site/src/hooks/use-is-doc' ;
29
28
async function fetchAutoCompleteJS ( ) {
30
29
const autoCompleteModule = await import ( '@easyops-cn/autocomplete.js' ) ;
31
30
const autoComplete = autoCompleteModule . default ;
@@ -42,37 +41,54 @@ const SEARCH_PARAM_HIGHLIGHT = '_highlight';
42
41
export default function SearchBar ( { handleSearchBarToggle } ) {
43
42
const isBrowser = useIsBrowser ( ) ;
44
43
const [ curVersion , setCurVersion ] = useState ( DEFAULT_VERSION ) ;
44
+ const location = useLocation ( ) ;
45
45
const {
46
46
siteConfig : { baseUrl } ,
47
47
i18n : { currentLocale } ,
48
48
} = useDocusaurusContext ( ) ;
49
49
// It returns undefined for non-docs pages
50
50
const activePlugin = useActivePlugin ( ) ;
51
+ const [ isDocsPage ] = useIsDocPage ( false ) ;
51
52
let versionUrl = baseUrl ;
53
+ if ( location ?. pathname && location . pathname . includes ( 'zh-CN' ) && ! versionUrl . includes ( 'zh-CN' ) ) {
54
+ versionUrl = baseUrl + 'zh-CN/' ;
55
+ }
56
+ if ( location ?. pathname ) {
57
+ VERSIONS . forEach ( version => {
58
+ if ( location . pathname . includes ( version ) ) {
59
+ versionUrl += `docs/${ version } /` ;
60
+ }
61
+ } ) ;
62
+ }
63
+
52
64
// For non-docs pages while using plugin-content-docs with custom ids,
53
65
// this will throw an error of:
54
66
// > Docusaurus plugin global data not found for "docusaurus-plugin-content-docs" plugin with id "default".
55
67
// It seems that we can not get the correct id for non-docs pages.
56
- try {
57
- // The try-catch is a hack because useDocsPreferredVersion just throws an
58
- // exception when versions are not used.
59
- // The same hack is used in SearchPage.tsx
60
- // eslint-disable-next-line react-hooks/rules-of-hooks
61
- const { preferredVersion } = useDocsPreferredVersion ( activePlugin ?. pluginId ?? docsPluginIdForPreferredVersion ) ;
62
- if ( preferredVersion && ! preferredVersion . isLast ) {
63
- versionUrl = preferredVersion . path + '/' ;
64
- }
65
- } catch ( e ) {
66
- if ( indexDocs ) {
67
- if ( e instanceof ReactContextError ) {
68
- /* ignore, happens when website doesn't use versions */
69
- } else {
70
- throw e ;
71
- }
72
- }
73
- }
68
+ // try {
69
+ // // The try-catch is a hack because useDocsPreferredVersion just throws an
70
+ // // exception when versions are not used.
71
+ // // The same hack is used in SearchPage.tsx
72
+ // // eslint-disable-next-line react-hooks/rules-of-hooks
73
+ // const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId ?? docsPluginIdForPreferredVersion);
74
+ // console.log('preferredVersion',preferredVersion);
75
+
76
+ // if (preferredVersion && !preferredVersion.isLast) {
77
+ // versionUrl = preferredVersion.path + "/";
78
+ // }
79
+ // }
80
+ // catch (e) {
81
+ // if (indexDocs) {
82
+ // if (e instanceof ReactContextError) {
83
+ // /* ignore, happens when website doesn't use versions */
84
+ // }
85
+ // else {
86
+ // throw e;
87
+ // }
88
+ // }
89
+ // }
90
+
74
91
const history = useHistory ( ) ;
75
- const location = useLocation ( ) ;
76
92
const searchBarRef = useRef ( null ) ;
77
93
const indexStateMap = useRef ( new Map ( ) ) ;
78
94
// Should the input be focused after the index is loaded?
@@ -111,16 +127,16 @@ export default function SearchBar({ handleSearchBarToggle }) {
111
127
} , [ location . pathname , versionUrl ] ) ;
112
128
const hidden = ! ! hideSearchBarWithNoSearchContext && Array . isArray ( searchContextByPaths ) && searchContext === '' ;
113
129
const loadIndex = useCallback ( async ( forceLoad = false ) => {
114
- if ( ( hidden || indexStateMap . current . get ( searchContext ) ) && ! forceLoad ) {
130
+ if ( hidden || indexStateMap . current . get ( searchContext ) && ! forceLoad ) {
115
131
// Do not load the index (again) if its already loaded or in the process of being loaded.
116
132
return ;
117
133
}
118
134
indexStateMap . current . set ( searchContext , 'loading' ) ;
119
135
search . current ?. autocomplete . destroy ( ) ;
120
136
setLoading ( true ) ;
121
- const [ { wrappedIndexes, zhDictionary } , autoComplete ] = await Promise . all ( [
122
- fetchIndexes ( versionUrl , searchContext ) ,
137
+ const [ autoComplete ] = await Promise . all ( [
123
138
fetchAutoCompleteJS ( ) ,
139
+ fetchIndexesByWorker ( versionUrl , searchContext ) ,
124
140
] ) ;
125
141
const searchFooterLinkElement = ( { query, isEmpty } ) => {
126
142
const a = document . createElement ( 'a' ) ;
@@ -210,7 +226,10 @@ export default function SearchBar({ handleSearchBarToggle }) {
210
226
} ,
211
227
[
212
228
{
213
- source : SearchSourceFactory ( wrappedIndexes , zhDictionary , searchResultLimits ) ,
229
+ source : async ( input , callback ) => {
230
+ const result = await searchByWorker ( versionUrl , searchContext , input ) ;
231
+ callback ( result ) ;
232
+ } ,
214
233
templates : {
215
234
suggestion : SuggestionTemplate ,
216
235
empty : EmptyTemplate ,
@@ -335,6 +354,7 @@ export default function SearchBar({ handleSearchBarToggle }) {
335
354
document . removeEventListener ( 'keydown' , handleShortcut ) ;
336
355
} ;
337
356
} , [ isMac , onInputFocus ] ) ;
357
+
338
358
const onClearSearch = useCallback ( ( ) => {
339
359
const params = new URLSearchParams ( location . search ) ;
340
360
params . delete ( SEARCH_PARAM_HIGHLIGHT ) ;
@@ -347,6 +367,7 @@ export default function SearchBar({ handleSearchBarToggle }) {
347
367
setInputValue ( '' ) ;
348
368
search . current ?. autocomplete . setVal ( '' ) ;
349
369
} , [ location . pathname , location . search , location . hash , history ] ) ;
370
+
350
371
return (
351
372
< div
352
373
className = { clsx ( 'navbar__search' , styles . searchBarContainer , {
@@ -364,7 +385,7 @@ export default function SearchBar({ handleSearchBarToggle }) {
364
385
description : 'The ARIA label and placeholder for search button' ,
365
386
} ) }
366
387
aria-label = "Search"
367
- className = " navbar__search-input"
388
+ className = { clsx ( ' navbar__search-input' , styles . navbarSearchInput ) }
368
389
onMouseEnter = { onInputMouseEnter }
369
390
onFocus = { onInputFocus }
370
391
onBlur = { onInputBlur }
0 commit comments