@rspress/core 2.1.1 → 2.1.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/dist/eject-theme/components/Llms/LlmsOpenRow.tsx +14 -3
- package/dist/eject-theme/components/Llms/LlmsViewOptions.tsx +18 -2
- package/dist/eject-theme/components/PageTabs/index.tsx +8 -18
- package/dist/eject-theme/components/Prompt/index.test.tsx +91 -0
- package/dist/eject-theme/components/Prompt/index.tsx +31 -0
- package/dist/eject-theme/components/Search/useSearchPanel.ts +85 -81
- package/dist/eject-theme/components/Sidebar/SidebarItem.scss +10 -7
- package/dist/eject-theme/components/Sidebar/SidebarItem.tsx +2 -2
- package/dist/eject-theme/components/Tabs/index.test.tsx +31 -8
- package/dist/node/runtimeModule/i18n/DEFAULT_I18N_TEXT.d.ts +463 -0
- package/dist/node/runtimeModule/i18n/DEFAULT_I18N_TEXT.js +497 -34
- package/dist/theme/assets/markdown.js +22 -0
- package/dist/theme/components/Llms/LlmsOpenRow.js +11 -2
- package/dist/theme/components/Llms/LlmsViewOptions.d.ts +4 -2
- package/dist/theme/components/Llms/LlmsViewOptions.js +14 -2
- package/dist/theme/components/PageTabs/index.js +10 -14
- package/dist/theme/components/Prompt/index.js +29 -1
- package/dist/theme/components/Search/useSearchPanel.js +66 -65
- package/dist/theme/components/Sidebar/SidebarItem.css +9 -11
- package/dist/theme/components/Sidebar/SidebarItem.js +2 -2
- package/dist/theme/hooks/useDynamicToc.js +21 -9
- package/dist/theme/icons.d.ts +5 -0
- package/dist/theme/icons.js +3 -1
- package/dist/theme/logic/useStorageValue.d.ts +2 -1
- package/dist/theme/logic/useStorageValue.js +51 -34
- package/dist/y~0.js +1 -1
- package/package.json +5 -5
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
IconAnthropic,
|
|
5
5
|
IconExternalLink,
|
|
6
6
|
IconLink,
|
|
7
|
+
IconMarkdown,
|
|
7
8
|
IconOpenAI,
|
|
8
9
|
IconOpenInChat,
|
|
9
10
|
SvgWrapper,
|
|
@@ -21,7 +22,12 @@ type MenuItem = {
|
|
|
21
22
|
onClick?: () => void;
|
|
22
23
|
};
|
|
23
24
|
|
|
24
|
-
const DEFAULT_OPTIONS = [
|
|
25
|
+
const DEFAULT_OPTIONS = [
|
|
26
|
+
'markdownLink',
|
|
27
|
+
'viewMarkdown',
|
|
28
|
+
'chatgpt',
|
|
29
|
+
'claude',
|
|
30
|
+
] as const;
|
|
25
31
|
|
|
26
32
|
export function LlmsOpenRow() {
|
|
27
33
|
const t = useI18n();
|
|
@@ -73,6 +79,11 @@ export function LlmsOpenRow() {
|
|
|
73
79
|
void copyToClipboard(fullMarkdownUrl);
|
|
74
80
|
},
|
|
75
81
|
},
|
|
82
|
+
viewMarkdown: {
|
|
83
|
+
title: t('viewMarkdownText'),
|
|
84
|
+
href: pathname,
|
|
85
|
+
icon: <SvgWrapper icon={IconMarkdown} />,
|
|
86
|
+
},
|
|
76
87
|
chatgpt: {
|
|
77
88
|
title: t('openInText', { name: 'ChatGPT' }),
|
|
78
89
|
href: `https://chatgpt.com/?${new URLSearchParams({ hints: 'search', q })}`,
|
|
@@ -84,7 +95,7 @@ export function LlmsOpenRow() {
|
|
|
84
95
|
icon: <SvgWrapper icon={IconAnthropic} />,
|
|
85
96
|
},
|
|
86
97
|
}),
|
|
87
|
-
[fullMarkdownUrl, t, q],
|
|
98
|
+
[fullMarkdownUrl, pathname, t, q],
|
|
88
99
|
);
|
|
89
100
|
|
|
90
101
|
const renderableItems = useMemo(
|
|
@@ -105,7 +116,7 @@ export function LlmsOpenRow() {
|
|
|
105
116
|
<div ref={containerRef} className="rp-outline__open-in-wrapper">
|
|
106
117
|
<button
|
|
107
118
|
className="rp-outline__action-row"
|
|
108
|
-
onClick={() => setIsOpen(!
|
|
119
|
+
onClick={() => setIsOpen(prevOpen => !prevOpen)}
|
|
109
120
|
>
|
|
110
121
|
<SvgWrapper icon={IconOpenInChat} />
|
|
111
122
|
<span>{t('openInText', { name: 'chat' })}</span>
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
IconDown,
|
|
11
11
|
IconExternalLink,
|
|
12
12
|
IconLink,
|
|
13
|
+
IconMarkdown,
|
|
13
14
|
IconOpenAI,
|
|
14
15
|
SvgWrapper,
|
|
15
16
|
} from '@rspress/core/theme';
|
|
@@ -31,6 +32,7 @@ export type LlmsViewOptionsItem =
|
|
|
31
32
|
icon?: React.ReactNode;
|
|
32
33
|
}
|
|
33
34
|
| 'markdownLink'
|
|
35
|
+
| 'viewMarkdown'
|
|
34
36
|
| 'chatgpt'
|
|
35
37
|
| 'claude';
|
|
36
38
|
|
|
@@ -44,7 +46,9 @@ type MenuItem = {
|
|
|
44
46
|
export interface LlmsViewOptionsProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
45
47
|
/**
|
|
46
48
|
* Default options for the dropdown.
|
|
47
|
-
* @default ['markdownLink', 'chatgpt', 'claude']
|
|
49
|
+
* @default ['markdownLink', 'viewMarkdown', 'chatgpt', 'claude']
|
|
50
|
+
* - 'markdownLink': Copy Markdown link
|
|
51
|
+
* - 'viewMarkdown': View as Markdown
|
|
48
52
|
* - 'chatgpt': Open in ChatGPT
|
|
49
53
|
* - 'claude': Open in Claude
|
|
50
54
|
*
|
|
@@ -67,6 +71,7 @@ export interface LlmsViewOptionsProps extends React.ButtonHTMLAttributes<HTMLBut
|
|
|
67
71
|
|
|
68
72
|
const DEFAULT_OPTIONS: LlmsViewOptionsItem[] = [
|
|
69
73
|
'markdownLink',
|
|
74
|
+
'viewMarkdown',
|
|
70
75
|
'chatgpt',
|
|
71
76
|
'claude',
|
|
72
77
|
];
|
|
@@ -101,7 +106,7 @@ export function LlmsViewOptions({
|
|
|
101
106
|
}, []);
|
|
102
107
|
|
|
103
108
|
const toggleDropdown = () => {
|
|
104
|
-
setIsOpen(!
|
|
109
|
+
setIsOpen(prevOpen => !prevOpen);
|
|
105
110
|
};
|
|
106
111
|
|
|
107
112
|
const { pathname } = useMdUrl();
|
|
@@ -122,6 +127,11 @@ export function LlmsViewOptions({
|
|
|
122
127
|
void copyToClipboard(fullMarkdownUrl);
|
|
123
128
|
},
|
|
124
129
|
},
|
|
130
|
+
viewMarkdown: {
|
|
131
|
+
title: t('viewMarkdownText'),
|
|
132
|
+
href: pathname,
|
|
133
|
+
icon: <SvgWrapper icon={IconMarkdown} />,
|
|
134
|
+
},
|
|
125
135
|
chatgpt: {
|
|
126
136
|
title: t('openInText', { name: 'ChatGPT' }),
|
|
127
137
|
href: `https://chatgpt.com/?${new URLSearchParams({
|
|
@@ -147,6 +157,9 @@ export function LlmsViewOptions({
|
|
|
147
157
|
if (option === 'markdownLink') {
|
|
148
158
|
return items.markdownLink;
|
|
149
159
|
}
|
|
160
|
+
if (option === 'viewMarkdown') {
|
|
161
|
+
return items.viewMarkdown;
|
|
162
|
+
}
|
|
150
163
|
if (option === 'chatgpt') {
|
|
151
164
|
return items.chatgpt;
|
|
152
165
|
}
|
|
@@ -176,6 +189,9 @@ export function LlmsViewOptions({
|
|
|
176
189
|
<button
|
|
177
190
|
ref={dropdownRef}
|
|
178
191
|
onClick={toggleDropdown}
|
|
192
|
+
aria-label="View options"
|
|
193
|
+
aria-expanded={isOpen}
|
|
194
|
+
aria-haspopup="true"
|
|
179
195
|
className={`rp-llms-button rp-llms-view-options__trigger ${
|
|
180
196
|
isOpen ? 'rp-llms-view-options__trigger--active' : ''
|
|
181
197
|
}`}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useSearchParams } from '@rspress/core/runtime';
|
|
1
|
+
import { useLocation, useSearchParams } from '@rspress/core/runtime';
|
|
2
2
|
import clsx from 'clsx';
|
|
3
3
|
import {
|
|
4
4
|
Children,
|
|
@@ -52,26 +52,28 @@ export interface PageTabsProps {
|
|
|
52
52
|
|
|
53
53
|
function usePageTabs(id: string, children: ReactElement<PageTabProps>[]) {
|
|
54
54
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
55
|
+
const { search } = useLocation();
|
|
55
56
|
|
|
56
57
|
const tabValues = useMemo(() => {
|
|
57
58
|
return getTabValuesFromChildren(children);
|
|
58
59
|
}, [children]);
|
|
59
60
|
|
|
60
61
|
function navigateToTab(index: number) {
|
|
62
|
+
const nextSearchParams = new URLSearchParams(searchParams);
|
|
61
63
|
if (index === 0) {
|
|
62
|
-
|
|
64
|
+
nextSearchParams.delete(id);
|
|
63
65
|
} else {
|
|
64
|
-
|
|
66
|
+
nextSearchParams.set(id, String(index));
|
|
65
67
|
}
|
|
66
|
-
setSearchParams(
|
|
68
|
+
setSearchParams(nextSearchParams);
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
useEffect(() => {
|
|
70
|
-
const currIndex = Number(
|
|
72
|
+
const currIndex = Number(new URLSearchParams(search).get(id) ?? '0');
|
|
71
73
|
if (!Number.isNaN(currIndex)) {
|
|
72
74
|
document.body.dataset.pageTabsActiveIndex = currIndex.toString();
|
|
73
75
|
}
|
|
74
|
-
}, [
|
|
76
|
+
}, [id, search]);
|
|
75
77
|
|
|
76
78
|
const injectScript = `(function () {
|
|
77
79
|
var searchParams = new URLSearchParams(window.location.search);
|
|
@@ -88,8 +90,6 @@ function usePageTabs(id: string, children: ReactElement<PageTabProps>[]) {
|
|
|
88
90
|
};
|
|
89
91
|
}
|
|
90
92
|
|
|
91
|
-
let renderCountForTocUpdate = 0;
|
|
92
|
-
|
|
93
93
|
export const PageTabs = forwardRef(
|
|
94
94
|
(props: PageTabsProps, ref: ForwardedRef<HTMLDivElement>): ReactElement => {
|
|
95
95
|
const {
|
|
@@ -111,8 +111,6 @@ export const PageTabs = forwardRef(
|
|
|
111
111
|
children,
|
|
112
112
|
);
|
|
113
113
|
|
|
114
|
-
renderCountForTocUpdate++;
|
|
115
|
-
|
|
116
114
|
return (
|
|
117
115
|
<>
|
|
118
116
|
{/* First screen during SSR */}
|
|
@@ -159,14 +157,6 @@ export const PageTabs = forwardRef(
|
|
|
159
157
|
})}
|
|
160
158
|
</div>
|
|
161
159
|
</div>
|
|
162
|
-
{/*
|
|
163
|
-
Render a hidden <h2> element to trigger a Table of Contents (TOC) update.
|
|
164
|
-
This mechanism ensures that the TOC stays in sync with tab content changes,
|
|
165
|
-
as some TOC generators rely on heading elements being present in the DOM.
|
|
166
|
-
*/}
|
|
167
|
-
{renderCountForTocUpdate % 2 === 0 ? (
|
|
168
|
-
<h2 style={{ display: 'none' }} />
|
|
169
|
-
) : null}
|
|
170
160
|
</>
|
|
171
161
|
);
|
|
172
162
|
},
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, rs } from '@rstest/core';
|
|
2
|
+
import { renderToMarkdownString } from 'react-render-to-markdown';
|
|
3
|
+
import { renderInlineMarkdown } from '../../logic/utils';
|
|
4
|
+
import { Prompt } from './index';
|
|
5
|
+
|
|
6
|
+
rs.mock('@rspress/core/runtime', () => ({
|
|
7
|
+
useI18n: () => (key: string) => key,
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
rs.mock('@rspress/core/theme', () => ({ renderInlineMarkdown }));
|
|
11
|
+
|
|
12
|
+
const originalSsgMd = import.meta.env.SSG_MD;
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
if (originalSsgMd === undefined) {
|
|
16
|
+
delete (import.meta.env as unknown as Record<string, unknown>).SSG_MD;
|
|
17
|
+
} else {
|
|
18
|
+
(import.meta.env as unknown as Record<string, unknown>).SSG_MD =
|
|
19
|
+
originalSsgMd;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('Prompt Markdown rendering', () => {
|
|
24
|
+
it('preserves prompt text in a code block and omits interactive controls', async () => {
|
|
25
|
+
(import.meta.env as unknown as Record<string, unknown>).SSG_MD = true;
|
|
26
|
+
const prompt =
|
|
27
|
+
'Follow https://rspress.rs/guide/start/getting-started.md.\n\n1. Run `create rspress@latest`.\n2. Start the dev server.\n\n```sh\npnpm dev\n```';
|
|
28
|
+
const markdown = await renderToMarkdownString(
|
|
29
|
+
<Prompt
|
|
30
|
+
title="Start a Rspress project"
|
|
31
|
+
description="Send this to your **AI agent**."
|
|
32
|
+
prompt={prompt}
|
|
33
|
+
/>,
|
|
34
|
+
);
|
|
35
|
+
expect(markdown).toMatchInlineSnapshot(`
|
|
36
|
+
"**Start a Rspress project**
|
|
37
|
+
|
|
38
|
+
Send this to your **AI agent**.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
\`\`\`\`
|
|
42
|
+
<prompt>
|
|
43
|
+
Follow https://rspress.rs/guide/start/getting-started.md.
|
|
44
|
+
|
|
45
|
+
1. Run \`create rspress@latest\`.
|
|
46
|
+
2. Start the dev server.
|
|
47
|
+
|
|
48
|
+
\`\`\`sh
|
|
49
|
+
pnpm dev
|
|
50
|
+
\`\`\`
|
|
51
|
+
</prompt>
|
|
52
|
+
\`\`\`\`
|
|
53
|
+
"
|
|
54
|
+
`);
|
|
55
|
+
expect(markdown).toContain(`<prompt>\n${prompt}\n</prompt>`);
|
|
56
|
+
expect(markdown).toContain('Send this to your **AI agent**.');
|
|
57
|
+
expect(markdown).not.toContain('promptCopyText');
|
|
58
|
+
expect(markdown).not.toContain('For your Agent');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('renders custom children as Markdown without a code block', async () => {
|
|
62
|
+
(import.meta.env as unknown as Record<string, unknown>).SSG_MD = true;
|
|
63
|
+
expect(
|
|
64
|
+
await renderToMarkdownString(
|
|
65
|
+
<Prompt custom eyebrow="Agent resources">
|
|
66
|
+
<h3>Get started</h3>
|
|
67
|
+
<p>
|
|
68
|
+
<a href="https://rspress.rs">Read the guide</a>
|
|
69
|
+
</p>
|
|
70
|
+
</Prompt>,
|
|
71
|
+
),
|
|
72
|
+
).toMatchInlineSnapshot(`
|
|
73
|
+
"**Agent resources**
|
|
74
|
+
|
|
75
|
+
### Get started
|
|
76
|
+
|
|
77
|
+
[Read the guide](https://rspress.rs)
|
|
78
|
+
|
|
79
|
+
"
|
|
80
|
+
`);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('uses the default title and omits absent content', async () => {
|
|
84
|
+
(import.meta.env as unknown as Record<string, unknown>).SSG_MD = true;
|
|
85
|
+
expect(await renderToMarkdownString(<Prompt />)).toMatchInlineSnapshot(`
|
|
86
|
+
"**Agent Prompt**
|
|
87
|
+
|
|
88
|
+
"
|
|
89
|
+
`);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -329,6 +329,37 @@ function CopyablePrompt({
|
|
|
329
329
|
}
|
|
330
330
|
|
|
331
331
|
export function Prompt({ custom = false, ...props }: PromptProps) {
|
|
332
|
+
if (import.meta.env.SSG_MD) {
|
|
333
|
+
const {
|
|
334
|
+
children,
|
|
335
|
+
description,
|
|
336
|
+
eyebrow = 'For your Agent',
|
|
337
|
+
prompt,
|
|
338
|
+
title = 'Agent Prompt',
|
|
339
|
+
} = props;
|
|
340
|
+
|
|
341
|
+
if (custom) {
|
|
342
|
+
return (
|
|
343
|
+
<>
|
|
344
|
+
<p>
|
|
345
|
+
<strong>{eyebrow}</strong>
|
|
346
|
+
</p>
|
|
347
|
+
{children}
|
|
348
|
+
</>
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return (
|
|
353
|
+
<>
|
|
354
|
+
<p>
|
|
355
|
+
<strong>{title}</strong>
|
|
356
|
+
</p>
|
|
357
|
+
{description ? <p>{description}</p> : null}
|
|
358
|
+
{prompt ? <pre>{`<prompt>\n${prompt}\n</prompt>`}</pre> : null}
|
|
359
|
+
</>
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
|
|
332
363
|
return custom ? <CustomPrompt {...props} /> : <CopyablePrompt {...props} />;
|
|
333
364
|
}
|
|
334
365
|
|
|
@@ -4,6 +4,8 @@ import { debounce } from '@rspress/shared/lodash-es';
|
|
|
4
4
|
import {
|
|
5
5
|
useCallback,
|
|
6
6
|
useEffect,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useMemo,
|
|
7
9
|
useRef,
|
|
8
10
|
useState,
|
|
9
11
|
type RefObject,
|
|
@@ -25,18 +27,6 @@ import {
|
|
|
25
27
|
toWebMcpSearchResults,
|
|
26
28
|
} from '../../webMcp/tools';
|
|
27
29
|
|
|
28
|
-
const useDebounce = <T extends (...args: any[]) => any>(cb: T) => {
|
|
29
|
-
const cbRef = useRef(cb);
|
|
30
|
-
cbRef.current = cb;
|
|
31
|
-
return useCallback(
|
|
32
|
-
debounce(
|
|
33
|
-
((...args: Parameters<T>): ReturnType<T> => cbRef.current(...args)) as T,
|
|
34
|
-
150,
|
|
35
|
-
),
|
|
36
|
-
[],
|
|
37
|
-
);
|
|
38
|
-
};
|
|
39
|
-
|
|
40
30
|
export function useSearchPanel({
|
|
41
31
|
focused,
|
|
42
32
|
searchInputRef,
|
|
@@ -67,9 +57,10 @@ export function useSearchPanel({
|
|
|
67
57
|
>(undefined);
|
|
68
58
|
const searchEnabled = search !== false;
|
|
69
59
|
const searchOptions: LocalSearchOptions = search === false ? {} : search;
|
|
70
|
-
const
|
|
60
|
+
const isSearchVersioned = searchEnabled && (search?.versioned ?? true);
|
|
61
|
+
const searchVersion = isSearchVersioned ? version : '';
|
|
71
62
|
const searchIndexURL = searchEnabled
|
|
72
|
-
? getSearchIndexURL(lang,
|
|
63
|
+
? getSearchIndexURL(lang, searchVersion)
|
|
73
64
|
: undefined;
|
|
74
65
|
const DEFAULT_RESULT = [
|
|
75
66
|
{ group: siteTitle, result: [], renderType: RenderType.Default },
|
|
@@ -80,7 +71,7 @@ export function useSearchPanel({
|
|
|
80
71
|
if (
|
|
81
72
|
pageSearcherRef.current &&
|
|
82
73
|
previous?.currentLang === lang &&
|
|
83
|
-
(!
|
|
74
|
+
(!isSearchVersioned || previous.currentVersion === version) &&
|
|
84
75
|
pageSearcherURLRef.current === searchIndexURL
|
|
85
76
|
) {
|
|
86
77
|
return pageSearcherRef.current;
|
|
@@ -162,88 +153,76 @@ export function useSearchPanel({
|
|
|
162
153
|
});
|
|
163
154
|
}
|
|
164
155
|
|
|
156
|
+
const requestIdRef = useRef(0);
|
|
157
|
+
const scheduleQuery = useMemo(
|
|
158
|
+
() => debounce((run: () => void) => run(), 150),
|
|
159
|
+
[],
|
|
160
|
+
);
|
|
161
|
+
const invalidateQuery = useCallback(() => {
|
|
162
|
+
requestIdRef.current++;
|
|
163
|
+
scheduleQuery.cancel();
|
|
164
|
+
}, [scheduleQuery]);
|
|
165
|
+
|
|
166
|
+
// A query belongs to the popup session and index that started it.
|
|
167
|
+
useLayoutEffect(
|
|
168
|
+
() => invalidateQuery,
|
|
169
|
+
[
|
|
170
|
+
focused,
|
|
171
|
+
searchEnabled,
|
|
172
|
+
lang,
|
|
173
|
+
searchVersion,
|
|
174
|
+
searchIndexURL,
|
|
175
|
+
invalidateQuery,
|
|
176
|
+
],
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
useLayoutEffect(() => {
|
|
180
|
+
pageSearcherRef.current = null;
|
|
181
|
+
setInitStatus('initial');
|
|
182
|
+
setSearchError(undefined);
|
|
183
|
+
}, [searchEnabled, lang, searchVersion, searchIndexURL]);
|
|
184
|
+
|
|
165
185
|
const initSearch = async () => {
|
|
166
|
-
|
|
186
|
+
const searcher = createSearcher();
|
|
187
|
+
if (
|
|
188
|
+
initStatus !== 'initial' &&
|
|
189
|
+
initializationRef.current?.searcher === searcher
|
|
190
|
+
) {
|
|
167
191
|
return;
|
|
168
192
|
}
|
|
169
|
-
const searcher = createSearcher();
|
|
170
193
|
setSearchError(undefined);
|
|
171
194
|
setInitStatus('initing');
|
|
172
195
|
try {
|
|
173
196
|
await initializeSearcher(searcher);
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
return;
|
|
197
|
+
if (pageSearcherRef.current === searcher) {
|
|
198
|
+
setInitStatus('inited');
|
|
177
199
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
if (pageSearcherRef.current !== searcher) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
setInitStatus('inited');
|
|
187
|
-
const currentQuery = searchInputRef.current?.value;
|
|
188
|
-
if (currentQuery) {
|
|
189
|
-
const matched = await searcher.match(currentQuery);
|
|
190
|
-
if (
|
|
191
|
-
pageSearcherRef.current !== searcher ||
|
|
192
|
-
searchInputRef.current?.value !== currentQuery
|
|
193
|
-
) {
|
|
194
|
-
return;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (pageSearcherRef.current === searcher) {
|
|
202
|
+
setSearchError(error instanceof Error ? error.message : String(error));
|
|
203
|
+
setInitStatus('error');
|
|
195
204
|
}
|
|
196
|
-
setSearchResult(matched || DEFAULT_RESULT);
|
|
197
|
-
setIsSearching(false);
|
|
198
205
|
}
|
|
199
206
|
};
|
|
200
207
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (
|
|
206
|
-
setSearchResult(DEFAULT_RESULT);
|
|
207
|
-
initSearch();
|
|
208
|
-
} else {
|
|
209
|
-
setQuery('');
|
|
210
|
-
}
|
|
211
|
-
}, [focused, searchEnabled]);
|
|
212
|
-
|
|
213
|
-
useEffect(() => {
|
|
214
|
-
if (!searchEnabled) {
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
const { currentLang, currentVersion } = pageSearcherConfigRef.current ?? {};
|
|
218
|
-
const isLangChanged = lang !== currentLang;
|
|
219
|
-
const isVersionChanged = versionedSearch && version !== currentVersion;
|
|
220
|
-
const isIndexChanged = pageSearcherURLRef.current !== searchIndexURL;
|
|
221
|
-
if (isLangChanged || isVersionChanged || isIndexChanged) {
|
|
222
|
-
setInitStatus('initial');
|
|
223
|
-
setSearchError(undefined);
|
|
224
|
-
setIsSearching(false);
|
|
225
|
-
pageSearcherRef.current = null;
|
|
226
|
-
}
|
|
227
|
-
}, [lang, version, versionedSearch, searchEnabled, searchIndexURL]);
|
|
208
|
+
const runQuery = async (value: string, requestId: number) => {
|
|
209
|
+
const isCurrent = () =>
|
|
210
|
+
requestId === requestIdRef.current &&
|
|
211
|
+
searchInputRef.current?.value === value;
|
|
212
|
+
if (!isCurrent()) return;
|
|
228
213
|
|
|
229
|
-
|
|
230
|
-
const newQuery = value;
|
|
231
|
-
setQuery(newQuery);
|
|
232
|
-
if (!newQuery) {
|
|
233
|
-
setIsSearching(false);
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
214
|
+
setQuery(value);
|
|
236
215
|
try {
|
|
237
216
|
const result = await searchDocs(value);
|
|
238
|
-
if (
|
|
217
|
+
if (isCurrent()) {
|
|
239
218
|
setSearchError(undefined);
|
|
240
219
|
setInitStatus('inited');
|
|
241
220
|
setCurrentSuggestionIndex(0);
|
|
242
|
-
setSearchResult(result
|
|
221
|
+
setSearchResult(result);
|
|
243
222
|
setIsSearching(false);
|
|
244
223
|
}
|
|
245
224
|
} catch (error) {
|
|
246
|
-
if (
|
|
225
|
+
if (isCurrent()) {
|
|
247
226
|
setSearchError(error instanceof Error ? error.message : String(error));
|
|
248
227
|
setInitStatus('error');
|
|
249
228
|
setIsSearching(false);
|
|
@@ -251,16 +230,41 @@ export function useSearchPanel({
|
|
|
251
230
|
}
|
|
252
231
|
};
|
|
253
232
|
|
|
254
|
-
const handleQueryChange = useDebounce(handleQueryChangedImpl);
|
|
255
|
-
const handleQueryInput = (value: string) => {
|
|
256
|
-
setIsSearching(Boolean(value));
|
|
257
|
-
handleQueryChange(value);
|
|
258
|
-
};
|
|
259
233
|
const clearQuery = () => {
|
|
234
|
+
invalidateQuery();
|
|
260
235
|
setQuery('');
|
|
261
236
|
setIsSearching(false);
|
|
262
237
|
};
|
|
263
238
|
|
|
239
|
+
const handleQueryInput = (value: string) => {
|
|
240
|
+
if (!value) {
|
|
241
|
+
clearQuery();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
// Assign the ID before debounce so a previous request cannot publish while
|
|
245
|
+
// the latest input is waiting to start, even if the text is identical.
|
|
246
|
+
const requestId = ++requestIdRef.current;
|
|
247
|
+
setIsSearching(true);
|
|
248
|
+
scheduleQuery(() => {
|
|
249
|
+
void runQuery(value, requestId);
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
useEffect(() => {
|
|
254
|
+
if (!focused || !searchEnabled) {
|
|
255
|
+
clearQuery();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
setSearchResult(DEFAULT_RESULT);
|
|
260
|
+
const value = searchInputRef.current?.value;
|
|
261
|
+
if (value) {
|
|
262
|
+
handleQueryInput(value);
|
|
263
|
+
} else {
|
|
264
|
+
void initSearch();
|
|
265
|
+
}
|
|
266
|
+
}, [focused, searchEnabled, lang, searchVersion, searchIndexURL]);
|
|
267
|
+
|
|
264
268
|
return {
|
|
265
269
|
searchEnabled,
|
|
266
270
|
clearQuery,
|
|
@@ -40,18 +40,22 @@
|
|
|
40
40
|
&--active {
|
|
41
41
|
color: var(--rp-c-link);
|
|
42
42
|
background-color: var(--rp-c-brand-tint);
|
|
43
|
-
font-weight: 600;
|
|
44
43
|
&:hover {
|
|
45
44
|
color: var(--rp-c-link);
|
|
46
45
|
background-color: var(--rp-c-brand-tint);
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
|
|
49
|
+
&:focus-visible {
|
|
50
|
+
outline: 2px solid var(--rp-c-brand);
|
|
51
|
+
outline-offset: -2px;
|
|
52
|
+
}
|
|
53
|
+
|
|
50
54
|
&--group-item::before {
|
|
51
55
|
content: '';
|
|
52
56
|
position: absolute;
|
|
53
|
-
top:
|
|
54
|
-
left:
|
|
57
|
+
top: -2px;
|
|
58
|
+
left: -7px;
|
|
55
59
|
bottom: 0;
|
|
56
60
|
width: 1px;
|
|
57
61
|
background-color: var(--rp-c-divider-light);
|
|
@@ -61,10 +65,9 @@
|
|
|
61
65
|
.rp-sidebar-item__left::before {
|
|
62
66
|
content: '';
|
|
63
67
|
position: absolute;
|
|
64
|
-
top:
|
|
65
|
-
bottom:
|
|
66
|
-
left:
|
|
67
|
-
height: calc(50% - 4px);
|
|
68
|
+
top: 8px;
|
|
69
|
+
bottom: 8px;
|
|
70
|
+
left: -7px;
|
|
68
71
|
width: 1px;
|
|
69
72
|
background-color: var(--rp-c-brand);
|
|
70
73
|
}
|
|
@@ -88,7 +88,7 @@ export function SidebarItemRaw({
|
|
|
88
88
|
className,
|
|
89
89
|
)}
|
|
90
90
|
style={{
|
|
91
|
-
|
|
91
|
+
marginLeft: depth * 24,
|
|
92
92
|
}}
|
|
93
93
|
{...{ 'data-depth': depth }}
|
|
94
94
|
{...(context ? { 'data-context': context } : {})}
|
|
@@ -110,7 +110,7 @@ export function SidebarItemRaw({
|
|
|
110
110
|
className,
|
|
111
111
|
)}
|
|
112
112
|
style={{
|
|
113
|
-
|
|
113
|
+
marginLeft: depth * 24,
|
|
114
114
|
}}
|
|
115
115
|
{...{ 'data-depth': depth }}
|
|
116
116
|
{...(context ? { 'data-context': context } : {})}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { setImmediate } from 'node:timers/promises';
|
|
1
2
|
import { afterEach, describe, expect, it, rs } from '@rstest/core';
|
|
3
|
+
import { Component, type ReactNode } from 'react';
|
|
2
4
|
import { renderToMarkdownString } from 'react-render-to-markdown';
|
|
5
|
+
import { useStorageValue } from '../../logic/useStorageValue';
|
|
3
6
|
import { Tab, Tabs } from './index';
|
|
4
7
|
|
|
5
|
-
rs.mock('@rspress/core/theme', () => ({
|
|
6
|
-
useStorageValue: () => ['0', () => {}],
|
|
7
|
-
}));
|
|
8
|
+
rs.mock('@rspress/core/theme', () => ({ useStorageValue }));
|
|
8
9
|
|
|
9
10
|
const originalSsgMd = import.meta.env.SSG_MD;
|
|
10
11
|
|
|
@@ -24,9 +25,27 @@ afterEach(() => {
|
|
|
24
25
|
describe('Tabs', () => {
|
|
25
26
|
it('renders every tab as markdown when SSG_MD is enabled', async () => {
|
|
26
27
|
setSsgMd(true);
|
|
28
|
+
expect(typeof window).toBe('undefined');
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
const errors: Error[] = [];
|
|
31
|
+
class ErrorBoundary extends Component<{ children: ReactNode }> {
|
|
32
|
+
state = { hasError: false };
|
|
33
|
+
|
|
34
|
+
static getDerivedStateFromError() {
|
|
35
|
+
return { hasError: true };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
componentDidCatch(error: Error) {
|
|
39
|
+
errors.push(error);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
render() {
|
|
43
|
+
return this.state.hasError ? null : this.props.children;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const markdown = await renderToMarkdownString(
|
|
48
|
+
<ErrorBoundary>
|
|
30
49
|
<Tabs>
|
|
31
50
|
<Tab label="npm" value="npm">
|
|
32
51
|
npm install
|
|
@@ -34,9 +53,13 @@ describe('Tabs', () => {
|
|
|
34
53
|
<Tab label="pnpm" value="pnpm">
|
|
35
54
|
pnpm install
|
|
36
55
|
</Tab>
|
|
37
|
-
</Tabs
|
|
38
|
-
|
|
39
|
-
)
|
|
56
|
+
</Tabs>
|
|
57
|
+
</ErrorBoundary>,
|
|
58
|
+
);
|
|
59
|
+
// Storage subscriptions run after the Markdown renderer resolves its commit.
|
|
60
|
+
await setImmediate();
|
|
61
|
+
expect(errors).toEqual([]);
|
|
62
|
+
expect(markdown).toMatchInlineSnapshot(`
|
|
40
63
|
"
|
|
41
64
|
**npm**
|
|
42
65
|
|