@rspress/core 2.0.20 → 2.0.22

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.
Files changed (179) hide show
  1. package/dist/cli/index.js +12 -13
  2. package/dist/config/loadConfigFile.d.ts +3 -4
  3. package/dist/config/loadConfigFile.js +13 -17
  4. package/dist/{7748~0.js → d~0.js} +0 -2
  5. package/dist/eject-theme/components/Banner/index.tsx +50 -9
  6. package/dist/eject-theme/components/Callout/index.scss +6 -1
  7. package/dist/eject-theme/components/CodeBlock/index.scss +2 -0
  8. package/dist/eject-theme/components/DocContent/doc.scss +2 -2
  9. package/dist/eject-theme/components/Link/getHref.test.ts +33 -0
  10. package/dist/eject-theme/components/Link/getHref.ts +60 -0
  11. package/dist/eject-theme/components/Link/index.tsx +7 -6
  12. package/dist/eject-theme/components/Link/useLinkNavigate.ts +6 -51
  13. package/dist/eject-theme/components/Llms/LlmsHint.test.tsx +8 -0
  14. package/dist/eject-theme/components/Llms/LlmsHint.tsx +11 -3
  15. package/dist/eject-theme/components/Nav/NavMenu.scss +19 -3
  16. package/dist/eject-theme/components/Nav/NavMenu.tsx +20 -13
  17. package/dist/eject-theme/components/Nav/index.scss +3 -1
  18. package/dist/eject-theme/components/Nav/index.tsx +30 -4
  19. package/dist/eject-theme/components/NavHamburger/index.tsx +11 -3
  20. package/dist/eject-theme/components/NavList/context.ts +3 -0
  21. package/dist/eject-theme/components/NavList/index.tsx +24 -0
  22. package/dist/eject-theme/components/NavScreen/NavScreenMenu.test.tsx +59 -0
  23. package/dist/eject-theme/components/NavScreen/NavScreenMenu.tsx +43 -2
  24. package/dist/eject-theme/components/NavScreen/index.scss +0 -6
  25. package/dist/eject-theme/components/NavScreen/index.tsx +20 -3
  26. package/dist/eject-theme/components/Prompt/index.scss +2 -1
  27. package/dist/eject-theme/components/ReadPercent/useReadPercent.ts +1 -1
  28. package/dist/eject-theme/components/Search/SearchButton.scss +1 -0
  29. package/dist/eject-theme/components/Search/SearchPanel.scss +51 -12
  30. package/dist/eject-theme/components/Search/SearchPanel.tsx +54 -212
  31. package/dist/eject-theme/components/Search/logic/providers/LocalProvider.ts +38 -16
  32. package/dist/eject-theme/components/Search/useSearchPanel.ts +216 -0
  33. package/dist/eject-theme/components/Steps/index.scss +1 -1
  34. package/dist/eject-theme/components/SwitchAppearance/getClipPath.test.ts +15 -0
  35. package/dist/eject-theme/components/SwitchAppearance/getClipPath.ts +26 -0
  36. package/dist/eject-theme/components/SwitchAppearance/global.scss +18 -0
  37. package/dist/eject-theme/components/SwitchAppearance/index.tsx +26 -35
  38. package/dist/eject-theme/components/Tabs/index.tsx +14 -14
  39. package/dist/index.js +1 -1
  40. package/dist/node/PluginDriver.d.ts +1 -1
  41. package/dist/node/PluginDriver.js +5 -9
  42. package/dist/node/auto-nav-sidebar/index.js +1 -1
  43. package/dist/node/auto-nav-sidebar/locales.js +1 -1
  44. package/dist/node/auto-nav-sidebar/normalize.js +6 -6
  45. package/dist/node/auto-nav-sidebar/utils.js +1 -1
  46. package/dist/node/auto-nav-sidebar/walk.js +1 -1
  47. package/dist/node/build.d.ts +3 -2
  48. package/dist/node/build.js +9 -3
  49. package/dist/node/constants.js +1 -1
  50. package/dist/node/dev.d.ts +4 -4
  51. package/dist/node/dev.js +8 -6
  52. package/dist/node/eject.js +5 -5
  53. package/dist/node/initRsbuild.d.ts +2 -3
  54. package/dist/node/initRsbuild.js +9 -7
  55. package/dist/node/last-updated/index.js +2 -2
  56. package/dist/node/logger/hint.js +4 -4
  57. package/dist/node/mdx/options.js +1 -1
  58. package/dist/node/mdx/processor.js +3 -3
  59. package/dist/node/mdx/rehypePlugins/codeMeta.js +1 -1
  60. package/dist/node/mdx/rehypePlugins/headerAnchor.js +1 -1
  61. package/dist/node/mdx/rehypePlugins/shiki.js +1 -1
  62. package/dist/node/mdx/remarkPlugins/builtin.js +1 -1
  63. package/dist/node/mdx/remarkPlugins/containerSyntax.js +6 -5
  64. package/dist/node/mdx/remarkPlugins/fileCodeBlock.js +5 -5
  65. package/dist/node/mdx/remarkPlugins/image.js +5 -5
  66. package/dist/node/mdx/remarkPlugins/link.js +5 -5
  67. package/dist/node/mdx/remarkPlugins/toc.js +1 -1
  68. package/dist/node/medium-zoom/index.js +1 -1
  69. package/dist/node/route/RoutePage.js +6 -4
  70. package/dist/node/route/RouteService.js +3 -3
  71. package/dist/node/route/extractPageData.js +20 -7
  72. package/dist/node/route/normalizeRoutePath.js +1 -1
  73. package/dist/node/runtimeModule/i18n.js +5 -5
  74. package/dist/node/runtimeModule/pageData/createPageData.js +1 -1
  75. package/dist/node/runtimeModule/pageData/rsbuildPlugin.js +1 -1
  76. package/dist/node/runtimeModule/searchHooks.js +1 -1
  77. package/dist/node/runtimeModule/siteData/createSiteData.js +1 -1
  78. package/dist/node/runtimeModule/siteData/normalizeThemeConfig.js +1 -1
  79. package/dist/node/runtimeModule/siteData/rsbuildPlugin.js +1 -1
  80. package/dist/node/serve.d.ts +3 -3
  81. package/dist/node/serve.js +10 -4
  82. package/dist/node/ssg/alternateLinks.js +1 -1
  83. package/dist/node/ssg/htmlFile.js +1 -1
  84. package/dist/node/ssg/renderHtmlTemplate.js +1 -1
  85. package/dist/node/ssg/renderPage.js +5 -5
  86. package/dist/node/ssg/renderPageWorker.js +2 -2
  87. package/dist/node/ssg/renderPages.js +6 -6
  88. package/dist/node/ssg/rsbuildPluginSSG.js +1 -1
  89. package/dist/node/ssg/ssgEnv.js +1 -1
  90. package/dist/node/ssg-md/llms/emitLlmsTxt.js +1 -1
  91. package/dist/node/ssg-md/llms/llmsTxt.js +1 -1
  92. package/dist/node/ssg-md/remarkSplitMdx.js +1 -1
  93. package/dist/node/ssg-md/renderPage.js +5 -5
  94. package/dist/node/ssg-md/renderPages.js +6 -6
  95. package/dist/node/ssg-md/rsbuildPluginSSGMD.js +1 -1
  96. package/dist/node/utils/checkLanguageParity.js +5 -7
  97. package/dist/node/utils/createHash.js +1 -1
  98. package/dist/node/utils/error.js +4 -4
  99. package/dist/node/utils/flattenMdxContent.js +2 -2
  100. package/dist/node/utils/fs.js +1 -1
  101. package/dist/node/utils/getPageKey.js +1 -1
  102. package/dist/node/utils/index.d.ts +1 -1
  103. package/dist/node/utils/reactAlias.js +5 -5
  104. package/dist/node/utils/slash.d.ts +1 -0
  105. package/dist/node/utils/slash.js +4 -0
  106. package/dist/{864~0.js → n~0.js} +41 -41
  107. package/dist/{2711~0.js → o~0.js} +90 -56
  108. package/dist/rslib-runtime~0.js +27 -35
  109. package/dist/theme/components/Banner/index.js +53 -9
  110. package/dist/theme/components/Callout/index.css +6 -1
  111. package/dist/theme/components/CodeBlock/index.css +2 -0
  112. package/dist/theme/components/DocContent/doc.css +2 -2
  113. package/dist/theme/components/Link/getHref.d.ts +8 -0
  114. package/dist/theme/components/Link/getHref.js +33 -0
  115. package/dist/theme/components/Link/index.js +7 -6
  116. package/dist/theme/components/Link/useLinkNavigate.d.ts +0 -7
  117. package/dist/theme/components/Link/useLinkNavigate.js +8 -32
  118. package/dist/theme/components/Llms/LlmsHint.js +23 -6
  119. package/dist/theme/components/Nav/NavMenu.css +18 -6
  120. package/dist/theme/components/Nav/NavMenu.d.ts +4 -2
  121. package/dist/theme/components/Nav/NavMenu.js +17 -9
  122. package/dist/theme/components/Nav/index.css +3 -1
  123. package/dist/theme/components/Nav/index.d.ts +7 -1
  124. package/dist/theme/components/Nav/index.js +13 -4
  125. package/dist/theme/components/NavHamburger/index.d.ts +2 -1
  126. package/dist/theme/components/NavHamburger/index.js +8 -2
  127. package/dist/theme/components/NavList/context.d.ts +1 -0
  128. package/dist/theme/components/NavList/context.js +3 -0
  129. package/dist/theme/components/NavList/index.d.ts +6 -0
  130. package/dist/theme/components/NavList/index.js +17 -0
  131. package/dist/theme/components/NavScreen/NavScreenMenu.d.ts +3 -2
  132. package/dist/theme/components/NavScreen/NavScreenMenu.js +42 -6
  133. package/dist/theme/components/NavScreen/index.css +0 -10
  134. package/dist/theme/components/NavScreen/index.d.ts +2 -1
  135. package/dist/theme/components/NavScreen/index.js +11 -3
  136. package/dist/theme/components/PackageManagerTabs/index.js +2 -2
  137. package/dist/theme/components/Prompt/index.css +2 -1
  138. package/dist/theme/components/ReadPercent/useReadPercent.js +1 -0
  139. package/dist/theme/components/Search/SearchButton.css +1 -0
  140. package/dist/theme/components/Search/SearchPanel.css +47 -9
  141. package/dist/theme/components/Search/SearchPanel.js +97 -205
  142. package/dist/theme/components/Search/logic/providers/LocalProvider.d.ts +1 -0
  143. package/dist/theme/components/Search/logic/providers/LocalProvider.js +23 -13
  144. package/dist/theme/components/Search/logic/search.js +1 -1
  145. package/dist/theme/components/Search/useSearchPanel.d.ts +23 -0
  146. package/dist/theme/components/Search/useSearchPanel.js +155 -0
  147. package/dist/theme/components/Steps/index.css +3 -3
  148. package/dist/theme/components/SwitchAppearance/getClipPath.d.ts +8 -0
  149. package/dist/theme/components/SwitchAppearance/getClipPath.js +13 -0
  150. package/dist/theme/components/SwitchAppearance/global.css +12 -0
  151. package/dist/theme/components/SwitchAppearance/index.js +11 -15
  152. package/dist/theme/components/Tabs/index.js +4 -4
  153. package/dist/theme/components/Tag/index.js +2 -2
  154. package/dist/theme/index.d.ts +1 -0
  155. package/dist/theme/index.js +1 -0
  156. package/dist/theme/layout/DocLayout/index.js +2 -2
  157. package/dist/theme/layout/Layout/index.js +12 -4
  158. package/dist/theme/logic/applyThemeToDOM.d.ts +2 -0
  159. package/dist/theme/logic/applyThemeToDOM.js +17 -0
  160. package/dist/theme/logic/useFullTextSearch.js +6 -1
  161. package/dist/theme/logic/useThemeState.d.ts +1 -2
  162. package/dist/theme/logic/useThemeState.js +1 -7
  163. package/dist/theme/styles/vars/shiki-vars.css +1 -1
  164. package/dist/tinypool~0.js +5 -3
  165. package/dist/{4538~0.js → t~0.js} +2 -2
  166. package/dist/{8472~0.js → v~0.js} +1 -1
  167. package/package.json +11 -12
  168. package/dist/eject-theme/components/NavScreen/NavScreenMenuOthers.tsx +0 -29
  169. package/dist/node/utils/normalizePath.d.ts +0 -3
  170. package/dist/node/utils/normalizePath.js +0 -10
  171. package/dist/theme/components/NavScreen/NavScreenMenuOthers.d.ts +0 -2
  172. package/dist/theme/components/NavScreen/NavScreenMenuOthers.js +0 -30
  173. package/dist/theme/logic/useHandler.d.ts +0 -6
  174. package/dist/theme/logic/useHandler.js +0 -7
  175. /package/dist/{9857~0.js → 4~0.js} +0 -0
  176. /package/dist/{8016~0.js → i~0.js} +0 -0
  177. /package/dist/{5357~0.js → r~0.js} +0 -0
  178. /package/dist/{102~0.js → t2~0.js} +0 -0
  179. /package/dist/{9940~0.js → tx~0.js} +0 -0
@@ -1,11 +1,47 @@
1
- import { Fragment, jsx } from "react/jsx-runtime";
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { Fragment as external_react_Fragment } from "react";
2
3
  import { NavScreenMenuItem } from "./NavScreenMenuItem.js";
3
4
  import "./NavScreenMenu.css";
4
- function NavScreenMenu({ menuItems }) {
5
- return /*#__PURE__*/ jsx(Fragment, {
6
- children: menuItems.map((item, index)=>/*#__PURE__*/ jsx(NavScreenMenuItem, {
7
- menuItem: item
8
- }, index))
5
+ function NavScreenMenu({ menuItems, beforeLeftNavItems, afterLeftNavItems, beforeRightNavItems, afterRightNavItems }) {
6
+ let firstLeft = -1;
7
+ let lastLeft = -1;
8
+ let firstRight = -1;
9
+ let lastRight = -1;
10
+ menuItems.forEach((item, index)=>{
11
+ if ('left' === item.position) {
12
+ if (-1 === firstLeft) firstLeft = index;
13
+ lastLeft = index;
14
+ } else {
15
+ if (-1 === firstRight) firstRight = index;
16
+ lastRight = index;
17
+ }
18
+ });
19
+ return /*#__PURE__*/ jsxs(Fragment, {
20
+ children: [
21
+ -1 === firstLeft ? /*#__PURE__*/ jsxs(Fragment, {
22
+ children: [
23
+ beforeLeftNavItems,
24
+ afterLeftNavItems
25
+ ]
26
+ }) : null,
27
+ menuItems.map((item, index)=>/*#__PURE__*/ jsxs(external_react_Fragment, {
28
+ children: [
29
+ index === firstLeft ? beforeLeftNavItems : null,
30
+ index === firstRight ? beforeRightNavItems : null,
31
+ /*#__PURE__*/ jsx(NavScreenMenuItem, {
32
+ menuItem: item
33
+ }),
34
+ index === lastLeft ? afterLeftNavItems : null,
35
+ index === lastRight ? afterRightNavItems : null
36
+ ]
37
+ }, index)),
38
+ -1 === firstRight ? /*#__PURE__*/ jsxs(Fragment, {
39
+ children: [
40
+ beforeRightNavItems,
41
+ afterRightNavItems
42
+ ]
43
+ }) : null
44
+ ]
9
45
  });
10
46
  }
11
47
  export { NavScreenMenu };
@@ -23,16 +23,6 @@
23
23
  display: flex;
24
24
  }
25
25
 
26
- .rp-nav-screen__others {
27
- display: flex;
28
- }
29
-
30
- @media (width <= 1280px) {
31
- .rp-nav-screen__others {
32
- display: none;
33
- }
34
- }
35
-
36
26
  .rp-nav-screen-divider {
37
27
  background-color: var(--rp-c-divider-light);
38
28
  width: 100%;
@@ -1,5 +1,6 @@
1
1
  import './index.scss';
2
- export interface NavScreenProps {
2
+ import type { NavItemsSlots } from '../Nav/index.js';
3
+ export interface NavScreenProps extends NavItemsSlots {
3
4
  isScreenOpen: boolean;
4
5
  toggleScreen: () => void;
5
6
  }
@@ -5,6 +5,7 @@ import { clearAllBodyScrollLocks, disableBodyScroll } from "body-scroll-lock";
5
5
  import clsx from "clsx";
6
6
  import { useEffect, useRef } from "react";
7
7
  import "./index.css";
8
+ import { NavListContext } from "../NavList/context.js";
8
9
  import { NavScreenAppearance } from "./NavScreenAppearance.js";
9
10
  import { NavScreenLangs } from "./NavScreenLangs.js";
10
11
  import { NavScreenMenu } from "./NavScreenMenu.js";
@@ -15,7 +16,7 @@ function NavScreenDivider() {
15
16
  });
16
17
  }
17
18
  function NavScreen(props) {
18
- const { isScreenOpen, toggleScreen } = props;
19
+ const { isScreenOpen, toggleScreen, beforeLeftNavItems, afterLeftNavItems, beforeRightNavItems, afterRightNavItems } = props;
19
20
  const screen = useRef(null);
20
21
  const menuItems = useNav();
21
22
  useEffect(()=>{
@@ -47,8 +48,15 @@ function NavScreen(props) {
47
48
  className: "rp-nav-screen__container",
48
49
  onClick: (e)=>e.stopPropagation(),
49
50
  children: [
50
- /*#__PURE__*/ jsx(NavScreenMenu, {
51
- menuItems: menuItems
51
+ /*#__PURE__*/ jsx(NavListContext.Provider, {
52
+ value: "screen",
53
+ children: /*#__PURE__*/ jsx(NavScreenMenu, {
54
+ menuItems: menuItems,
55
+ beforeLeftNavItems: beforeLeftNavItems,
56
+ afterLeftNavItems: afterLeftNavItems,
57
+ beforeRightNavItems: beforeRightNavItems,
58
+ afterRightNavItems: afterRightNavItems
59
+ })
52
60
  }),
53
61
  /*#__PURE__*/ jsx(NavScreenDivider, {}),
54
62
  /*#__PURE__*/ jsx(NavScreenAppearance, {}),
@@ -80,8 +80,8 @@ function PackageManagerTabs({ command, exec, dlx, additionalTabs = [] }) {
80
80
  default:
81
81
  return packageManager;
82
82
  }
83
- if (!exec) return packageManager;
84
- switch(packageManager){
83
+ else if (!exec) return packageManager;
84
+ else switch(packageManager){
85
85
  case 'npm':
86
86
  return 'npx';
87
87
  case 'yarn':
@@ -302,7 +302,8 @@
302
302
 
303
303
  .rp-prompt__custom-content a {
304
304
  color: var(--rp-prompt-accent, var(--rp-c-brand-dark));
305
- border-bottom: 1px solid;
305
+ text-underline-offset: 4px;
306
+ text-decoration: underline;
306
307
  }
307
308
 
308
309
  .rp-prompt__custom-content a > code, .rp-prompt__custom-content a:hover > code {
@@ -25,6 +25,7 @@ const useReadPercent = ()=>{
25
25
  const deltaHeight = Math.min(scrollTop, height);
26
26
  return Math.floor(Math.min(Math.max(0, (scrollTop - 50 + deltaHeight) / h * 100), 100)) || 0;
27
27
  }, [
28
+ h,
28
29
  scrollTop,
29
30
  height
30
31
  ]);
@@ -54,6 +54,7 @@
54
54
  cursor: pointer;
55
55
  justify-content: center;
56
56
  align-items: center;
57
+ font-size: 20px;
57
58
  font-weight: 500;
58
59
  display: none;
59
60
  }
@@ -1,6 +1,6 @@
1
1
  .rp-search-panel__mask {
2
2
  backdrop-filter: blur(5px);
3
- z-index: 100;
3
+ z-index: 1000;
4
4
  background: #3c3c3c66;
5
5
  position: fixed;
6
6
  inset: 0;
@@ -13,9 +13,16 @@
13
13
  height: auto;
14
14
  margin: 80px auto auto;
15
15
  padding: 20px;
16
+ animation: .18s ease-out rp-search-panel-in;
16
17
  position: relative;
17
18
  }
18
19
 
20
+ @media (prefers-reduced-motion: reduce) {
21
+ .rp-search-panel__modal {
22
+ animation: none;
23
+ }
24
+ }
25
+
19
26
  .rp-search-panel__header {
20
27
  align-items: center;
21
28
  display: flex;
@@ -24,6 +31,7 @@
24
31
  .rp-search-panel__cancel {
25
32
  color: var(--rp-c-brand);
26
33
  cursor: pointer;
34
+ white-space: nowrap;
27
35
  margin-left: .5rem;
28
36
  }
29
37
 
@@ -65,18 +73,31 @@
65
73
  transition: color .3s;
66
74
  }
67
75
 
76
+ .rp-search-panel__search-icon--loading {
77
+ animation: 1s ease-in-out infinite rp-search-panel-icon-loading;
78
+ }
79
+
80
+ @media (prefers-reduced-motion: reduce) {
81
+ .rp-search-panel__search-icon--loading {
82
+ animation: none;
83
+ }
84
+ }
85
+
86
+ .rp-search-panel__search-icon {
87
+ font-size: 20px;
88
+ display: inline-block;
89
+ }
90
+
68
91
  .rp-search-panel__results {
69
92
  max-height: calc(100vh - 230px);
70
93
  padding-right: 2px;
71
94
  overflow: scroll;
72
95
  }
73
96
 
74
- .rp-search-panel__loading {
75
- opacity: .8;
76
- flex-direction: column;
77
- align-items: center;
78
- margin: 2rem 0;
79
- display: flex;
97
+ .rp-search-panel__error {
98
+ color: var(--rp-c-text-2);
99
+ text-align: center;
100
+ padding: 2rem 0;
80
101
  }
81
102
 
82
103
  .rp-search-panel__tabs {
@@ -97,14 +118,14 @@
97
118
 
98
119
  @media (width <= 960px) {
99
120
  .rp-search-panel__modal {
100
- margin-top: 0;
121
+ margin-top: calc(32px + env(safe-area-inset-top, 0px));
101
122
  }
102
123
 
103
124
  .rp-search-panel__input-form {
104
125
  height: 40px;
105
126
  }
106
127
 
107
- .rp-search-panel__input-form svg {
128
+ .rp-search-panel__search-icon, .rp-search-panel__close {
108
129
  width: 16px;
109
130
  height: 16px;
110
131
  }
@@ -114,3 +135,20 @@
114
135
  }
115
136
  }
116
137
 
138
+ @keyframes rp-search-panel-in {
139
+ from {
140
+ opacity: 0;
141
+ transform: translateY(-8px) scale(.98);
142
+ }
143
+ }
144
+
145
+ @keyframes rp-search-panel-icon-loading {
146
+ 0%, 100% {
147
+ opacity: 1;
148
+ }
149
+
150
+ 50% {
151
+ opacity: .35;
152
+ }
153
+ }
154
+
@@ -1,15 +1,14 @@
1
1
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
- import { useI18n, usePageData } from "@rspress/core/runtime";
3
- import { IconClose, IconLoading, IconSearch, SvgWrapper, Tab, Tabs, useLinkNavigate } from "@rspress/core/theme";
4
- import { debounce } from "@rspress/shared/lodash-es";
5
- import { useCallback, useEffect, useRef, useState } from "react";
2
+ import { Head, useI18n } from "@rspress/core/runtime";
3
+ import { IconClose, IconSearch, SvgWrapper, Tab, Tabs, useLinkNavigate } from "@rspress/core/theme";
4
+ import { useEffect, useRef, useState } from "react";
6
5
  import { createPortal } from "react-dom";
7
6
  import * as __rspack_external_virtual_search_hooks_705b6e40 from "virtual-search-hooks";
8
- import { PageSearcher } from "./logic/search.js";
9
7
  import { RenderType } from "./logic/types.js";
10
8
  import { NoSearchResult } from "./NoSearchResult.js";
11
9
  import "./SearchPanel.css";
12
10
  import { SuggestItem } from "./SuggestItem.js";
11
+ import { useSearchPanel } from "./useSearchPanel.js";
13
12
  const KEY_CODE = {
14
13
  ARROW_UP: 'ArrowUp',
15
14
  ARROW_DOWN: 'ArrowDown',
@@ -17,12 +16,6 @@ const KEY_CODE = {
17
16
  SEARCH: 'KeyK',
18
17
  ESC: 'Escape'
19
18
  };
20
- const useDebounce = (cb)=>{
21
- const cbRef = useRef(cb);
22
- cbRef.current = cb;
23
- const debounced = useCallback(debounce((...args)=>cbRef.current(...args), 150), []);
24
- return debounced;
25
- };
26
19
  const normalizeSuggestions = (suggestions)=>suggestions.reduce((groups, item)=>{
27
20
  const group = item.title;
28
21
  if (!groups.has(group)) groups.set(group, []);
@@ -30,15 +23,7 @@ const normalizeSuggestions = (suggestions)=>suggestions.reduce((groups, item)=>{
30
23
  return groups;
31
24
  }, new Map());
32
25
  function SearchPanel({ focused, setFocused }) {
33
- const [query, setQuery] = useState('');
34
- const [searchResult, setSearchResult] = useState([]);
35
26
  const searchInputRef = useRef(null);
36
- const [isSearching, setIsSearching] = useState(false);
37
- const [resultTabIndex, setResultTabIndex] = useState(0);
38
- const [currentSuggestionIndex, setCurrentSuggestionIndex] = useState(0);
39
- const pageSearcherRef = useRef(null);
40
- const pageSearcherConfigRef = useRef(null);
41
- const [initStatus, setInitStatus] = useState('initial');
42
27
  const searchResultRef = useRef(null);
43
28
  const searchResultTabRef = useRef(null);
44
29
  const mousePositionRef = useRef({
@@ -60,49 +45,13 @@ function SearchPanel({ focused, setFocused }) {
60
45
  });
61
46
  }
62
47
  };
63
- const { siteData, page: { lang, version } } = usePageData();
64
48
  const t = useI18n();
65
49
  const navigate = useLinkNavigate();
66
- const { search, title: siteTitle } = siteData;
67
- const versionedSearch = 'boolean' != typeof search && (search?.versioned ?? true);
68
- const DEFAULT_RESULT = [
69
- {
70
- group: siteTitle,
71
- result: [],
72
- renderType: RenderType.Default
73
- }
74
- ];
75
- const currentSuggestions = searchResult[resultTabIndex]?.result ?? [];
76
- const currentRenderType = searchResult[resultTabIndex]?.renderType ?? RenderType.Default;
77
- if (false === search) return null;
78
- const createSearcher = ()=>{
79
- if (pageSearcherRef.current) return pageSearcherRef.current;
80
- const pageSearcherConfig = {
81
- currentLang: lang,
82
- currentVersion: version
83
- };
84
- const pageSearcher = new PageSearcher({
85
- indexName: siteTitle,
86
- ...search,
87
- ...pageSearcherConfig
88
- });
89
- pageSearcherRef.current = pageSearcher;
90
- pageSearcherConfigRef.current = pageSearcherConfig;
91
- return pageSearcherRef.current;
92
- };
93
- async function initSearch() {
94
- if ('initial' !== initStatus) return;
95
- const searcher = createSearcher();
96
- setInitStatus('initing');
97
- await searcher.init();
98
- setInitStatus('inited');
99
- const query = searchInputRef.current?.value;
100
- if (query) {
101
- const matched = await searcher.match(query);
102
- setSearchResult(matched || DEFAULT_RESULT);
103
- setIsSearching(false);
104
- }
105
- }
50
+ const { currentRenderType, currentSuggestions, currentSuggestionIndex, clearQuery, handleQueryInput, initStatus, isSearching, query, resultTabIndex, searchError, searchIndexURL, searchResult, searchEnabled, setCurrentSuggestionIndex, setResultTabIndex } = useSearchPanel({
51
+ focused,
52
+ searchInputRef
53
+ });
54
+ if (!searchEnabled) return null;
106
55
  const clearSearchState = ()=>{
107
56
  setFocused(false);
108
57
  setResultTabIndex(0);
@@ -166,81 +115,12 @@ function SearchPanel({ focused, setFocused }) {
166
115
  currentSuggestions,
167
116
  currentSuggestionIndex
168
117
  ]);
169
- useEffect(()=>{
170
- if (focused) {
171
- setSearchResult(DEFAULT_RESULT);
172
- initSearch();
173
- } else setQuery('');
174
- }, [
175
- focused
176
- ]);
177
- useEffect(()=>{
178
- let idleCallbackID;
179
- if ('requestIdleCallback' in window && !pageSearcherRef.current) idleCallbackID = window.requestIdleCallback(()=>{
180
- const searcher = createSearcher();
181
- searcher.fetchSearchIndex();
182
- });
183
- return ()=>{
184
- if (void 0 !== idleCallbackID && 'cancelIdleCallback' in window) window.cancelIdleCallback(idleCallbackID);
185
- };
186
- }, []);
187
- useEffect(()=>{
188
- const { currentLang, currentVersion } = pageSearcherConfigRef.current ?? {};
189
- const isLangChanged = lang !== currentLang;
190
- const isVersionChanged = versionedSearch && version !== currentVersion;
191
- if (isLangChanged || isVersionChanged) {
192
- setInitStatus('initial');
193
- pageSearcherRef.current = null;
194
- const searcher = createSearcher();
195
- searcher.fetchSearchIndex();
196
- }
197
- }, [
198
- lang,
199
- version,
200
- versionedSearch
201
- ]);
202
- const handleQueryChangedImpl = async (value)=>{
203
- let newQuery = value;
204
- setQuery(newQuery);
205
- if (newQuery) {
206
- const searchResult = [];
207
- if ("beforeSearch" in __rspack_external_virtual_search_hooks_705b6e40) {
208
- const key = 'beforeSearch';
209
- const transformedQuery = await __rspack_external_virtual_search_hooks_705b6e40[key](newQuery);
210
- if (transformedQuery) newQuery = transformedQuery;
211
- }
212
- const defaultSearchResult = await pageSearcherRef.current?.match(newQuery);
213
- if (defaultSearchResult) searchResult.push(...defaultSearchResult);
214
- if ("onSearch" in __rspack_external_virtual_search_hooks_705b6e40) {
215
- const key = 'onSearch';
216
- const customSearchResult = await __rspack_external_virtual_search_hooks_705b6e40[key](newQuery, searchResult);
217
- if (customSearchResult) searchResult.push(...customSearchResult.map((item)=>({
218
- renderType: RenderType.Custom,
219
- ...item
220
- })));
221
- }
222
- if ("afterSearch" in __rspack_external_virtual_search_hooks_705b6e40) {
223
- const key = 'afterSearch';
224
- await __rspack_external_virtual_search_hooks_705b6e40[key](newQuery, searchResult);
225
- }
226
- const currQuery = searchInputRef.current?.value;
227
- if (currQuery === newQuery) {
228
- setCurrentSuggestionIndex(0);
229
- setSearchResult(searchResult || DEFAULT_RESULT);
230
- setIsSearching(false);
231
- }
232
- }
233
- };
234
- const handleQueryChange = useDebounce(handleQueryChangedImpl);
235
- const renderSearchResult = (result, isSearching)=>{
118
+ const renderSearchResult = (result)=>{
236
119
  if (1 === result.length) {
237
120
  const currentSearchResult = result[0].result;
238
- if (0 === currentSearchResult.length && !isSearching) return /*#__PURE__*/ jsx(NoSearchResult, {
239
- query: query
240
- });
241
121
  return /*#__PURE__*/ jsx("div", {
242
122
  ref: searchResultTabRef,
243
- children: renderSearchResultItem(currentSearchResult, query, isSearching)
123
+ children: renderSearchResultItem(currentSearchResult)
244
124
  });
245
125
  }
246
126
  const renderKey = 'render';
@@ -255,20 +135,14 @@ function SearchPanel({ focused, setFocused }) {
255
135
  children: result.map((item)=>/*#__PURE__*/ jsxs(Tab, {
256
136
  label: item.group,
257
137
  children: [
258
- item.renderType === RenderType.Default && renderSearchResultItem(item.result, query, isSearching),
138
+ item.renderType === RenderType.Default && renderSearchResultItem(item.result),
259
139
  item.renderType === RenderType.Custom && __rspack_external_virtual_search_hooks_705b6e40[renderKey](item.result)
260
140
  ]
261
141
  }, item.group))
262
142
  });
263
143
  };
264
- const renderSearchResultItem = (suggestionList, query, isSearching)=>{
265
- if (isSearching) return /*#__PURE__*/ jsx("div", {
266
- className: "rp-search-panel__loading",
267
- children: /*#__PURE__*/ jsx(SvgWrapper, {
268
- icon: IconLoading
269
- })
270
- });
271
- if (0 === suggestionList.length && 'inited' === initStatus) return /*#__PURE__*/ jsx(NoSearchResult, {
144
+ const renderSearchResultItem = (suggestionList)=>{
145
+ if (0 === suggestionList.length && 'inited' === initStatus) return isSearching ? null : /*#__PURE__*/ jsx(NoSearchResult, {
272
146
  query: query
273
147
  });
274
148
  const normalizedSuggestions = normalizeSuggestions(suggestionList);
@@ -308,75 +182,93 @@ function SearchPanel({ focused, setFocused }) {
308
182
  })
309
183
  });
310
184
  };
311
- return /*#__PURE__*/ jsx(Fragment, {
312
- children: focused && /*#__PURE__*/ createPortal(/*#__PURE__*/ jsx("div", {
313
- className: "rp-search-panel__mask",
314
- onClick: ()=>{
315
- clearSearchState();
316
- },
317
- children: /*#__PURE__*/ jsxs("div", {
318
- className: "rp-search-panel__modal",
319
- onClick: (e)=>{
320
- setFocused(true);
321
- e.stopPropagation();
185
+ return /*#__PURE__*/ jsxs(Fragment, {
186
+ children: [
187
+ searchIndexURL && /*#__PURE__*/ jsx(Head, {
188
+ children: /*#__PURE__*/ jsx("link", {
189
+ rel: "prefetch",
190
+ href: searchIndexURL
191
+ })
192
+ }),
193
+ focused && /*#__PURE__*/ createPortal(/*#__PURE__*/ jsx("div", {
194
+ className: "rp-search-panel__mask",
195
+ onClick: ()=>{
196
+ clearSearchState();
322
197
  },
323
- children: [
324
- /*#__PURE__*/ jsxs("div", {
325
- className: "rp-search-panel__header",
326
- children: [
327
- /*#__PURE__*/ jsxs("div", {
328
- className: "rp-search-panel__input-form",
329
- children: [
330
- /*#__PURE__*/ jsx("label", {
331
- children: /*#__PURE__*/ jsx(SvgWrapper, {
332
- icon: IconSearch
333
- })
334
- }),
335
- /*#__PURE__*/ jsx("input", {
336
- className: "rp-search-panel__input",
337
- ref: searchInputRef,
338
- placeholder: t('searchPlaceholderText'),
339
- "aria-label": "SearchPanelInput",
340
- autoComplete: "off",
341
- autoFocus: true,
342
- inputMode: "search",
343
- onChange: (e)=>handleQueryChange(e.target.value)
344
- }),
345
- /*#__PURE__*/ jsx("label", {
346
- children: /*#__PURE__*/ jsx(SvgWrapper, {
347
- icon: IconClose,
348
- className: "rp-search-panel__close",
349
- onClick: (e)=>{
350
- if (searchInputRef.current) {
351
- e.stopPropagation();
352
- if (query) {
353
- searchInputRef.current.value = '';
354
- setQuery('');
355
- } else clearSearchState();
356
- }
198
+ children: /*#__PURE__*/ jsxs("div", {
199
+ className: "rp-search-panel__modal",
200
+ onClick: (e)=>{
201
+ setFocused(true);
202
+ e.stopPropagation();
203
+ },
204
+ children: [
205
+ /*#__PURE__*/ jsxs("div", {
206
+ className: "rp-search-panel__header",
207
+ children: [
208
+ /*#__PURE__*/ jsxs("div", {
209
+ className: "rp-search-panel__input-form",
210
+ children: [
211
+ /*#__PURE__*/ jsx("label", {
212
+ children: /*#__PURE__*/ jsx(SvgWrapper, {
213
+ icon: IconSearch,
214
+ className: `rp-search-panel__search-icon${'initing' === initStatus || isSearching ? ' rp-search-panel__search-icon--loading' : ''}`
215
+ })
216
+ }),
217
+ /*#__PURE__*/ jsx("input", {
218
+ className: "rp-search-panel__input",
219
+ ref: searchInputRef,
220
+ placeholder: t('searchPlaceholderText'),
221
+ "aria-label": "SearchPanelInput",
222
+ autoComplete: "off",
223
+ autoFocus: true,
224
+ inputMode: "search",
225
+ onChange: (e)=>{
226
+ const value = e.target.value;
227
+ handleQueryInput(value);
357
228
  }
229
+ }),
230
+ /*#__PURE__*/ jsx("label", {
231
+ children: /*#__PURE__*/ jsx(SvgWrapper, {
232
+ icon: IconClose,
233
+ className: "rp-search-panel__close",
234
+ onClick: (e)=>{
235
+ if (searchInputRef.current) {
236
+ e.stopPropagation();
237
+ if (query) {
238
+ searchInputRef.current.value = '';
239
+ clearQuery();
240
+ } else clearSearchState();
241
+ }
242
+ }
243
+ })
358
244
  })
359
- })
245
+ ]
246
+ }),
247
+ /*#__PURE__*/ jsx("h2", {
248
+ className: "rp-search-panel__cancel",
249
+ onClick: (e)=>{
250
+ e.stopPropagation();
251
+ clearSearchState();
252
+ },
253
+ children: t('searchPanelCancelText')
254
+ })
255
+ ]
256
+ }),
257
+ (query || 'error' === initStatus) && ('inited' === initStatus || 'error' === initStatus) ? /*#__PURE__*/ jsx("div", {
258
+ className: "rp-search-panel__results rp-scrollbar",
259
+ ref: searchResultRef,
260
+ children: 'error' === initStatus ? /*#__PURE__*/ jsxs("div", {
261
+ className: "rp-search-panel__error",
262
+ children: [
263
+ "Error: ",
264
+ searchError
360
265
  ]
361
- }),
362
- /*#__PURE__*/ jsx("h2", {
363
- className: "rp-search-panel__cancel",
364
- onClick: (e)=>{
365
- e.stopPropagation();
366
- clearSearchState();
367
- },
368
- children: t('searchPanelCancelText')
369
- })
370
- ]
371
- }),
372
- query && 'inited' === initStatus ? /*#__PURE__*/ jsx("div", {
373
- className: "rp-search-panel__results rp-scrollbar",
374
- ref: searchResultRef,
375
- children: renderSearchResult(searchResult, isSearching)
376
- }) : null
377
- ]
378
- })
379
- }), document.getElementById('__rspress_modal_container'))
266
+ }) : renderSearchResult(searchResult)
267
+ }) : null
268
+ ]
269
+ })
270
+ }), document.getElementById('__rspress_modal_container'))
271
+ ]
380
272
  });
381
273
  }
382
274
  export { SearchPanel };
@@ -5,6 +5,7 @@
5
5
  import { type PageIndexInfo } from '@rspress/shared';
6
6
  import { type Provider, type SearchQuery } from '../Provider.js';
7
7
  import type { SearchOptions } from '../types.js';
8
+ export declare function getSearchIndexURL(lang: string, version: string): string | undefined;
8
9
  export declare class LocalProvider implements Provider {
9
10
  #private;
10
11
  fetchSearchIndex(options: SearchOptions): Promise<PageIndexInfo[]>;