@rspress/core 1.2.0 → 1.3.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 (158) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.js +53 -40
  3. package/dist/index.js.map +1 -1
  4. package/dist/loader.d.ts +1 -1
  5. package/dist/loader.js.map +1 -1
  6. package/dist/runtime/App.js +8 -4
  7. package/dist/runtime/clientEntry.js +7 -4
  8. package/dist/runtime/ssrEntry.js +2 -2
  9. package/package.json +19 -15
  10. package/runtime.ts +1 -1
  11. package/src/runtime/App.tsx +8 -4
  12. package/src/runtime/clientEntry.tsx +7 -4
  13. package/src/runtime/ssrEntry.tsx +2 -2
  14. package/theme.ts +2 -2
  15. package/dist/runtime/Content.d.ts +0 -6
  16. package/dist/runtime/Content.js +0 -33
  17. package/dist/runtime/NoSSR.d.ts +0 -4
  18. package/dist/runtime/NoSSR.js +0 -17
  19. package/dist/runtime/hooks.d.ts +0 -39
  20. package/dist/runtime/hooks.js +0 -54
  21. package/dist/runtime/index.d.ts +0 -6
  22. package/dist/runtime/index.js +0 -34
  23. package/dist/runtime/utils.d.ts +0 -8
  24. package/dist/runtime/utils.js +0 -45
  25. package/dist/theme/bundle.css +0 -2691
  26. package/dist/theme/bundle.d.ts +0 -176
  27. package/dist/theme/bundle.js +0 -4433
  28. package/dist/theme/bundle.js.map +0 -1
  29. package/dist/theme/index.js +0 -6
  30. package/src/runtime/Content.tsx +0 -43
  31. package/src/runtime/NoSSR.tsx +0 -16
  32. package/src/runtime/hooks.ts +0 -110
  33. package/src/runtime/index.ts +0 -17
  34. package/src/runtime/utils.ts +0 -46
  35. package/src/theme-default/assets/arrow-down.svg +0 -1
  36. package/src/theme-default/assets/arrow-right.svg +0 -1
  37. package/src/theme-default/assets/copy.svg +0 -1
  38. package/src/theme-default/assets/down.svg +0 -1
  39. package/src/theme-default/assets/github.svg +0 -1
  40. package/src/theme-default/assets/loading.svg +0 -1
  41. package/src/theme-default/assets/menu.svg +0 -1
  42. package/src/theme-default/assets/moon.svg +0 -3
  43. package/src/theme-default/assets/right.svg +0 -1
  44. package/src/theme-default/assets/small-menu.svg +0 -1
  45. package/src/theme-default/assets/success.svg +0 -1
  46. package/src/theme-default/assets/sun.svg +0 -11
  47. package/src/theme-default/assets/translator.svg +0 -1
  48. package/src/theme-default/components/Aside/index.css +0 -22
  49. package/src/theme-default/components/Aside/index.tsx +0 -75
  50. package/src/theme-default/components/BackDrop/index.module.scss +0 -14
  51. package/src/theme-default/components/BackDrop/index.tsx +0 -13
  52. package/src/theme-default/components/Button/index.module.scss +0 -68
  53. package/src/theme-default/components/Button/index.tsx +0 -40
  54. package/src/theme-default/components/DocFooter/index.module.scss +0 -60
  55. package/src/theme-default/components/DocFooter/index.tsx +0 -66
  56. package/src/theme-default/components/EditLink/index.module.scss +0 -11
  57. package/src/theme-default/components/EditLink/index.tsx +0 -18
  58. package/src/theme-default/components/HomeFeatures/index.module.scss +0 -50
  59. package/src/theme-default/components/HomeFeatures/index.tsx +0 -65
  60. package/src/theme-default/components/HomeFooter/index.tsx +0 -15
  61. package/src/theme-default/components/HomeHero/index.module.scss +0 -42
  62. package/src/theme-default/components/HomeHero/index.tsx +0 -75
  63. package/src/theme-default/components/Link/index.module.scss +0 -9
  64. package/src/theme-default/components/Link/index.tsx +0 -90
  65. package/src/theme-default/components/LocalSideBar/index.scss +0 -40
  66. package/src/theme-default/components/LocalSideBar/index.tsx +0 -39
  67. package/src/theme-default/components/Nav/NavBarTitle.tsx +0 -43
  68. package/src/theme-default/components/Nav/NavMenuGroup.tsx +0 -156
  69. package/src/theme-default/components/Nav/NavMenuSingleItem.tsx +0 -40
  70. package/src/theme-default/components/Nav/NavTranslations.tsx +0 -16
  71. package/src/theme-default/components/Nav/NavVersions.tsx +0 -16
  72. package/src/theme-default/components/Nav/index.module.scss +0 -115
  73. package/src/theme-default/components/Nav/index.tsx +0 -150
  74. package/src/theme-default/components/Nav/menuDataHooks.tsx +0 -75
  75. package/src/theme-default/components/NavHambmger/index.module.scss +0 -81
  76. package/src/theme-default/components/NavHambmger/index.tsx +0 -39
  77. package/src/theme-default/components/NavScreen/NavScreenMenuGroup.tsx +0 -95
  78. package/src/theme-default/components/NavScreen/index.module.scss +0 -88
  79. package/src/theme-default/components/NavScreen/index.tsx +0 -123
  80. package/src/theme-default/components/Overview/index.module.scss +0 -154
  81. package/src/theme-default/components/Overview/index.tsx +0 -136
  82. package/src/theme-default/components/PackageManagerTabs/icons/Bun.tsx +0 -101
  83. package/src/theme-default/components/PackageManagerTabs/icons/Npm.tsx +0 -16
  84. package/src/theme-default/components/PackageManagerTabs/icons/Pnpm.tsx +0 -22
  85. package/src/theme-default/components/PackageManagerTabs/icons/Yarn.tsx +0 -18
  86. package/src/theme-default/components/PackageManagerTabs/index.scss +0 -5
  87. package/src/theme-default/components/PackageManagerTabs/index.tsx +0 -110
  88. package/src/theme-default/components/Search/NoSearchResult.tsx +0 -13
  89. package/src/theme-default/components/Search/SearchPanel.tsx +0 -340
  90. package/src/theme-default/components/Search/SuggestItem.tsx +0 -135
  91. package/src/theme-default/components/Search/assets/close.svg +0 -1
  92. package/src/theme-default/components/Search/assets/file.svg +0 -1
  93. package/src/theme-default/components/Search/assets/header.svg +0 -1
  94. package/src/theme-default/components/Search/assets/jump.svg +0 -1
  95. package/src/theme-default/components/Search/assets/loading.svg +0 -1
  96. package/src/theme-default/components/Search/assets/search.svg +0 -1
  97. package/src/theme-default/components/Search/assets/title.svg +0 -1
  98. package/src/theme-default/components/Search/empty.svg +0 -4
  99. package/src/theme-default/components/Search/index.module.scss +0 -236
  100. package/src/theme-default/components/Search/index.tsx +0 -39
  101. package/src/theme-default/components/Search/logic/Provider.ts +0 -35
  102. package/src/theme-default/components/Search/logic/providers/LocalProvider.ts +0 -102
  103. package/src/theme-default/components/Search/logic/providers/RemoteProvider.ts +0 -40
  104. package/src/theme-default/components/Search/logic/search.ts +0 -341
  105. package/src/theme-default/components/Search/logic/util.test.ts +0 -21
  106. package/src/theme-default/components/Search/logic/util.ts +0 -127
  107. package/src/theme-default/components/Sidebar/SidebarGroup.tsx +0 -180
  108. package/src/theme-default/components/Sidebar/SidebarItem.tsx +0 -61
  109. package/src/theme-default/components/Sidebar/index.module.scss +0 -142
  110. package/src/theme-default/components/Sidebar/index.tsx +0 -156
  111. package/src/theme-default/components/SocialLinks/HiddenLinks.tsx +0 -30
  112. package/src/theme-default/components/SocialLinks/LinkContent.tsx +0 -90
  113. package/src/theme-default/components/SocialLinks/ShownLinks.tsx +0 -32
  114. package/src/theme-default/components/SocialLinks/index.module.scss +0 -18
  115. package/src/theme-default/components/SocialLinks/index.tsx +0 -36
  116. package/src/theme-default/components/SocialLinks/presetIcons.tsx +0 -165
  117. package/src/theme-default/components/SwitchAppearance/index.module.scss +0 -15
  118. package/src/theme-default/components/SwitchAppearance/index.tsx +0 -35
  119. package/src/theme-default/components/Tabs/index.module.scss +0 -46
  120. package/src/theme-default/components/Tabs/index.tsx +0 -123
  121. package/src/theme-default/components/Tag/index.tsx +0 -18
  122. package/src/theme-default/components/Toc/index.css +0 -18
  123. package/src/theme-default/components/Toc/index.tsx +0 -42
  124. package/src/theme-default/index.ts +0 -26
  125. package/src/theme-default/layout/DocLayout/docComponents/code.tsx +0 -112
  126. package/src/theme-default/layout/DocLayout/docComponents/hr.tsx +0 -10
  127. package/src/theme-default/layout/DocLayout/docComponents/index.module.scss +0 -69
  128. package/src/theme-default/layout/DocLayout/docComponents/index.tsx +0 -33
  129. package/src/theme-default/layout/DocLayout/docComponents/link.tsx +0 -22
  130. package/src/theme-default/layout/DocLayout/docComponents/list.tsx +0 -13
  131. package/src/theme-default/layout/DocLayout/docComponents/paragraph.tsx +0 -19
  132. package/src/theme-default/layout/DocLayout/docComponents/pre.tsx +0 -37
  133. package/src/theme-default/layout/DocLayout/docComponents/prisim-theme.ts +0 -73
  134. package/src/theme-default/layout/DocLayout/docComponents/table.tsx +0 -37
  135. package/src/theme-default/layout/DocLayout/docComponents/title.tsx +0 -40
  136. package/src/theme-default/layout/DocLayout/index.module.scss +0 -138
  137. package/src/theme-default/layout/DocLayout/index.tsx +0 -125
  138. package/src/theme-default/layout/HomeLayout/index.css +0 -18
  139. package/src/theme-default/layout/HomeLayout/index.tsx +0 -39
  140. package/src/theme-default/layout/Layout/index.tsx +0 -176
  141. package/src/theme-default/layout/NotFountLayout/index.tsx +0 -40
  142. package/src/theme-default/logic/TabDataContext.ts +0 -14
  143. package/src/theme-default/logic/index.ts +0 -8
  144. package/src/theme-default/logic/sideEffects.ts +0 -174
  145. package/src/theme-default/logic/useAppearance.ts +0 -57
  146. package/src/theme-default/logic/useEditLink.ts +0 -33
  147. package/src/theme-default/logic/useHiddenNav.ts +0 -58
  148. package/src/theme-default/logic/useLocaleSiteData.ts +0 -26
  149. package/src/theme-default/logic/useNav.ts +0 -36
  150. package/src/theme-default/logic/usePathUtils.ts +0 -73
  151. package/src/theme-default/logic/usePrevNextPage.ts +0 -40
  152. package/src/theme-default/logic/useSidebarData.ts +0 -73
  153. package/src/theme-default/logic/utils.ts +0 -38
  154. package/src/theme-default/styles/base.css +0 -102
  155. package/src/theme-default/styles/code.css +0 -177
  156. package/src/theme-default/styles/index.ts +0 -7
  157. package/src/theme-default/styles/scrollbar.css +0 -28
  158. package/src/theme-default/styles/vars.css +0 -81
@@ -1,341 +0,0 @@
1
- import {
2
- LocalSearchOptions,
3
- PageIndexInfo,
4
- RemotePageInfo,
5
- RemoteSearchOptions,
6
- } from '@rspress/shared';
7
- import { LOCAL_INDEX, NormalizedSearchResultItem, Provider } from './Provider';
8
- import {
9
- backTrackHeaders,
10
- byteToCharIndex,
11
- getStrByteLength,
12
- normalizeTextCase,
13
- } from './util';
14
- import { LocalProvider } from './providers/LocalProvider';
15
- import { RemoteProvider } from './providers/RemoteProvider';
16
- import { normalizeHrefInRuntime as normalizeHref } from '@/runtime';
17
-
18
- const THRESHOLD_CONTENT_LENGTH = 100;
19
-
20
- export interface HightlightInfo {
21
- start: number;
22
- length: number;
23
- }
24
-
25
- interface CommonMatchResult {
26
- title: string;
27
- header: string;
28
- link: string;
29
- query: string;
30
- highlightInfoList: HightlightInfo[];
31
- group: string;
32
- }
33
-
34
- interface TitleMatch extends CommonMatchResult {
35
- type: 'title';
36
- }
37
-
38
- interface HeaderMatch extends CommonMatchResult {
39
- type: 'header';
40
- }
41
-
42
- interface ContentMatch extends CommonMatchResult {
43
- type: 'content';
44
- statement: string;
45
- }
46
-
47
- export type MatchResultItem = TitleMatch | HeaderMatch | ContentMatch;
48
-
49
- export interface MatchResult {
50
- current: MatchResultItem[];
51
- others: {
52
- index: string;
53
- items: MatchResultItem[];
54
- }[];
55
- }
56
-
57
- export type SearchOptions = (LocalSearchOptions | RemoteSearchOptions) & {
58
- currentLang: string;
59
- extractGroupName: (path: string) => string;
60
- };
61
-
62
- export class PageSearcher {
63
- #options: SearchOptions;
64
-
65
- #indexName: string = LOCAL_INDEX;
66
-
67
- #provider?: Provider;
68
-
69
- constructor(options: SearchOptions) {
70
- this.#options = options;
71
- switch (options.mode) {
72
- case 'remote':
73
- this.#provider = new RemoteProvider();
74
- this.#indexName = options.indexName;
75
- break;
76
- default:
77
- this.#provider = new LocalProvider();
78
- break;
79
- }
80
- }
81
-
82
- async init() {
83
- await this.#provider?.init(this.#options);
84
- }
85
-
86
- async match(keyword: string, limit = 7) {
87
- const searchResult = await this.#provider?.search({ keyword, limit });
88
- const normaizedKeyWord = normalizeTextCase(keyword);
89
- const currentIndexInfo = searchResult?.find(res =>
90
- this.#isCurrentIndex(res.index),
91
- ) || {
92
- index: LOCAL_INDEX,
93
- hits: [],
94
- };
95
-
96
- const matchResult: MatchResult = {
97
- current: this.#matchResultItem(normaizedKeyWord, currentIndexInfo),
98
- others: (
99
- searchResult?.filter(res => !this.#isCurrentIndex(res.index)) || []
100
- ).map(res => ({
101
- index: res.index,
102
- items: this.#matchResultItem(normaizedKeyWord, res),
103
- })),
104
- };
105
-
106
- return matchResult;
107
- }
108
-
109
- #matchResultItem(
110
- normaizedKeyWord: string,
111
- resultItem: NormalizedSearchResultItem,
112
- ) {
113
- const matchedResult: MatchResultItem[] = [];
114
- resultItem?.hits.forEach(item => {
115
- // Title Match
116
- this.#matchTitle(item, normaizedKeyWord, matchedResult);
117
- // Header match
118
- const matchedHeader = this.#matchHeader(
119
- item,
120
- normaizedKeyWord,
121
- matchedResult,
122
- );
123
- // If we have matched header, we don't need to match content
124
- // Because the header is already in the content
125
- if (matchedHeader) {
126
- return;
127
- }
128
- // Content match
129
- this.#matchContent(item, normaizedKeyWord, matchedResult);
130
- });
131
- return matchedResult;
132
- }
133
-
134
- #matchTitle(
135
- item: PageIndexInfo,
136
- query: string,
137
- matchedResult: MatchResultItem[],
138
- ): boolean {
139
- const { title = '' } = item;
140
- const normalizedTitle = normalizeTextCase(title);
141
- if (normalizedTitle.includes(query)) {
142
- matchedResult.push({
143
- type: 'title',
144
- title,
145
- header: title,
146
- link: `${item.domain}${normalizeHref(item.routePath)}`,
147
- query,
148
- highlightInfoList: [
149
- {
150
- start: normalizedTitle.indexOf(query),
151
- length: getStrByteLength(query),
152
- },
153
- ],
154
- group: this.#options.extractGroupName(item.routePath),
155
- });
156
- return true;
157
- }
158
- return false;
159
- }
160
-
161
- #matchHeader(
162
- item: PageIndexInfo,
163
- query: string,
164
- matchedResult: MatchResultItem[],
165
- ): boolean {
166
- const { toc = [], domain = '', title = '' } = item;
167
- for (const [index, header] of toc.entries()) {
168
- const normalizedHeader = normalizeTextCase(header.text);
169
- if (normalizedHeader.includes(query)) {
170
- // Find the all parent headers
171
- // So we can show the full path of the header in search result
172
- // e.g. header2 > header3 > header4
173
- const headerGroup = backTrackHeaders(toc, index);
174
- const headerStr = headerGroup.map(item => item.text).join(' > ');
175
- const headerMatchIndex = normalizeTextCase(headerStr).indexOf(query);
176
- const titlePrefix = `${title} > `;
177
-
178
- matchedResult.push({
179
- type: 'header',
180
- title: item.title,
181
- header: `${titlePrefix}${headerStr}`,
182
- highlightInfoList: [
183
- {
184
- start: headerMatchIndex + titlePrefix.length,
185
- length: getStrByteLength(query),
186
- },
187
- ],
188
- link: `${domain}${normalizeHref(item.routePath)}#${header.id}`,
189
- query,
190
- group: this.#options.extractGroupName(item.routePath),
191
- });
192
- return true;
193
- }
194
- }
195
- return false;
196
- }
197
-
198
- #matchContent(
199
- item: PageIndexInfo,
200
- query: string,
201
- matchedResult: MatchResultItem[],
202
- ) {
203
- const { content, toc, domain } = item;
204
- if (!content.length) {
205
- return;
206
- }
207
- const normalizedContent = normalizeTextCase(content);
208
- let queryIndex = normalizedContent.indexOf(query);
209
- const headersIndex = toc.map(h => h.charIndex);
210
- const getCurrentHeader = (currentIndex: number) => {
211
- const currentHeaderIndex = headersIndex.findIndex((hIndex, position) => {
212
- if (position < toc.length - 1) {
213
- const next = headersIndex[position + 1];
214
- if (hIndex <= currentIndex && next >= currentIndex) {
215
- return true;
216
- }
217
- } else {
218
- return hIndex < currentIndex;
219
- }
220
- return false;
221
- });
222
- return toc[currentHeaderIndex];
223
- };
224
- if (queryIndex === -1) {
225
- // In case fuzzy search
226
- // We get the matched content position from server response
227
- const hightlightItems = (item as RemotePageInfo)._matchesPosition
228
- ?.content;
229
- if (!hightlightItems?.length) {
230
- return;
231
- }
232
- const highlightStartIndex = (item as RemotePageInfo)._matchesPosition
233
- .content[0].start;
234
- const currentHeader = getCurrentHeader(highlightStartIndex);
235
- const statementStartIndex = byteToCharIndex(content, highlightStartIndex);
236
- const statementEndIndex = byteToCharIndex(
237
- content,
238
- highlightStartIndex + THRESHOLD_CONTENT_LENGTH,
239
- );
240
- const statement = content.slice(statementStartIndex, statementEndIndex);
241
- const highlightInfoList = (
242
- item as RemotePageInfo
243
- )._matchesPosition.content
244
- .filter(
245
- match =>
246
- match.start >= highlightStartIndex &&
247
- match.start + match.length <=
248
- highlightStartIndex + THRESHOLD_CONTENT_LENGTH,
249
- )
250
- .map(match => {
251
- const startCharIndex =
252
- byteToCharIndex(content, match.start) - statementStartIndex + 3;
253
- return {
254
- // prefix `...` length is 3
255
- start: startCharIndex,
256
- length: match.length,
257
- };
258
- });
259
-
260
- matchedResult.push({
261
- type: 'content',
262
- title: item.title,
263
- header: currentHeader?.text ?? item.title,
264
- link: `${domain}${normalizeHref(item.routePath)}${
265
- currentHeader ? `#${currentHeader.id}` : ''
266
- }`,
267
- query,
268
- highlightInfoList,
269
- group: this.#options.extractGroupName(item.routePath),
270
- statement: `...${statement}...`,
271
- });
272
- return;
273
- }
274
- while (queryIndex !== -1) {
275
- const currentHeader = getCurrentHeader(queryIndex);
276
- let statementStartIndex = content.slice(0, queryIndex).lastIndexOf('\n');
277
- statementStartIndex =
278
- statementStartIndex === -1 ? 0 : statementStartIndex;
279
- const statementEndIndex = content.indexOf(
280
- '\n\n',
281
- queryIndex + query.length,
282
- );
283
- let statement = content.slice(statementStartIndex, statementEndIndex);
284
- if (statement.length > THRESHOLD_CONTENT_LENGTH) {
285
- statement = this.#normalizeStatement(statement, query);
286
- }
287
- const highlightIndex = normalizeTextCase(statement).indexOf(query);
288
- const highlightInfoList = [
289
- {
290
- start: highlightIndex,
291
- length: getStrByteLength(query),
292
- },
293
- ];
294
- matchedResult.push({
295
- type: 'content',
296
- title: item.title,
297
- header: currentHeader?.text ?? item.title,
298
- statement,
299
- highlightInfoList,
300
- link: `${domain}${normalizeHref(item.routePath)}${
301
- currentHeader ? `#${currentHeader.id}` : ''
302
- }`,
303
- query,
304
- group: this.#options.extractGroupName(item.routePath),
305
- });
306
- queryIndex = normalizedContent.indexOf(
307
- query,
308
- queryIndex + statement.length - highlightIndex,
309
- );
310
- }
311
- }
312
-
313
- #normalizeStatement(statement: string, query: string) {
314
- // If statement is too long, we will only show 120 characters
315
- const queryIndex = normalizeTextCase(statement).indexOf(
316
- normalizeTextCase(query),
317
- );
318
- const maxPrefixOrSuffix = Math.floor(
319
- (THRESHOLD_CONTENT_LENGTH - query.length) / 2,
320
- );
321
- let prefix = statement.slice(0, queryIndex);
322
- if (prefix.length > maxPrefixOrSuffix) {
323
- prefix = `...${statement.slice(
324
- queryIndex - maxPrefixOrSuffix + 3,
325
- queryIndex,
326
- )}`;
327
- }
328
- let suffix = statement.slice(queryIndex + query.length);
329
- if (suffix.length > maxPrefixOrSuffix) {
330
- suffix = `${statement.slice(
331
- queryIndex + query.length,
332
- queryIndex + maxPrefixOrSuffix - 3,
333
- )}...`;
334
- }
335
- return prefix + query + suffix;
336
- }
337
-
338
- #isCurrentIndex(index: string) {
339
- return index === this.#indexName || index === LOCAL_INDEX;
340
- }
341
- }
@@ -1,21 +0,0 @@
1
- import { expect, test, describe } from 'vitest';
2
- import { Header } from '@rspress/shared';
3
- import { backTrackHeaders } from './util';
4
-
5
- describe('utils logic', () => {
6
- test('back track the headers', () => {
7
- const headers = [
8
- { depth: 1, text: '1', id: '1' },
9
- { depth: 2, text: '2', id: '2' },
10
- { depth: 3, text: '3', id: '3' },
11
- { depth: 4, text: '4', id: '4' },
12
- { depth: 5, text: '5', id: '5' },
13
- ];
14
- const res = backTrackHeaders(headers as Header[], 3);
15
- expect(res).toEqual([
16
- { depth: 2, text: '2', id: '2' },
17
- { depth: 3, text: '3', id: '3' },
18
- { depth: 4, text: '4', id: '4' },
19
- ]);
20
- });
21
- });
@@ -1,127 +0,0 @@
1
- import { RemoteSearchIndexInfo, Header } from '@rspress/shared';
2
-
3
- const MAX_TITLE_LENGTH = 20;
4
-
5
- export function backTrackHeaders(
6
- rawHeaders: Header[],
7
- index: number,
8
- ): Header[] {
9
- let current = rawHeaders[index];
10
- let currentIndex = index;
11
-
12
- const res: Header[] = [current];
13
- while (current && current.depth > 2) {
14
- // If there is no parent header, we will stop the loop
15
- let matchedParent = false;
16
- for (let i = currentIndex - 1; i >= 0; i--) {
17
- const header = rawHeaders[i];
18
- if (header.depth > 1 && header.depth === current.depth - 1) {
19
- current = header;
20
- currentIndex = i;
21
- res.unshift(current);
22
- matchedParent = true;
23
- break;
24
- }
25
- }
26
- if (!matchedParent) {
27
- break;
28
- }
29
- }
30
- return res;
31
- }
32
-
33
- export function formatText(text: string) {
34
- return text.length > MAX_TITLE_LENGTH
35
- ? `${text.slice(0, MAX_TITLE_LENGTH)}...`
36
- : text;
37
- }
38
-
39
- export function normalizeTextCase(text: string) {
40
- return text
41
- .toLowerCase()
42
- .normalize('NFD')
43
- .replace(/[\u0300-\u036f]/g, '');
44
- }
45
-
46
- export function removeDomain(url: string) {
47
- return url.replace(/https?:\/\/[^/]+/, '');
48
- }
49
-
50
- function getCharByteCount(char: string) {
51
- const charCode = char.charCodeAt(0);
52
- if (charCode > 255) {
53
- // Chinese character
54
- return 3;
55
- } else {
56
- return 1;
57
- }
58
- }
59
-
60
- export const normalizeSearchIndexes = (
61
- items: RemoteSearchIndexInfo[],
62
- ): { value: string; label: string }[] => {
63
- return items.map(item =>
64
- typeof item === 'string'
65
- ? {
66
- value: item,
67
- label: item,
68
- }
69
- : item,
70
- );
71
- };
72
-
73
- export function substrByBytes(str: string, start: number, len: number): string {
74
- let resultStr = '';
75
- let bytesCount = 0;
76
- const strLength = str.length;
77
- for (let i = 0; i < strLength; i++) {
78
- bytesCount += getCharByteCount(str.charAt(i));
79
- if (bytesCount > start + len) {
80
- break;
81
- } else if (bytesCount > start) {
82
- resultStr += str.charAt(i);
83
- }
84
- }
85
- return resultStr;
86
- }
87
-
88
- export function byteToCharIndex(str: string, byteIndex: number): number {
89
- let charIndex = 0;
90
- let byteCount = 0;
91
-
92
- for (let i = 0; i < str.length; i++) {
93
- if (byteCount >= byteIndex) {
94
- break;
95
- }
96
-
97
- byteCount += getCharByteCount(str.charAt(i));
98
-
99
- charIndex++;
100
- }
101
-
102
- return charIndex;
103
- }
104
-
105
- /**
106
- *
107
- * @param str raw text content
108
- * @param start start index (char index)
109
- * @param length byte length for sliced string
110
- * @returns sliced string
111
- */
112
- export function getSlicedStrByByteLength(
113
- str: string,
114
- start: number,
115
- length: number,
116
- ): string {
117
- const slicedStr = str.slice(start);
118
- return substrByBytes(slicedStr, 0, length);
119
- }
120
-
121
- export function getStrByteLength(str: string): number {
122
- let byteLength = 0;
123
- for (let i = 0; i < str.length; i++) {
124
- byteLength += getCharByteCount(str.charAt(i));
125
- }
126
- return byteLength;
127
- }
@@ -1,180 +0,0 @@
1
- import React, { useEffect, useRef } from 'react';
2
- import { NormalizedSidebarGroup } from '@rspress/shared';
3
- import { useNavigate } from 'react-router-dom';
4
- import ArrowRight from '../../assets/arrow-right.svg';
5
- import { Tag } from '../Tag';
6
- import styles from './index.module.scss';
7
- import { SidebarItem } from './SidebarItem';
8
- import { highlightTitleStyle, matchCache, type SidebarItemProps } from '.';
9
- import { normalizeHrefInRuntime as normalizeHref, withBase } from '@/runtime';
10
-
11
- export function SidebarGroup(props: SidebarItemProps) {
12
- const { item, depth = 0, activeMatcher, id, setSidebarData } = props;
13
- const navigate = useNavigate();
14
- const isGroupMatched = matchCache.get(item);
15
- const containerRef = useRef<HTMLDivElement>(null);
16
- const transitionRef = useRef<any>(null);
17
- const innerRef = useRef<HTMLDivElement>(null);
18
- const initialRender = useRef(true);
19
- const initialState = useRef((item as NormalizedSidebarGroup).collapsed);
20
- const active = item.link && activeMatcher(item.link);
21
- const { collapsed, collapsible = true } = item as NormalizedSidebarGroup;
22
- const collapsibleIcon = (
23
- <div
24
- style={{
25
- cursor: 'pointer',
26
- transition: 'transform 0.2s ease-out',
27
- transform: collapsed ? 'rotate(0deg)' : 'rotate(90deg)',
28
- }}
29
- >
30
- <ArrowRight />
31
- </div>
32
- );
33
-
34
- useEffect(() => {
35
- if (initialRender.current) {
36
- return;
37
- }
38
-
39
- if (!containerRef.current || !innerRef.current) {
40
- return;
41
- }
42
-
43
- if (transitionRef.current) {
44
- clearTimeout(transitionRef.current);
45
- }
46
-
47
- const container = containerRef.current;
48
- const inner = innerRef.current;
49
- // We should add the margin-top(4px) of first item in list, which is a part of the height of the container
50
- const contentHeight = inner.clientHeight + 4;
51
- if (collapsed) {
52
- container.style.maxHeight = `${contentHeight}px`;
53
- container.style.transitionDuration = '0.5s';
54
- inner.style.opacity = '0';
55
-
56
- transitionRef.current = setTimeout(() => {
57
- if (containerRef.current) {
58
- containerRef.current.style.maxHeight = '0px';
59
- }
60
- }, 0);
61
- } else {
62
- container.style.maxHeight = `${contentHeight}px`;
63
- container.style.transitionDuration = '0.3s';
64
- inner.style.opacity = '1';
65
-
66
- transitionRef.current = setTimeout(() => {
67
- if (containerRef.current) {
68
- containerRef.current.style.removeProperty('max-height');
69
- }
70
- }, 300);
71
- }
72
- }, [collapsed]);
73
-
74
- useEffect(() => {
75
- initialRender.current = false;
76
- }, []);
77
-
78
- const toggleCollapse: React.MouseEventHandler<HTMLDivElement> = (e): void => {
79
- e.stopPropagation();
80
- // update collapsed state
81
- setSidebarData(sidebarData => {
82
- const newSidebarData = [...sidebarData];
83
- const indexes = id.split('-').map(Number);
84
- const initialIndex = indexes.shift()!;
85
- const root = newSidebarData[initialIndex];
86
- let current = root;
87
- for (const index of indexes) {
88
- current = (current as NormalizedSidebarGroup).items[index];
89
- }
90
- if ('items' in current) {
91
- current.collapsed = !current.collapsed;
92
- }
93
- return newSidebarData;
94
- });
95
- };
96
-
97
- return (
98
- <section
99
- key={id}
100
- className="mt-0.5 block"
101
- style={{
102
- marginLeft: depth === 0 ? 0 : '18px',
103
- }}
104
- >
105
- <div
106
- className={`flex justify-between items-center ${
107
- isGroupMatched && !active ? styles.menuGroupActive : ''
108
- } ${
109
- // eslint-disable-next-line no-nested-ternary
110
- active
111
- ? styles.menuItemActive
112
- : collapsible || item.link
113
- ? styles.menuItem
114
- : styles.menuItemStatic
115
- }`}
116
- onMouseEnter={() => item.link && props.preloadLink(item.link)}
117
- onClick={e => {
118
- if (item.link) {
119
- navigate(withBase(normalizeHref(item.link)));
120
- collapsed && toggleCollapse(e);
121
- } else {
122
- collapsible && toggleCollapse(e);
123
- }
124
- }}
125
- style={{
126
- borderRadius: depth === 0 ? '0 1rem 1rem 0' : undefined,
127
- cursor: collapsible || item.link ? 'pointer' : 'normal',
128
- }}
129
- >
130
- <h2
131
- className="py-2 px-3 text-sm font-medium flex"
132
- style={{
133
- ...(depth === 0 ? highlightTitleStyle : {}),
134
- }}
135
- >
136
- <Tag tag={item.tag} />
137
- {item.text}
138
- </h2>
139
- {collapsible && (
140
- <div
141
- className={`${styles.collapseContainer} p-2 rounded-xl`}
142
- onClick={toggleCollapse}
143
- >
144
- {collapsibleIcon}
145
- </div>
146
- )}
147
- </div>
148
- <div
149
- ref={containerRef}
150
- className="transition-all duration-300 ease-in-out"
151
- style={{
152
- overflow: 'hidden',
153
- maxHeight: initialState.current ? 0 : undefined,
154
- }}
155
- >
156
- <div
157
- ref={innerRef}
158
- className="rspress-sidebar-group transition-opacity duration-500 ease-in-out"
159
- style={{
160
- opacity: initialState.current ? 0 : 1,
161
- marginLeft: depth === 0 ? '12px' : 0,
162
- }}
163
- >
164
- {(item as NormalizedSidebarGroup)?.items?.map((item, index) => (
165
- // eslint-disable-next-line react/no-array-index-key
166
- <div key={index}>
167
- <SidebarItem
168
- {...props}
169
- item={item}
170
- depth={depth + 1}
171
- id={`${id}-${index}`}
172
- preloadLink={props.preloadLink}
173
- />
174
- </div>
175
- ))}
176
- </div>
177
- </div>
178
- </section>
179
- );
180
- }