@rspress/core 0.0.13 → 0.2.0

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 (39) hide show
  1. package/dist/index.js +90 -50
  2. package/dist/index.js.map +1 -1
  3. package/dist/loader.d.ts +3 -0
  4. package/dist/loader.js +28 -72
  5. package/dist/loader.js.map +1 -1
  6. package/dist/runtime/App.d.ts +8 -0
  7. package/dist/runtime/App.js +1 -0
  8. package/dist/runtime/Content.d.ts +6 -0
  9. package/dist/runtime/Content.js +7 -2
  10. package/dist/runtime/NoSSR.d.ts +4 -0
  11. package/dist/runtime/clientEntry.d.ts +1 -0
  12. package/dist/runtime/hooks.d.ts +29 -0
  13. package/dist/runtime/hooks.js +23 -1
  14. package/dist/runtime/index.d.ts +6 -0
  15. package/dist/runtime/ssrEntry.d.ts +6 -0
  16. package/dist/runtime/tsconfig.json +9 -0
  17. package/dist/runtime/utils.d.ts +6 -0
  18. package/dist/theme/bundle.css +94 -53
  19. package/dist/theme/bundle.d.ts +12 -1
  20. package/dist/theme/bundle.js +527 -489
  21. package/dist/theme/bundle.js.map +1 -1
  22. package/package.json +14 -14
  23. package/src/runtime/App.tsx +1 -0
  24. package/src/runtime/Content.tsx +11 -2
  25. package/src/runtime/global.d.ts +49 -0
  26. package/src/runtime/hooks.ts +47 -1
  27. package/src/runtime/tsconfig.json +9 -0
  28. package/src/theme-default/components/LocalSideBar/{index.module.scss → index.scss} +6 -5
  29. package/src/theme-default/components/LocalSideBar/index.tsx +4 -4
  30. package/src/theme-default/components/Nav/index.tsx +19 -5
  31. package/src/theme-default/components/Search/SearchPanel.tsx +339 -0
  32. package/src/theme-default/components/Search/index.tsx +6 -326
  33. package/src/theme-default/index.ts +3 -1
  34. package/src/theme-default/layout/DocLayout/docComponents/link.tsx +8 -23
  35. package/src/theme-default/layout/DocLayout/index.module.scss +52 -3
  36. package/src/theme-default/layout/DocLayout/index.tsx +9 -7
  37. package/src/theme-default/logic/index.ts +1 -0
  38. package/src/theme-default/logic/useAppearance.ts +0 -1
  39. package/src/theme-default/logic/usePathUtils.ts +72 -0
@@ -1,265 +1,10 @@
1
- import { createPortal } from 'react-dom';
2
- import { useEffect, useRef, useState } from 'react';
3
- import { groupBy, debounce } from 'lodash-es';
4
- import { onSearch } from 'virtual-search-hooks';
5
- import { SearchOptions } from '@rspress/shared';
6
- import { getSidebarGroupData } from '../../logic/useSidebarData';
7
- import { useLocaleSiteData } from '../../logic/useLocaleSiteData';
8
- import { Tabs, Tab } from '../Tabs';
9
- import styles from './index.module.scss';
1
+ import { useState } from 'react';
10
2
  import SearchSvg from './assets/search.svg';
11
- import LoadingSvg from './assets/loading.svg';
12
- import CloseSvg from './assets/close.svg';
13
- import { MatchResult, MatchResultItem, PageSearcher } from './logic/search';
14
- import { SuggestItem } from './SuggestItem';
15
- import { normalizeSearchIndexes, removeDomain } from './logic/util';
16
- import { NoSearchResult } from './NoSearchResult';
17
- import { isProduction, usePageData } from '@/runtime';
18
-
19
- const KEY_CODE = {
20
- ARROW_UP: 'ArrowUp',
21
- ARROW_DOWN: 'ArrowDown',
22
- ENTER: 'Enter',
23
- SEARCH: 'KeyK',
24
- ESC: 'Escape',
25
- };
26
-
27
- const RECOMMEND_WORD: Record<string, string> = {
28
- zh: '其它站点推荐结果',
29
- en: 'Other Site Search Results',
30
- };
3
+ import styles from './index.module.scss';
4
+ import { SearchPanel } from './SearchPanel';
31
5
 
32
6
  export function Search() {
33
- const [query, setQuery] = useState('');
34
- const [searchResult, setSearchResult] = useState<MatchResult>({
35
- current: [],
36
- others: [],
37
- });
38
7
  const [focused, setFocused] = useState(false);
39
- const searchInputRef = useRef<HTMLInputElement | null>(null);
40
- const [initing, setIniting] = useState(true);
41
- const [currentSuggestionIndex, setCurrentSuggestionIndex] = useState(0);
42
- const pageSearcherRef = useRef<PageSearcher | null>(null);
43
- const {
44
- siteData,
45
- page: { lang },
46
- } = usePageData();
47
- const { sidebar } = useLocaleSiteData();
48
- const { search } = siteData;
49
-
50
- const suggestions = [
51
- ...searchResult.current,
52
- ...searchResult.others.map(item => item.items),
53
- ].flat();
54
-
55
- // We need to extract the group name by the link so that we can divide the search result into different groups.
56
- const extractGroupName = (link: string) =>
57
- getSidebarGroupData(sidebar, link).group;
58
-
59
- async function initPageSearcher() {
60
- if (search === false) {
61
- return;
62
- }
63
- const pageSearcher = new PageSearcher({
64
- ...search,
65
- currentLang: lang,
66
- extractGroupName,
67
- });
68
- pageSearcherRef.current = pageSearcher;
69
- await pageSearcherRef.current.init();
70
- setIniting(false);
71
- const query = searchInputRef.current?.value;
72
- if (query) {
73
- const matched = await pageSearcherRef.current?.match(query);
74
- setSearchResult(matched || { current: [], others: [] });
75
- }
76
- }
77
-
78
- useEffect(() => {
79
- const onKeyDown = (e: KeyboardEvent) => {
80
- switch (e.code) {
81
- case KEY_CODE.SEARCH:
82
- if (e.ctrlKey || e.metaKey) {
83
- e.preventDefault();
84
- setFocused(!focused);
85
- }
86
- break;
87
- case KEY_CODE.ARROW_DOWN:
88
- if (focused) {
89
- e.preventDefault();
90
- setCurrentSuggestionIndex(
91
- (currentSuggestionIndex + 1) % suggestions.length,
92
- );
93
- }
94
- break;
95
- case KEY_CODE.ARROW_UP:
96
- if (focused) {
97
- e.preventDefault();
98
- setCurrentSuggestionIndex(
99
- (currentSuggestionIndex - 1 + suggestions.length) %
100
- suggestions.length,
101
- );
102
- }
103
- break;
104
- case KEY_CODE.ENTER:
105
- if (currentSuggestionIndex >= 0) {
106
- const suggestion = suggestions[currentSuggestionIndex];
107
- const isCurrent =
108
- currentSuggestionIndex < searchResult.current.length;
109
- if (isCurrent) {
110
- window.location.href = isProduction()
111
- ? suggestion.link
112
- : removeDomain(suggestion.link);
113
- setFocused(false);
114
- } else {
115
- window.open(suggestion.link);
116
- }
117
- }
118
- break;
119
- case KEY_CODE.ESC:
120
- setFocused(false);
121
- break;
122
- default:
123
- break;
124
- }
125
- };
126
- document.addEventListener('keydown', onKeyDown);
127
- return () => {
128
- document.removeEventListener('keydown', onKeyDown);
129
- };
130
- }, [
131
- setCurrentSuggestionIndex,
132
- setFocused,
133
- suggestions,
134
- currentSuggestionIndex,
135
- ]);
136
-
137
- useEffect(() => {
138
- if (focused) {
139
- setSearchResult({ current: [], others: [] });
140
- if (!pageSearcherRef.current) {
141
- initPageSearcher();
142
- }
143
- }
144
- }, [focused]);
145
-
146
- useEffect(() => {
147
- !initing && initPageSearcher();
148
- // init pageSearcher again when lang changed
149
- }, [lang]);
150
-
151
- const onQueryChanged = async (e: React.ChangeEvent<HTMLInputElement>) => {
152
- const newQuery = e.target.value;
153
- setQuery(newQuery);
154
- if (newQuery) {
155
- await onSearch(newQuery);
156
- const matched = await pageSearcherRef.current?.match(newQuery);
157
- setSearchResult(matched || { current: [], others: [] });
158
- }
159
- };
160
-
161
- const normalizeSuggestions = (suggestions: MatchResultItem[]) =>
162
- groupBy(suggestions, 'group');
163
-
164
- // accumulateIndex is used to calculate the index of the suggestion in the whole list.
165
- let accumulateIndex = -1;
166
-
167
- const renderSearchResult = (
168
- result: MatchResult,
169
- searchOptions: SearchOptions,
170
- ) => {
171
- if (!suggestions.length && !initing) {
172
- return <NoSearchResult query={query} />;
173
- }
174
-
175
- const hasOtherResult =
176
- searchResult.others.map(item => item.items).flat().length > 0;
177
-
178
- const tabValues = result.others.map(item => {
179
- if (!searchOptions || searchOptions.mode !== 'remote') {
180
- return item;
181
- }
182
- const indexItem = normalizeSearchIndexes(
183
- searchOptions.searchIndexes || [],
184
- ).find(indexInfo => indexInfo.value === item.index);
185
- return indexItem.label;
186
- }) as string[];
187
-
188
- return (
189
- <div>
190
- {/* current index */}
191
- {renderSearchResultItem(result.current)}
192
- {/* other indexes */}
193
- {hasOtherResult && (
194
- <h2 className={styles.groupTitle}>{RECOMMEND_WORD[lang]}</h2>
195
- )}
196
- <div style={{ marginTop: '-12px' }}>
197
- <Tabs values={tabValues} tabContainerClassName={styles.tabClassName}>
198
- {result.others.map(item => (
199
- <Tab key={item.index}>
200
- {renderSearchResultItem(item.items, false)}
201
- </Tab>
202
- ))}
203
- </Tabs>
204
- </div>
205
- </div>
206
- );
207
- };
208
-
209
- const renderSearchResultItem = (
210
- suggestionList: MatchResultItem[],
211
- isCurrent = true,
212
- ) => {
213
- // if no result, show no result
214
- if (suggestionList.length === 0 && !initing) {
215
- return (
216
- <div className="mt-4 flex-center">
217
- <div
218
- className="p-2 font-bold text-md"
219
- style={{
220
- color: '#2c3e50',
221
- }}
222
- >
223
- No results found
224
- </div>
225
- </div>
226
- );
227
- }
228
- const normalizedSuggestions = normalizeSuggestions(suggestionList);
229
- return (
230
- <ul className={styles.suggestList}>
231
- {Object.keys(normalizedSuggestions).map(group => {
232
- const groupSuggestions = normalizedSuggestions[group] || [];
233
- return (
234
- <li key={group}>
235
- {isCurrent && <h2 className={styles.groupTitle}>{group}</h2>}
236
- <ul className="pb-2">
237
- {groupSuggestions.map(suggestion => {
238
- accumulateIndex++;
239
- const suggestionIndex = accumulateIndex;
240
- return (
241
- <SuggestItem
242
- key={`${suggestion.title}-${suggestionIndex}`}
243
- suggestion={suggestion}
244
- isCurrent={suggestionIndex === currentSuggestionIndex}
245
- setCurrentSuggestionIndex={() => {
246
- setCurrentSuggestionIndex(suggestionIndex);
247
- }}
248
- closeSearch={() => setFocused(false)}
249
- inCurrentDocIndex={
250
- suggestionIndex < searchResult.current.length
251
- }
252
- />
253
- );
254
- })}
255
- </ul>
256
- </li>
257
- );
258
- })}
259
- </ul>
260
- );
261
- };
262
-
263
8
  return (
264
9
  <>
265
10
  <div className={styles.navSearchButton} onClick={() => setFocused(true)}>
@@ -278,74 +23,9 @@ export function Search() {
278
23
  >
279
24
  <SearchSvg />
280
25
  </div>
281
- {focused &&
282
- createPortal(
283
- <div className={styles.mask} onClick={() => setFocused(false)}>
284
- <div
285
- className={`${styles.modal}`}
286
- onClick={e => {
287
- setFocused(true);
288
- e.stopPropagation();
289
- }}
290
- >
291
- <div className="flex items-center">
292
- <div className={styles.inputForm}>
293
- <label>
294
- <SearchSvg />
295
- </label>
296
- <input
297
- className={styles.input}
298
- ref={searchInputRef}
299
- placeholder="Search Docs"
300
- aria-label="Search"
301
- autoComplete="off"
302
- autoFocus
303
- onChange={debounce(onQueryChanged, 150)}
304
- />
305
- <label>
306
- <CloseSvg
307
- className={styles.close}
308
- onClick={e => {
309
- if (searchInputRef.current) {
310
- e.stopPropagation();
311
- if (!query) {
312
- setFocused(false);
313
- } else {
314
- searchInputRef.current.value = '';
315
- setQuery('');
316
- }
317
- }
318
- }}
319
- />
320
- </label>
321
- </div>
322
- <h2
323
- className="text-brand ml-2 sm:hidden cursor-pointer"
324
- onClick={e => {
325
- e.stopPropagation();
326
- setFocused(false);
327
- }}
328
- >
329
- Cancel
330
- </h2>
331
- </div>
332
-
333
- {query ? (
334
- <div className={`${styles.searchHits} rspress-scrollbar`}>
335
- {renderSearchResult(searchResult, search)}
336
- </div>
337
- ) : null}
338
- {initing && (
339
- <div className="flex-center">
340
- <div className="p-2 text-sm">
341
- <LoadingSvg />
342
- </div>
343
- </div>
344
- )}
345
- </div>
346
- </div>,
347
- document.getElementById('search-container')!,
348
- )}
26
+ <SearchPanel focused={focused} setFocused={setFocused} />
349
27
  </>
350
28
  );
351
29
  }
30
+
31
+ export { SearchPanel };
@@ -4,7 +4,7 @@ import { HomeLayout } from './layout/HomeLayout';
4
4
  import { setup } from './logic';
5
5
 
6
6
  export { Nav } from './components/Nav';
7
- export { Search } from './components/Search';
7
+ export { Search, SearchPanel } from './components/Search';
8
8
  export { Tab, Tabs } from './components/Tabs';
9
9
  export { Button } from './components/Button';
10
10
  export { Link } from './components/Link';
@@ -22,3 +22,5 @@ export default {
22
22
  export { getCustomMDXComponent } from './layout/DocLayout/docComponents';
23
23
 
24
24
  export * from './logic';
25
+
26
+ export { Layout, NotFoundLayout, HomeLayout };
@@ -1,30 +1,15 @@
1
1
  import { ComponentProps } from 'react';
2
- import { isExternalUrl, normalizeHref, normalizeSlash } from '@rspress/shared';
3
2
  import styles from './index.module.scss';
4
- import { withBase, useLang, usePageData, removeBase } from '@/runtime';
3
+ import { usePathUtils } from '@/theme-default/logic';
5
4
 
6
5
  export const A = (props: ComponentProps<'a'>) => {
7
- let { href = '' } = props;
8
- const lang = useLang();
9
- const pageData = usePageData();
10
- const defaultLang = pageData.siteData.lang;
11
-
12
- if (defaultLang && !isExternalUrl(href) && !href.startsWith('#')) {
13
- href = normalizeSlash(href);
14
- const startWithLang = removeBase(href).startsWith(`/${lang}`);
15
- // Add lang prefix if not default lang
16
- if (lang !== defaultLang && !startWithLang) {
17
- href = `/${lang}${href}`;
18
- }
19
-
20
- if (lang === defaultLang && startWithLang) {
21
- href = removeBase(href).replace(`/${lang}`, '');
22
- }
23
-
24
- href = normalizeHref(withBase(href || ''));
25
- }
26
-
6
+ const { href = '' } = props;
7
+ const { normalizeLinkHref } = usePathUtils();
27
8
  return (
28
- <a {...props} className={`${styles.link} ${props.className}`} href={href} />
9
+ <a
10
+ {...props}
11
+ className={`${styles.link} ${props.className}`}
12
+ href={normalizeLinkHref(href)}
13
+ />
29
14
  );
30
15
  };
@@ -14,6 +14,52 @@
14
14
  display: none;
15
15
  }
16
16
 
17
+ :global(.rspress-doc) {
18
+ min-height: 60vh;
19
+ view-transition-name: flip;
20
+ }
21
+
22
+ :global(*) {
23
+ view-transition-name: none;
24
+ }
25
+
26
+
27
+ @keyframes fade-out {
28
+ from {
29
+ opacity: 1;
30
+ }
31
+ to {
32
+ opacity: 0;
33
+ transform: translateY(20px);
34
+ }
35
+ }
36
+
37
+ @keyframes fade-in {
38
+ from {
39
+ opacity: 0;
40
+ transform: translateY(20px);
41
+ }
42
+ to {
43
+ opacity: 1;
44
+ }
45
+ }
46
+
47
+ ::view-transition-old(flip),
48
+ ::view-transition-new(flip) {
49
+ animation-time: 250ms;
50
+ }
51
+
52
+ ::view-transition-old(flip) {
53
+ animation-name: fade-out;
54
+ transition-timing-function: cubic-bezier(0, 0, 0.58, 1);
55
+ }
56
+
57
+ ::view-transition-new(flip) {
58
+ animation-name: fade-in;
59
+ animation-delay: 180ms;
60
+ transition-timing-function: cubic-bezier(0.42, 0, 1, 1);
61
+ }
62
+
17
63
  @media (max-width: 960px) {
18
64
  .docLayout {
19
65
  position: relative;
@@ -34,7 +80,8 @@
34
80
  width: calc(100% - var(--rp-sidebar-width));
35
81
  padding: 48px 48px 72px 0;
36
82
 
37
- :global(.rspress-doc) {
83
+ :global(.rspress-doc),
84
+ :global(.rspress-doc-footer) {
38
85
  width: 100%;
39
86
  }
40
87
  }
@@ -52,7 +99,8 @@
52
99
 
53
100
  .content {
54
101
  padding: 48px 0 72px 0;
55
- :global(.rspress-doc) {
102
+ :global(.rspress-doc),
103
+ :global(.rspress-doc-footer) {
56
104
  padding: 0 48px;
57
105
  box-sizing: content-box;
58
106
  width: calc(100vw - var(--rp-aside-width) - var(--rp-sidebar-width));
@@ -67,7 +115,8 @@
67
115
  }
68
116
 
69
117
  .content {
70
- :global(.rspress-doc) {
118
+ :global(.rspress-doc),
119
+ :global(.rspress-doc-footer) {
71
120
  box-sizing: content-box;
72
121
  width: calc(100vw - var(--rp-sidebar-width) - var(--rp-aside-width));
73
122
  padding: 0
@@ -83,13 +83,15 @@ export function DocLayout(props: DocLayoutProps) {
83
83
  {isOverviewPage ? (
84
84
  <Overview />
85
85
  ) : (
86
- <div className="rspress-doc">
87
- <TabDataContext.Provider value={{ tabData, setTabData }}>
88
- <MDXProvider components={getCustomMDXComponent()}>
89
- <Content />
90
- </MDXProvider>
91
- </TabDataContext.Provider>
92
- <div>
86
+ <div>
87
+ <div className="rspress-doc">
88
+ <TabDataContext.Provider value={{ tabData, setTabData }}>
89
+ <MDXProvider components={getCustomMDXComponent()}>
90
+ <Content />
91
+ </MDXProvider>
92
+ </TabDataContext.Provider>
93
+ </div>
94
+ <div className="rspress-doc-footer">
93
95
  {beforeDocFooter}
94
96
  {hasFooter && <DocFooter />}
95
97
  </div>
@@ -3,4 +3,5 @@ export { useEditLink } from './useEditLink';
3
3
  export { useSidebarData } from './useSidebarData';
4
4
  export { useLocaleSiteData } from './useLocaleSiteData';
5
5
  export { setup, bindingAsideScroll, scrollToTarget } from './sideEffects';
6
+ export { usePathUtils } from './usePathUtils';
6
7
  export * from './utils';
@@ -19,7 +19,6 @@ const setClass = (dark: boolean): void => {
19
19
  const updateAppearance = (): void => {
20
20
  const disableDarkMode = siteData.themeConfig.darkMode === false;
21
21
  if (disableDarkMode) {
22
- setClass(false);
23
22
  return;
24
23
  }
25
24
  // We set the MODERN_THEME as a global variable to determine whether the theme is dark or light.
@@ -0,0 +1,72 @@
1
+ import { isExternalUrl, normalizeHref } from '@rspress/shared';
2
+ import {
3
+ removeBase,
4
+ useLang,
5
+ usePageData,
6
+ useVersion,
7
+ withBase,
8
+ } from '@/runtime';
9
+
10
+ export function usePathUtils() {
11
+ const currentLang = useLang();
12
+ const currentVersion = useVersion();
13
+ const pageData = usePageData();
14
+ const defaultLang = pageData.siteData.lang;
15
+ const defaultVersion = pageData.siteData.multiVersion.default;
16
+
17
+ const normalizeLinkHref = (rawHref: string) => {
18
+ let href = rawHref;
19
+ if (
20
+ (defaultLang || defaultVersion) &&
21
+ !isExternalUrl(href) &&
22
+ !href.startsWith('#')
23
+ ) {
24
+ href = removeBase(href);
25
+ const linkParts = href.split('/').filter(Boolean);
26
+ let versionPart = '';
27
+ let langPart = '';
28
+ let purePathPart = '';
29
+
30
+ // When add the version prefix, the situation is as follows:
31
+ // - current version is not default version
32
+ // - current link does not start with currrent version
33
+ if (defaultVersion) {
34
+ if (currentVersion !== defaultVersion) {
35
+ versionPart = currentVersion;
36
+
37
+ if (linkParts[0] === currentVersion) {
38
+ linkParts.shift();
39
+ }
40
+ } else if (linkParts[0] === defaultVersion) {
41
+ linkParts.shift();
42
+ }
43
+ }
44
+
45
+ if (defaultLang) {
46
+ if (currentLang !== defaultLang) {
47
+ langPart = currentLang;
48
+
49
+ if (linkParts[0] === currentLang) {
50
+ linkParts.shift();
51
+ }
52
+ } else if (linkParts[0] === defaultLang) {
53
+ linkParts.shift();
54
+ }
55
+ }
56
+
57
+ purePathPart = linkParts.join('/');
58
+
59
+ return normalizeHref(
60
+ withBase(
61
+ [versionPart, langPart, purePathPart].filter(Boolean).join('/'),
62
+ ),
63
+ );
64
+ } else {
65
+ return href;
66
+ }
67
+ };
68
+
69
+ return {
70
+ normalizeLinkHref,
71
+ };
72
+ }