@rspress/core 2.0.23 → 2.1.0-beta.2

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 (181) hide show
  1. package/dist/cli/index.js +2 -2
  2. package/dist/{common-Qw-RoVFD.js → common-Bo3KCGim.js} +4 -1
  3. package/dist/config/loadConfigFile.js +1 -1
  4. package/dist/eject-theme/components/CdnTabs/index.test.tsx +71 -0
  5. package/dist/eject-theme/components/CdnTabs/index.tsx +175 -0
  6. package/dist/eject-theme/components/DocContent/index.tsx +0 -3
  7. package/dist/eject-theme/components/FallbackHeading/index.tsx +6 -1
  8. package/dist/eject-theme/components/Link/getHref.ts +1 -0
  9. package/dist/eject-theme/components/Link/index.tsx +9 -21
  10. package/dist/eject-theme/components/Link/useLinkNavigate.ts +43 -54
  11. package/dist/eject-theme/components/NavTitle/index.test.tsx +78 -0
  12. package/dist/eject-theme/components/NavTitle/index.tsx +9 -5
  13. package/dist/eject-theme/components/PackageManagerTabs/index.test.tsx +79 -1
  14. package/dist/eject-theme/components/PackageManagerTabs/index.tsx +25 -12
  15. package/dist/eject-theme/components/Search/NoSearchResult.scss +1 -2
  16. package/dist/eject-theme/components/Search/SearchPanel.scss +75 -63
  17. package/dist/eject-theme/components/Search/SuggestItem.scss +28 -41
  18. package/dist/eject-theme/components/Search/logic/providers/LocalProvider.ts +4 -1
  19. package/dist/eject-theme/components/Search/useSearchPanel.ts +103 -36
  20. package/dist/eject-theme/components/Sidebar/SidebarItem.scss +8 -0
  21. package/dist/eject-theme/components/Sidebar/SidebarSectionHeader.scss +3 -0
  22. package/dist/eject-theme/components/SocialLinks/GithubStars.test.tsx +4 -1
  23. package/dist/eject-theme/components/SocialLinks/GithubStars.tsx +1 -1
  24. package/dist/entry/worker.js +1 -1
  25. package/dist/index.d.ts +1 -1
  26. package/dist/node/PluginDriver.d.ts +5 -0
  27. package/dist/node/PluginDriver.js +17 -2
  28. package/dist/node/auto-nav-sidebar/index.js +1 -1
  29. package/dist/node/auto-nav-sidebar/locales.js +1 -1
  30. package/dist/node/auto-nav-sidebar/normalize.js +1 -1
  31. package/dist/node/auto-nav-sidebar/utils.js +1 -1
  32. package/dist/node/auto-nav-sidebar/walk.js +1 -1
  33. package/dist/node/constants.js +1 -1
  34. package/dist/node/eject.js +1 -1
  35. package/dist/node/initRsbuild.js +19 -7
  36. package/dist/node/llms.d.ts +1 -1
  37. package/dist/node/llms.js +2 -2
  38. package/dist/node/logger/hint.d.ts +1 -0
  39. package/dist/node/logger/hint.js +8 -2
  40. package/dist/node/mdx/rehypePlugins/headerAnchor.js +1 -0
  41. package/dist/node/mdx/remarkPlugins/link.js +1 -1
  42. package/dist/node/route/RoutePage.js +1 -1
  43. package/dist/node/route/RouteService.js +1 -1
  44. package/dist/node/route/extractPageData.js +1 -1
  45. package/dist/node/route/normalizeRoutePath.js +1 -1
  46. package/dist/node/runtimeModule/{DEFAULT_I18N_TEXT.d.ts → i18n/DEFAULT_I18N_TEXT.d.ts} +28 -0
  47. package/dist/node/runtimeModule/{DEFAULT_I18N_TEXT.js → i18n/DEFAULT_I18N_TEXT.js} +28 -0
  48. package/dist/node/runtimeModule/i18n/getI18nData.d.ts +11 -0
  49. package/dist/node/runtimeModule/{i18n.js → i18n/getI18nData.js} +35 -22
  50. package/dist/node/runtimeModule/i18n/rsbuildPlugin.d.ts +5 -0
  51. package/dist/node/runtimeModule/i18n/rsbuildPlugin.js +21 -0
  52. package/dist/node/runtimeModule/pageData/createPageData.js +1 -1
  53. package/dist/node/runtimeModule/pageData/rsbuildPlugin.js +1 -1
  54. package/dist/node/runtimeModule/searchHooks.js +2 -2
  55. package/dist/node/runtimeModule/siteData/createSiteData.d.ts +2 -1
  56. package/dist/node/runtimeModule/siteData/createSiteData.js +3 -3
  57. package/dist/node/runtimeModule/siteData/normalizeThemeConfig.d.ts +2 -1
  58. package/dist/node/runtimeModule/siteData/normalizeThemeConfig.js +3 -4
  59. package/dist/node/runtimeModule/siteData/rsbuildPlugin.js +2 -2
  60. package/dist/node/serve.js +1 -1
  61. package/dist/node/sitemap.d.ts +7 -0
  62. package/dist/node/sitemap.js +86 -0
  63. package/dist/node/ssg/alternateLinks.js +1 -1
  64. package/dist/node/ssg/htmlFile.js +1 -1
  65. package/dist/node/ssg/renderHtmlTemplate.js +1 -1
  66. package/dist/node/ssg/renderPage.js +1 -1
  67. package/dist/node/ssg/renderPageWorker.js +1 -1
  68. package/dist/node/ssg/renderPages.js +2 -2
  69. package/dist/node/ssg/rsbuildPluginSSG.js +1 -1
  70. package/dist/node/ssg/ssgEnv.js +1 -1
  71. package/dist/node/ssg-md/llms/emitLlmsTxt.js +1 -1
  72. package/dist/node/ssg-md/llms/llmsTxt.js +1 -1
  73. package/dist/node/ssg-md/renderPage.js +5 -4
  74. package/dist/node/ssg-md/renderPages.js +2 -2
  75. package/dist/node/ssg-md/rsbuildPluginSSGMD.js +1 -1
  76. package/dist/node/utils/checkLanguageParity.js +1 -1
  77. package/dist/node/utils/createHash.js +1 -1
  78. package/dist/node/utils/flattenMdxContent.js +1 -1
  79. package/dist/node/utils/getPageKey.js +1 -1
  80. package/dist/node/utils/reactAlias.js +6 -3
  81. package/dist/{a~0.js → p~0.js} +23 -18
  82. package/dist/runtime/App.js +20 -29
  83. package/dist/runtime/BrowserOnly.d.ts +1 -1
  84. package/dist/runtime/BrowserOnly.js +53 -20
  85. package/dist/runtime/ClientApp.d.ts +3 -2
  86. package/dist/runtime/ClientApp.js +31 -21
  87. package/dist/runtime/ErrorRoute.d.ts +1 -0
  88. package/dist/runtime/ErrorRoute.js +30 -0
  89. package/dist/runtime/NoSSR.d.ts +7 -3
  90. package/dist/runtime/NoSSR.js +30 -9
  91. package/dist/runtime/PageRoute.d.ts +2 -0
  92. package/dist/runtime/PageRoute.js +70 -0
  93. package/dist/runtime/createServerRouter.d.ts +4 -0
  94. package/dist/runtime/createServerRouter.js +17 -0
  95. package/dist/runtime/csrClientEntry.js +5 -2
  96. package/dist/runtime/hooks/useWebMcp.d.ts +36 -0
  97. package/dist/runtime/hooks/useWebMcp.js +80 -0
  98. package/dist/runtime/index.d.ts +6 -3
  99. package/dist/runtime/index.js +6 -5
  100. package/dist/runtime/initPageData.d.ts +0 -3
  101. package/dist/runtime/initPageData.js +9 -45
  102. package/dist/runtime/pageDataLoader.d.ts +11 -0
  103. package/dist/runtime/pageDataLoader.js +7 -0
  104. package/dist/runtime/react.d.ts +5 -0
  105. package/dist/runtime/react.js +4 -0
  106. package/dist/runtime/reactDom.d.ts +6 -0
  107. package/dist/runtime/reactDom.js +2 -1
  108. package/dist/runtime/socialLinks.d.ts +1 -0
  109. package/dist/runtime/socialLinks.js +3 -0
  110. package/dist/runtime/ssrClientEntry.js +14 -6
  111. package/dist/runtime/ssrMdServerEntry.js +10 -21
  112. package/dist/runtime/ssrServerEntry.js +10 -21
  113. package/dist/static/assets/package.json +58 -0
  114. package/dist/theme/assets/bun.js +101 -0
  115. package/dist/theme/assets/deno.js +15 -0
  116. package/dist/theme/assets/jsdelivr.js +53 -0
  117. package/dist/theme/{components/PackageManagerTabs/icons/Npm.js → assets/npm.js} +4 -4
  118. package/dist/theme/{components/PackageManagerTabs/icons/Pnpm.js → assets/pnpm.js} +4 -4
  119. package/dist/theme/assets/unpkg.js +15 -0
  120. package/dist/theme/assets/yarn.js +22 -0
  121. package/dist/theme/components/CdnTabs/index.d.ts +10 -0
  122. package/dist/theme/components/CdnTabs/index.js +146 -0
  123. package/dist/theme/components/DocContent/index.js +1 -2
  124. package/dist/theme/components/FallbackHeading/index.js +1 -0
  125. package/dist/theme/components/Link/index.js +4 -17
  126. package/dist/theme/components/Link/useLinkNavigate.d.ts +2 -2
  127. package/dist/theme/components/Link/useLinkNavigate.js +22 -36
  128. package/dist/theme/components/NavTitle/index.js +10 -5
  129. package/dist/theme/components/PackageManagerTabs/index.d.ts +2 -0
  130. package/dist/theme/components/PackageManagerTabs/index.js +22 -12
  131. package/dist/theme/components/Search/NoSearchResult.css +1 -2
  132. package/dist/theme/components/Search/SearchPanel.css +72 -51
  133. package/dist/theme/components/Search/SuggestItem.css +21 -34
  134. package/dist/theme/components/Search/logic/providers/LocalProvider.js +4 -1
  135. package/dist/theme/components/Search/useSearchPanel.js +74 -26
  136. package/dist/theme/components/Sidebar/SidebarItem.css +5 -0
  137. package/dist/theme/components/Sidebar/SidebarSectionHeader.css +1 -0
  138. package/dist/theme/components/SocialLinks/GithubStars.js +1 -1
  139. package/dist/theme/hooks/useDynamicToc.js +27 -34
  140. package/dist/theme/hooks/useWebMcpNavigation.d.ts +6 -0
  141. package/dist/theme/hooks/useWebMcpNavigation.js +123 -0
  142. package/dist/theme/hooks/useWebMcpPageList.d.ts +6 -0
  143. package/dist/theme/hooks/useWebMcpPageList.js +25 -0
  144. package/dist/theme/hooks/useWebMcpPageReading.d.ts +7 -0
  145. package/dist/theme/hooks/useWebMcpPageReading.js +23 -0
  146. package/dist/theme/hooks/useWebMcpSiteInfo.d.ts +6 -0
  147. package/dist/theme/hooks/useWebMcpSiteInfo.js +22 -0
  148. package/dist/theme/icons.d.ts +7 -0
  149. package/dist/theme/icons.js +15 -1
  150. package/dist/theme/index.d.ts +6 -2
  151. package/dist/theme/index.js +6 -2
  152. package/dist/theme/layout/ErrorLayout/index.css +52 -0
  153. package/dist/theme/layout/ErrorLayout/index.d.ts +8 -0
  154. package/dist/theme/layout/ErrorLayout/index.js +76 -0
  155. package/dist/theme/layout/Layout/index.js +1 -2
  156. package/dist/theme/webMcp/tools.d.ts +56 -0
  157. package/dist/theme/webMcp/tools.js +122 -0
  158. package/dist/theme/webMcp/types.d.ts +35 -0
  159. package/dist/theme/webMcp/types.js +0 -0
  160. package/dist/tinypool~0.js +19 -49
  161. package/dist/y~0.js +166 -0
  162. package/package.json +18 -18
  163. package/dist/b~0.js +0 -106
  164. package/dist/eject-theme/components/PackageManagerTabs/icons/Bun.tsx +0 -101
  165. package/dist/eject-theme/components/PackageManagerTabs/icons/Deno.tsx +0 -18
  166. package/dist/eject-theme/components/PackageManagerTabs/icons/Npm.tsx +0 -16
  167. package/dist/eject-theme/components/PackageManagerTabs/icons/Pnpm.tsx +0 -22
  168. package/dist/eject-theme/components/PackageManagerTabs/icons/Yarn.tsx +0 -18
  169. package/dist/node/runtimeModule/i18n.d.ts +0 -8
  170. package/dist/theme/components/PackageManagerTabs/icons/Bun.d.ts +0 -2
  171. package/dist/theme/components/PackageManagerTabs/icons/Bun.js +0 -137
  172. package/dist/theme/components/PackageManagerTabs/icons/Deno.d.ts +0 -2
  173. package/dist/theme/components/PackageManagerTabs/icons/Deno.js +0 -15
  174. package/dist/theme/components/PackageManagerTabs/icons/Npm.d.ts +0 -2
  175. package/dist/theme/components/PackageManagerTabs/icons/Pnpm.d.ts +0 -2
  176. package/dist/theme/components/PackageManagerTabs/icons/Yarn.d.ts +0 -2
  177. package/dist/theme/components/PackageManagerTabs/icons/Yarn.js +0 -22
  178. package/dist/theme/logic/useScrollAfterNav.d.ts +0 -5
  179. package/dist/theme/logic/useScrollAfterNav.js +0 -35
  180. package/dist/theme/logic/useScrollReset.d.ts +0 -5
  181. package/dist/theme/logic/useScrollReset.js +0 -12
@@ -1,12 +1,18 @@
1
1
  import { afterEach, describe, expect, it, rs } from '@rstest/core';
2
2
  import type { ReactNode } from 'react';
3
3
  import { renderToMarkdownString } from 'react-render-to-markdown';
4
- import { PackageManagerTabs } from './index';
4
+ import { PackageManagerTabs, type PackageManagerTabProps } from './index';
5
5
 
6
6
  rs.mock('@rspress/core/theme', () => ({
7
7
  getCustomMDXComponent: () => ({
8
8
  pre: 'pre',
9
9
  }),
10
+ IconBun: 'bun',
11
+ IconDeno: 'deno',
12
+ IconNpm: 'npm',
13
+ IconPnpm: 'pnpm',
14
+ IconYarn: 'yarn',
15
+ SvgWrapper: () => null,
10
16
  Tab: ({ children }: { children: ReactNode }) => children,
11
17
  Tabs: ({ children }: { children: ReactNode }) => children,
12
18
  }));
@@ -27,6 +33,78 @@ afterEach(() => {
27
33
  });
28
34
 
29
35
  describe('PackageManagerTabs', () => {
36
+ it.each<PackageManagerTabProps>([
37
+ { command: 'install -D @rspress/core' },
38
+ { command: 'rspress --help', exec: true },
39
+ { command: 'rspress --help', dlx: true },
40
+ ])('renders an additional tab command as written with %j', async props => {
41
+ setSsgMd(true);
42
+
43
+ const markdown = await renderToMarkdownString(
44
+ <PackageManagerTabs
45
+ {...props}
46
+ additionalTabs={[
47
+ { tool: 'custom', command: 'corepack pnpm add --dev @rspress/core' },
48
+ ]}
49
+ />,
50
+ );
51
+
52
+ expect(markdown).toContain(
53
+ '```sh [corepack]\ncorepack pnpm add --dev @rspress/core\n```',
54
+ );
55
+ expect(markdown).not.toContain('custom ');
56
+ });
57
+
58
+ it('keeps generating commands for additional tabs without an explicit command', async () => {
59
+ setSsgMd(true);
60
+
61
+ const markdown = await renderToMarkdownString(
62
+ <PackageManagerTabs
63
+ command="install -D @rspress/core"
64
+ additionalTabs={[{ tool: 'custom' }]}
65
+ />,
66
+ );
67
+
68
+ expect(markdown).toContain(
69
+ '```sh [custom]\ncustom install -D @rspress/core\n```',
70
+ );
71
+ expect(markdown).toContain('```sh [yarn]\nyarn add -D @rspress/core\n```');
72
+ });
73
+
74
+ it('adds commands to an object command without mutating it', async () => {
75
+ setSsgMd(true);
76
+ const command = Object.freeze({ npm: 'npm install @rspress/core' });
77
+
78
+ const markdown = await renderToMarkdownString(
79
+ <PackageManagerTabs
80
+ command={command}
81
+ additionalTabs={[
82
+ { tool: 'custom', command: 'custom add @rspress/core' },
83
+ { tool: 'unused' },
84
+ ]}
85
+ />,
86
+ );
87
+
88
+ expect(markdown).toContain('```sh [npm]\nnpm install @rspress/core\n```');
89
+ expect(markdown).toContain('```sh [custom]\ncustom add @rspress/core\n```');
90
+ expect(markdown).not.toContain('unused');
91
+ expect(command).toEqual({ npm: 'npm install @rspress/core' });
92
+ });
93
+
94
+ it('does not normalize an explicit command for a built-in tool', async () => {
95
+ setSsgMd(true);
96
+
97
+ const markdown = await renderToMarkdownString(
98
+ <PackageManagerTabs
99
+ command="install -D @rspress/core"
100
+ additionalTabs={[{ tool: 'yarn', command: 'yarn install --immutable' }]}
101
+ />,
102
+ );
103
+
104
+ expect(markdown).toContain('```sh [yarn]\nyarn install --immutable\n```');
105
+ expect(markdown).not.toContain('yarn add');
106
+ });
107
+
30
108
  it('renders install commands as markdown code fences when SSG_MD is enabled', async () => {
31
109
  setSsgMd(true);
32
110
 
@@ -1,10 +1,15 @@
1
- import { getCustomMDXComponent, Tab, Tabs } from '@rspress/core/theme';
1
+ import {
2
+ getCustomMDXComponent,
3
+ IconBun,
4
+ IconDeno,
5
+ IconNpm,
6
+ IconPnpm,
7
+ IconYarn,
8
+ SvgWrapper,
9
+ Tab,
10
+ Tabs,
11
+ } from '@rspress/core/theme';
2
12
  import { type ReactNode, useMemo } from 'react';
3
- import { Bun } from './icons/Bun';
4
- import { Deno } from './icons/Deno';
5
- import { Npm } from './icons/Npm';
6
- import { Pnpm } from './icons/Pnpm';
7
- import { Yarn } from './icons/Yarn';
8
13
 
9
14
  export type PackageManagerTabProps = (
10
15
  | {
@@ -35,6 +40,8 @@ export type PackageManagerTabProps = (
35
40
  ) & {
36
41
  additionalTabs?: {
37
42
  tool: string;
43
+ /** Full command for this tab, rendered as written. */
44
+ command?: string;
38
45
  icon?: ReactNode;
39
46
  }[];
40
47
  };
@@ -143,11 +150,11 @@ export function PackageManagerTabs({
143
150
  let commandInfo: Record<string, string>;
144
151
  // Init Icons
145
152
  const packageMangerToIcon: Record<string, ReactNode> = {
146
- npm: <Npm />,
147
- yarn: <Yarn />,
148
- pnpm: <Pnpm />,
149
- bun: <Bun />,
150
- deno: <Deno />,
153
+ npm: <SvgWrapper icon={IconNpm} />,
154
+ yarn: <SvgWrapper icon={IconYarn} />,
155
+ pnpm: <SvgWrapper icon={IconPnpm} />,
156
+ bun: <SvgWrapper icon={IconBun} />,
157
+ deno: <SvgWrapper icon={IconDeno} />,
151
158
  };
152
159
  additionalTabs.forEach(tab => {
153
160
  packageMangerToIcon[tab.tool] = tab.icon;
@@ -215,9 +222,15 @@ export function PackageManagerTabs({
215
222
  commandInfo.deno = normalizeCommand(commandInfo.deno);
216
223
  } else {
217
224
  // When using { "yarn": "", "pnpm": "", "bun": "", "deno": "" } as command we don't normalize anything
218
- commandInfo = command;
225
+ commandInfo = { ...command };
219
226
  }
220
227
 
228
+ additionalTabs.forEach(tab => {
229
+ if (tab.command !== undefined) {
230
+ commandInfo[tab.tool] = tab.command;
231
+ }
232
+ });
233
+
221
234
  if (import.meta.env.SSG_MD) {
222
235
  return Object.values(commandInfo).reduce((previous, current) => {
223
236
  const [packageManager, command] = splitTo2Parts(current);
@@ -2,8 +2,7 @@
2
2
  display: flex;
3
3
  flex-direction: column;
4
4
  align-items: center;
5
- padding-top: 2rem;
6
- padding-bottom: 0.5rem;
5
+ padding: 2rem 1rem;
7
6
 
8
7
  &__icon {
9
8
  margin-bottom: 1rem;
@@ -1,10 +1,7 @@
1
1
  .rp-search-panel {
2
2
  &__mask {
3
3
  position: fixed;
4
- left: 0;
5
- top: 0;
6
- right: 0;
7
- bottom: 0;
4
+ inset: 0;
8
5
  background: #3c3c3c66;
9
6
  backdrop-filter: blur(5px);
10
7
  // Keep the modal above the sticky banner (z-index: 999).
@@ -12,71 +9,109 @@
12
9
  }
13
10
 
14
11
  &__modal {
12
+ --rp-search-panel-top: min(80px, 10dvh);
13
+
15
14
  position: relative;
16
- background-color: var(--rp-c-bg-soft);
17
- border-radius: var(--rp-radius);
18
- margin: 80px auto auto;
19
- max-width: 560px;
20
- padding: 20px;
21
- padding-bottom: 20px;
22
- height: auto;
15
+ display: flex;
16
+ flex-direction: column;
17
+ margin: calc(var(--rp-search-panel-top) + env(safe-area-inset-top, 0px))
18
+ auto auto;
19
+ width: calc(100% - 48px);
20
+ max-width: 640px;
21
+ max-height: calc(
22
+ 100dvh - var(--rp-search-panel-top) -
23
+ 24px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)
24
+ );
25
+ overflow: hidden;
26
+ border: 1px solid var(--rp-c-divider-light);
27
+ border-radius: 16px;
28
+ background-color: var(--rp-c-bg);
29
+ box-shadow: 0 16px 48px #0003;
23
30
  animation: rp-search-panel-in 0.18s ease-out;
24
31
 
25
32
  @media (prefers-reduced-motion: reduce) {
26
33
  animation: none;
27
34
  }
35
+
36
+ @media (max-width: 768px) {
37
+ --rp-search-panel-top: 24px;
38
+
39
+ width: calc(100% - 24px);
40
+ max-width: 560px;
41
+ }
28
42
  }
29
43
 
30
44
  &__header {
31
45
  display: flex;
32
46
  align-items: center;
47
+ flex-shrink: 0;
48
+ gap: 8px;
49
+ padding: 12px 20px;
50
+
51
+ @media (max-width: 768px) {
52
+ padding: 8px 12px;
53
+ }
33
54
  }
34
55
 
35
56
  &__cancel {
57
+ display: none;
36
58
  color: var(--rp-c-brand);
37
- margin-left: 0.5rem;
38
59
  cursor: pointer;
39
- // CJK cancel text can break between characters and get squeezed
40
- // by the 100%-width input form, keep it on one line.
60
+ // Keep CJK cancel text on one line when the input shrinks.
41
61
  white-space: nowrap;
42
62
 
43
- @media (min-width: 640px) {
44
- display: none;
63
+ @media (max-width: 768px) {
64
+ display: flex;
65
+ align-items: center;
66
+ flex-shrink: 0;
67
+ min-height: 44px;
68
+ margin: 0;
69
+ padding: 0 4px;
70
+ font-size: 14px;
71
+ font-weight: 500;
45
72
  }
46
73
  }
47
74
 
48
75
  &__input-form {
49
- width: 100%;
50
- height: 55px;
51
76
  display: flex;
52
77
  align-items: center;
53
- padding: 0 12px;
54
- background-color: var(--rp-c-bg);
55
- border: 1px solid var(--rp-c-brand);
56
- border-radius: var(--rp-radius-small);
78
+ flex: 1;
79
+ min-width: 0;
80
+ height: 48px;
81
+
82
+ @media (max-width: 768px) {
83
+ height: 44px;
84
+ }
57
85
  }
58
86
 
59
87
  &__input {
60
88
  flex: 1;
61
- padding-left: 8px;
89
+ min-width: 0;
90
+ padding-left: 10px;
62
91
  height: 100%;
63
- width: 80%;
64
92
  outline: none;
65
93
  background-color: var(--rp-c-bg);
66
- font-weight: 500;
67
- font-size: 20px;
94
+ color: var(--rp-c-text-1);
95
+ font-weight: 400;
96
+ font-size: 18px;
68
97
 
69
- .dark & {
70
- color: var(--rp-c-text);
98
+ @media (max-width: 768px) {
99
+ font-size: 16px;
71
100
  }
72
101
  }
73
102
 
103
+ &__search-icon,
74
104
  &__close {
75
- &:hover {
76
- cursor: pointer;
77
- color: var(--rp-c-brand);
78
- transition: color 0.3s;
79
- }
105
+ display: block;
106
+ width: 20px;
107
+ height: 20px;
108
+ color: var(--rp-c-text-2);
109
+ }
110
+
111
+ &__close:hover {
112
+ cursor: pointer;
113
+ color: var(--rp-c-brand);
114
+ transition: color 0.3s;
80
115
  }
81
116
 
82
117
  &__search-icon--loading {
@@ -87,15 +122,12 @@
87
122
  }
88
123
  }
89
124
 
90
- &__search-icon {
91
- display: inline-block;
92
- font-size: 20px;
93
- }
94
-
95
125
  &__results {
96
- max-height: calc(100vh - 230px);
97
- overflow: scroll;
98
- padding-right: 2px;
126
+ min-height: 0;
127
+ padding: 8px;
128
+ border-top: 1px solid var(--rp-c-divider-light);
129
+ overflow: auto;
130
+ overscroll-behavior: contain;
99
131
  }
100
132
 
101
133
  &__error {
@@ -106,7 +138,7 @@
106
138
 
107
139
  &__tabs {
108
140
  position: sticky;
109
- background-color: var(--rp-c-bg-soft);
141
+ background-color: var(--rp-c-bg);
110
142
  padding-bottom: 6px;
111
143
 
112
144
  &.rp-tabs {
@@ -118,27 +150,7 @@
118
150
  }
119
151
 
120
152
  &__group {
121
- padding-bottom: 0.5rem;
122
- }
123
-
124
- @media (max-width: 960px) {
125
- &__modal {
126
- margin-top: calc(32px + env(safe-area-inset-top, 0px));
127
- }
128
-
129
- &__input-form {
130
- height: 40px;
131
- }
132
-
133
- &__search-icon,
134
- &__close {
135
- width: 16px;
136
- height: 16px;
137
- }
138
-
139
- &__input {
140
- font-size: 16px;
141
- }
153
+ padding-bottom: 4px;
142
154
  }
143
155
  }
144
156
 
@@ -1,53 +1,15 @@
1
1
  .rp-suggest-item {
2
2
  list-style: none;
3
3
  box-sizing: border-box;
4
- margin: 5px 2px;
4
+ margin: 4px 0;
5
5
 
6
- .dark &,
7
- .dark & .rp-suggest-item__link {
8
- box-shadow: none;
9
- }
10
-
11
- & .rp-suggest-item__link {
6
+ &__link {
12
7
  color: var(--rp-c-text-1);
13
8
  background-color: var(--rp-c-bg);
14
9
  border-radius: var(--rp-radius-small);
15
10
  padding-left: 12px;
16
11
  display: flex;
17
12
  width: 100%;
18
- box-shadow: 0 1px 3px 0 #d4d9e1;
19
- }
20
-
21
- &--current {
22
- .rp-suggest-item__link {
23
- background-color: var(--rp-c-brand);
24
- cursor: pointer;
25
- color: #ffffff;
26
- }
27
-
28
- .rp-suggest-item__container {
29
- .rp-suggest-item__icon {
30
- color: #ffffff;
31
- }
32
-
33
- .rp-suggest-item__action-icon {
34
- opacity: 1;
35
- }
36
-
37
- .rp-suggest-item__content {
38
- .rp-suggest-item__statement {
39
- color: #ffffff;
40
- }
41
- .rp-suggest-item__mark {
42
- color: #ffffff;
43
- text-decoration: underline;
44
- }
45
-
46
- .rp-suggest-item__title {
47
- color: #ffffff;
48
- }
49
- }
50
- }
51
13
  }
52
14
 
53
15
  &__container {
@@ -64,7 +26,9 @@
64
26
  }
65
27
 
66
28
  &__content {
67
- padding: 6px 8px 6px;
29
+ min-width: 0;
30
+ padding: 6px 8px;
31
+ overflow-wrap: anywhere;
68
32
  font-size: 14px;
69
33
  line-height: 1.5;
70
34
  width: 100%;
@@ -92,4 +56,27 @@
92
56
  &__action-icon {
93
57
  opacity: 0;
94
58
  }
59
+
60
+ &--current {
61
+ .rp-suggest-item__link {
62
+ background-color: var(--rp-c-brand);
63
+ cursor: pointer;
64
+ }
65
+
66
+ .rp-suggest-item__link,
67
+ .rp-suggest-item__icon,
68
+ .rp-suggest-item__statement,
69
+ .rp-suggest-item__mark,
70
+ .rp-suggest-item__title {
71
+ color: #fff;
72
+ }
73
+
74
+ .rp-suggest-item__mark {
75
+ text-decoration: underline;
76
+ }
77
+
78
+ .rp-suggest-item__action-icon {
79
+ opacity: 1;
80
+ }
81
+ }
95
82
  }
@@ -124,7 +124,10 @@ export class LocalProvider implements Provider {
124
124
  this.#fetchPromise = this.#getPages(
125
125
  currentLang,
126
126
  versioned ? currentVersion : '',
127
- );
127
+ ).catch(error => {
128
+ this.#fetchPromise = undefined;
129
+ throw error;
130
+ });
128
131
 
129
132
  return this.#fetchPromise;
130
133
  }
@@ -1,4 +1,4 @@
1
- import { usePageData, useSearchHooks } from '@rspress/core/runtime';
1
+ import { usePageData, useSearchHooks, useWebMcp } from '@rspress/core/runtime';
2
2
  import type { LocalSearchOptions } from '@rspress/core';
3
3
  import { debounce } from '@rspress/shared/lodash-es';
4
4
  import {
@@ -18,6 +18,12 @@ import type {
18
18
  PageSearcherConfig,
19
19
  } from './logic/types';
20
20
  import { RenderType } from './logic/types';
21
+ // eslint-disable-next-line no-restricted-imports -- Existing cross-directory dependency; retained until this component is eject-safe.
22
+ import {
23
+ defineTool,
24
+ queryField,
25
+ toWebMcpSearchResults,
26
+ } from '../../webMcp/tools';
21
27
 
22
28
  const useDebounce = <T extends (...args: any[]) => any>(cb: T) => {
23
29
  const cbRef = useRef(cb);
@@ -55,6 +61,10 @@ export function useSearchPanel({
55
61
  const [searchError, setSearchError] = useState<string>();
56
62
  const pageSearcherRef = useRef<PageSearcher | null>(null);
57
63
  const pageSearcherConfigRef = useRef<PageSearcherConfig | null>(null);
64
+ const pageSearcherURLRef = useRef<string | undefined>(undefined);
65
+ const initializationRef = useRef<
66
+ { searcher: PageSearcher; promise: Promise<void> } | undefined
67
+ >(undefined);
58
68
  const searchEnabled = search !== false;
59
69
  const searchOptions: LocalSearchOptions = search === false ? {} : search;
60
70
  const versionedSearch = searchEnabled && (search?.versioned ?? true);
@@ -66,7 +76,13 @@ export function useSearchPanel({
66
76
  ];
67
77
 
68
78
  const createSearcher = () => {
69
- if (pageSearcherRef.current) {
79
+ const previous = pageSearcherConfigRef.current;
80
+ if (
81
+ pageSearcherRef.current &&
82
+ previous?.currentLang === lang &&
83
+ (!versionedSearch || previous.currentVersion === version) &&
84
+ pageSearcherURLRef.current === searchIndexURL
85
+ ) {
70
86
  return pageSearcherRef.current;
71
87
  }
72
88
  const pageSearcherConfig = { currentLang: lang, currentVersion: version };
@@ -77,9 +93,75 @@ export function useSearchPanel({
77
93
  });
78
94
  pageSearcherRef.current = pageSearcher;
79
95
  pageSearcherConfigRef.current = pageSearcherConfig;
96
+ pageSearcherURLRef.current = searchIndexURL;
80
97
  return pageSearcher;
81
98
  };
82
99
 
100
+ // UI searches and agent calls initialize the same provider once, on demand.
101
+ const initializeSearcher = (searcher: PageSearcher) => {
102
+ if (initializationRef.current?.searcher === searcher)
103
+ return initializationRef.current.promise;
104
+ const promise = searcher.init().catch(error => {
105
+ if (initializationRef.current?.searcher === searcher)
106
+ initializationRef.current = undefined;
107
+ throw error;
108
+ });
109
+ initializationRef.current = { searcher, promise };
110
+ return promise;
111
+ };
112
+
113
+ const searchDocs = async (value: string, limit?: number) => {
114
+ const searcher = createSearcher();
115
+ let newQuery = value;
116
+ const result: MatchResult = [];
117
+ if (userSearchHooks.beforeSearch) {
118
+ const transformedQuery = await userSearchHooks.beforeSearch(newQuery);
119
+ if (transformedQuery) newQuery = transformedQuery;
120
+ }
121
+ await initializeSearcher(searcher);
122
+ const defaultSearchResult = await searcher.match(newQuery, limit);
123
+ result.push(...defaultSearchResult);
124
+ if (userSearchHooks.onSearch) {
125
+ const customSearchResult = await userSearchHooks.onSearch(
126
+ newQuery,
127
+ result as DefaultMatchResult[],
128
+ );
129
+ if (customSearchResult)
130
+ result.push(
131
+ ...customSearchResult.map(
132
+ item =>
133
+ ({ renderType: RenderType.Custom, ...item }) as CustomMatchResult,
134
+ ),
135
+ );
136
+ }
137
+ await userSearchHooks.afterSearch?.(newQuery, result);
138
+ return result;
139
+ };
140
+
141
+ // Build-time gating keeps tool code out of sites that do not enable WebMCP.
142
+ if (import.meta.env.ENABLE_WEBMCP) {
143
+ useWebMcp({
144
+ ...defineTool(
145
+ 'search_docs',
146
+ 'Search documentation through the active local search component.',
147
+ {
148
+ query: queryField,
149
+ limit: { type: 'integer', minimum: 1, maximum: 20 },
150
+ },
151
+ ['query'],
152
+ async args =>
153
+ toWebMcpSearchResults(
154
+ await searchDocs(
155
+ args.query as string,
156
+ (args.limit as number | undefined) ?? 10,
157
+ ),
158
+ ),
159
+ ),
160
+ enabled:
161
+ searchEnabled && Boolean(searchIndexURL || userSearchHooks.onSearch),
162
+ });
163
+ }
164
+
83
165
  const initSearch = async () => {
84
166
  if (initStatus !== 'initial') {
85
167
  return;
@@ -88,7 +170,7 @@ export function useSearchPanel({
88
170
  setSearchError(undefined);
89
171
  setInitStatus('initing');
90
172
  try {
91
- await searcher.init();
173
+ await initializeSearcher(searcher);
92
174
  } catch (error) {
93
175
  if (pageSearcherRef.current !== searcher) {
94
176
  return;
@@ -135,52 +217,37 @@ export function useSearchPanel({
135
217
  const { currentLang, currentVersion } = pageSearcherConfigRef.current ?? {};
136
218
  const isLangChanged = lang !== currentLang;
137
219
  const isVersionChanged = versionedSearch && version !== currentVersion;
138
- if (isLangChanged || isVersionChanged) {
220
+ const isIndexChanged = pageSearcherURLRef.current !== searchIndexURL;
221
+ if (isLangChanged || isVersionChanged || isIndexChanged) {
139
222
  setInitStatus('initial');
140
223
  setSearchError(undefined);
141
224
  setIsSearching(false);
142
225
  pageSearcherRef.current = null;
143
- createSearcher()
144
- .fetchSearchIndex()
145
- .catch(() => {});
146
226
  }
147
- }, [lang, version, versionedSearch, searchEnabled]);
227
+ }, [lang, version, versionedSearch, searchEnabled, searchIndexURL]);
148
228
 
149
229
  const handleQueryChangedImpl = async (value: string) => {
150
- let newQuery = value;
230
+ const newQuery = value;
151
231
  setQuery(newQuery);
152
232
  if (!newQuery) {
153
233
  setIsSearching(false);
154
234
  return;
155
235
  }
156
- const result: MatchResult = [];
157
- if (userSearchHooks.beforeSearch) {
158
- const transformedQuery = await userSearchHooks.beforeSearch(newQuery);
159
- if (transformedQuery) newQuery = transformedQuery;
160
- }
161
- const defaultSearchResult = await pageSearcherRef.current?.match(newQuery);
162
- if (defaultSearchResult) result.push(...defaultSearchResult);
163
- if (userSearchHooks.onSearch) {
164
- const customSearchResult = await userSearchHooks.onSearch(
165
- newQuery,
166
- result as DefaultMatchResult[],
167
- );
168
- if (customSearchResult) {
169
- result.push(
170
- ...customSearchResult.map(
171
- item =>
172
- ({ renderType: RenderType.Custom, ...item }) as CustomMatchResult,
173
- ),
174
- );
236
+ try {
237
+ const result = await searchDocs(value);
238
+ if (searchInputRef.current?.value === newQuery) {
239
+ setSearchError(undefined);
240
+ setInitStatus('inited');
241
+ setCurrentSuggestionIndex(0);
242
+ setSearchResult(result || DEFAULT_RESULT);
243
+ setIsSearching(false);
244
+ }
245
+ } catch (error) {
246
+ if (searchInputRef.current?.value === newQuery) {
247
+ setSearchError(error instanceof Error ? error.message : String(error));
248
+ setInitStatus('error');
249
+ setIsSearching(false);
175
250
  }
176
- }
177
- if (userSearchHooks.afterSearch) {
178
- await userSearchHooks.afterSearch(newQuery, result);
179
- }
180
- if (searchInputRef.current?.value === newQuery) {
181
- setCurrentSuggestionIndex(0);
182
- setSearchResult(result || DEFAULT_RESULT);
183
- setIsSearching(false);
184
251
  }
185
252
  };
186
253