@rspress/shared 2.0.0-beta.2 → 2.0.0-beta.20

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/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { loadConfig } from '@rsbuild/core';
2
2
  import type { PluggableList } from 'unified';
3
+ import type { RehypeShikiOptions } from '@shikijs/rehype';
3
4
  import type { RsbuildConfig } from '@rsbuild/core';
4
5
  import type { RsbuildPlugin } from '@rsbuild/core';
5
6
 
@@ -143,7 +144,7 @@ export declare interface DefaultThemeConfig {
143
144
  * Whether to redirect to the closest locale when the user visits the site
144
145
  * @default 'auto'
145
146
  */
146
- localeRedirect?: 'auto' | 'never';
147
+ localeRedirect?: 'auto' | 'never' | 'only-default-lang';
147
148
  /**
148
149
  * Whether to show the fallback heading title when the heading title is not presented but `frontmatter.title` exists
149
150
  * @default true
@@ -220,7 +221,7 @@ export declare interface FrontMatterMeta {
220
221
  * @param currentPathname
221
222
  * @returns
222
223
  */
223
- export declare const getSidebarDataGroup: (sidebar: NormalizedSidebar, currentPathname: string, base: string) => NormalizedSidebar[string];
224
+ export declare const getSidebarDataGroup: (sidebar: NormalizedSidebar, currentPathname: string) => NormalizedSidebar[string];
224
225
 
225
226
  export declare const HASH_REGEXP: RegExp;
226
227
 
@@ -330,7 +331,7 @@ export declare type LocalSearchOptions = SearchHooks & {
330
331
  versioned?: boolean;
331
332
  /**
332
333
  * If enabled, the search index will include code block content, which allows users to search code blocks.
333
- * @default false
334
+ * @default true
334
335
  */
335
336
  codeBlocks?: boolean;
336
337
  };
@@ -352,23 +353,24 @@ export declare interface MarkdownOptions {
352
353
  */
353
354
  globalComponents?: string[];
354
355
  /**
355
- * Code highlighter, default is prism for performance reason
356
+ * @type import('@shikijs/rehype').RehypeShikiOptions
356
357
  */
357
- codeHighlighter?: 'prism' | 'shiki';
358
+ shiki?: Partial<PluginShikiOptions>;
358
359
  /**
359
- * Register prism languages
360
+ * Speed up build time by caching mdx parsing result in `rspress build`
361
+ * @default true
360
362
  */
361
- highlightLanguages?: (string | [string, string])[];
363
+ crossCompilerCache?: boolean;
362
364
  }
363
365
 
364
- export declare const matchNavbar: (item: NavItemWithLink, currentPathname: string, base: string) => boolean;
366
+ export declare const matchNavbar: (item: NavItemWithLink, currentPathname: string) => boolean;
365
367
 
366
368
  /**
367
369
  * match the sidebar key in user config
368
370
  * @param pattern /zh/guide
369
371
  * @param currentPathname /base/zh/guide/getting-started
370
372
  */
371
- export declare const matchSidebar: (pattern: string, currentPathname: string, base: string) => boolean;
373
+ export declare const matchSidebar: (pattern: string, currentPathname: string) => boolean;
372
374
 
373
375
  export declare const MDX_OR_MD_REGEXP: RegExp;
374
376
 
@@ -413,12 +415,16 @@ export declare interface NormalizedLocales extends Omit<LocaleConfig, 'sidebar'>
413
415
  sidebar: NormalizedSidebar;
414
416
  }
415
417
 
418
+ export declare interface NormalizedRuntimeConfig {
419
+ base: string;
420
+ }
421
+
416
422
  export declare interface NormalizedSidebar {
417
423
  [path: string]: (NormalizedSidebarGroup | SidebarItem | SidebarDivider)[];
418
424
  }
419
425
 
420
426
  export declare interface NormalizedSidebarGroup extends Omit<SidebarGroup, 'items'> {
421
- items: (SidebarDivider | SidebarItem | NormalizedSidebarGroup)[];
427
+ items: (SidebarDivider | SidebarItem | SidebarSectionHeader | NormalizedSidebarGroup)[];
422
428
  collapsible: boolean;
423
429
  collapsed: boolean;
424
430
  }
@@ -447,9 +453,8 @@ export declare interface PageData {
447
453
  * and should not be used in the runtime (usePageData).
448
454
  */
449
455
  export declare interface PageIndexInfo {
450
- id: number;
451
- title: string;
452
456
  routePath: string;
457
+ title: string;
453
458
  toc: Header[];
454
459
  content: string;
455
460
  _flattenContent?: string;
@@ -476,6 +481,8 @@ export declare const parseUrl: (url: string) => {
476
481
  hash: string;
477
482
  };
478
483
 
484
+ declare type PluginShikiOptions = RehypeShikiOptions;
485
+
479
486
  export declare const QUERY_REGEXP: RegExp;
480
487
 
481
488
  export declare type RemotePageInfo = PageIndexInfo & {
@@ -622,6 +629,7 @@ declare interface RspressPlugin {
622
629
  addPages?: (config: UserConfig, isProd: boolean) => AdditionalPage[] | Promise<AdditionalPage[]>;
623
630
  /**
624
631
  * Add runtime modules
632
+ * @deprecated use [rsbuild-plugin-virtual-module](https://github.com/rspack-contrib/rsbuild-plugin-virtual-module) instead.
625
633
  */
626
634
  addRuntimeModules?: (config: UserConfig, isProd: boolean) => Record<string, string> | Promise<Record<string, string>>;
627
635
  /**
@@ -672,7 +680,7 @@ export declare interface SidebarGroup {
672
680
  text: string;
673
681
  link?: string;
674
682
  tag?: string;
675
- items: (SidebarItem | SidebarDivider | SidebarGroup | string)[];
683
+ items: (SidebarGroup | SidebarItem | SidebarDivider | SidebarSectionHeader)[];
676
684
  collapsible?: boolean;
677
685
  collapsed?: boolean;
678
686
  /**
@@ -702,7 +710,6 @@ export declare type SidebarSectionHeader = {
702
710
 
703
711
  export declare interface SiteData<ThemeConfig = NormalizedConfig> {
704
712
  root: string;
705
- base: string;
706
713
  lang: string;
707
714
  route: RouteOptions;
708
715
  locales: {
@@ -724,7 +731,7 @@ export declare interface SiteData<ThemeConfig = NormalizedConfig> {
724
731
  markdown: {
725
732
  showLineNumbers: boolean;
726
733
  defaultWrapCode: boolean;
727
- codeHighlighter: 'prism' | 'shiki';
734
+ shiki: Partial<PluginShikiOptions>;
728
735
  };
729
736
  multiVersion: {
730
737
  default: string;
@@ -770,7 +777,7 @@ export declare interface UserConfig<ThemeConfig = DefaultThemeConfig> {
770
777
  /**
771
778
  * Path to html icon file.
772
779
  */
773
- icon?: string;
780
+ icon?: string | URL;
774
781
  /**
775
782
  * Default language of the site.
776
783
  */
@@ -890,8 +897,6 @@ export declare interface UserConfig<ThemeConfig = DefaultThemeConfig> {
890
897
 
891
898
  export declare function withBase(url: string, base: string): string;
892
899
 
893
- export declare function withoutBase(path: string, base: string): string;
894
-
895
900
  export declare function withoutLang(path: string, langs: string[]): string;
896
901
 
897
902
  declare interface ZoomContainer {
package/dist/index.js CHANGED
@@ -1,3 +1,18 @@
1
+ const matchSidebar = (pattern, currentPathname)=>{
2
+ if (pattern === currentPathname) return true;
3
+ if (currentPathname.startsWith(pattern)) return true;
4
+ const prefixWithDot = `${pattern}.`;
5
+ return currentPathname.startsWith(prefixWithDot);
6
+ };
7
+ const getSidebarDataGroup = (sidebar, currentPathname)=>{
8
+ const navRoutes = Object.keys(sidebar).sort((a, b)=>b.length - a.length);
9
+ for (const name of navRoutes)if (matchSidebar(name, currentPathname)) {
10
+ const sidebarGroup = sidebar[name];
11
+ return sidebarGroup;
12
+ }
13
+ return [];
14
+ };
15
+ const matchNavbar = (item, currentPathname)=>new RegExp(item.activeMatch || item.link).test(currentPathname);
1
16
  const QUERY_REGEXP = /\?.*$/s;
2
17
  const HASH_REGEXP = /#.*$/s;
3
18
  const MDX_OR_MD_REGEXP = /\.mdx?$/;
@@ -100,11 +115,11 @@ function replaceLang(rawUrl, lang, version, base = '', cleanUrls = false, isPage
100
115
  } else parts.shift();
101
116
  purePathPart = parts.join('/') || '';
102
117
  if ((versionPart || langPart) && !purePathPart) purePathPart = cleanUrls ? 'index' : 'index.html';
103
- return withBase(addLeadingSlash([
118
+ return addLeadingSlash([
104
119
  versionPart,
105
120
  langPart,
106
121
  purePathPart
107
- ].filter(Boolean).join('/')), base);
122
+ ].filter(Boolean).join('/'));
108
123
  }
109
124
  function replaceVersion(rawUrl, version, base = '', cleanUrls = false, isPageNotFound = false) {
110
125
  let url = removeBase(rawUrl, base);
@@ -117,10 +132,10 @@ function replaceVersion(rawUrl, version, base = '', cleanUrls = false, isPageNot
117
132
  } else parts.shift();
118
133
  let restPart = parts.join('/') || '';
119
134
  if (versionPart && !restPart) restPart = cleanUrls ? 'index' : 'index.html';
120
- return withBase(addLeadingSlash([
135
+ return addLeadingSlash([
121
136
  versionPart,
122
137
  restPart
123
- ].filter(Boolean).join('/')), base);
138
+ ].filter(Boolean).join('/'));
124
139
  }
125
140
  const parseUrl = (url)=>{
126
141
  const [withoutHash, hash = ''] = url.split('#');
@@ -145,9 +160,6 @@ function withoutLang(path, langs) {
145
160
  const langRegexp = new RegExp(`^\\/(${langs.join('|')})`);
146
161
  return addLeadingSlash(path.replace(langRegexp, ''));
147
162
  }
148
- function withoutBase(path, base) {
149
- return addLeadingSlash(path).replace(normalizeSlash(base), '');
150
- }
151
163
  function withBase(url, base) {
152
164
  const normalizedUrl = addLeadingSlash(url);
153
165
  const normalizedBase = normalizeSlash(base);
@@ -156,21 +168,4 @@ function withBase(url, base) {
156
168
  function removeBase(url, base) {
157
169
  return addLeadingSlash(url).replace(new RegExp(`^${normalizeSlash(base)}`), '');
158
170
  }
159
- const matchSidebar = (pattern, currentPathname, base)=>{
160
- const prefix = withBase(pattern, base);
161
- if (prefix === currentPathname) return true;
162
- const prefixWithTrailingSlash = addTrailingSlash(prefix);
163
- if (currentPathname.startsWith(prefixWithTrailingSlash)) return true;
164
- const prefixWithDot = `${prefix}.`;
165
- return currentPathname.startsWith(prefixWithDot);
166
- };
167
- const getSidebarDataGroup = (sidebar, currentPathname, base)=>{
168
- const navRoutes = Object.keys(sidebar).sort((a, b)=>b.length - a.length);
169
- for (const name of navRoutes)if (matchSidebar(name, currentPathname, base)) {
170
- const sidebarGroup = sidebar[name];
171
- return sidebarGroup;
172
- }
173
- return [];
174
- };
175
- const matchNavbar = (item, currentPathname, base)=>new RegExp(item.activeMatch || item.link).test(withoutBase(currentPathname, base));
176
- export { APPEARANCE_KEY, DEFAULT_HIGHLIGHT_LANGUAGES, HASH_REGEXP, MDX_OR_MD_REGEXP, QUERY_REGEXP, RSPRESS_TEMP_DIR, SEARCH_INDEX_NAME, addLeadingSlash, addTrailingSlash, utils_cleanUrl as cleanUrl, getSidebarDataGroup, inBrowser, isDataUrl, isDebugMode, isDevDebugMode, isExternalUrl, isProduction, isSCM, matchNavbar, matchSidebar, normalizeHref, normalizePosixPath, normalizeSlash, parseUrl, removeBase, removeHash, removeLeadingSlash, removeTrailingSlash, replaceLang, replaceVersion, slash, withBase, withoutBase, withoutLang };
171
+ export { APPEARANCE_KEY, DEFAULT_HIGHLIGHT_LANGUAGES, HASH_REGEXP, MDX_OR_MD_REGEXP, QUERY_REGEXP, RSPRESS_TEMP_DIR, SEARCH_INDEX_NAME, addLeadingSlash, addTrailingSlash, utils_cleanUrl as cleanUrl, getSidebarDataGroup, inBrowser, isDataUrl, isDebugMode, isDevDebugMode, isExternalUrl, isProduction, isSCM, matchNavbar, matchSidebar, normalizeHref, normalizePosixPath, normalizeSlash, parseUrl, removeBase, removeHash, removeLeadingSlash, removeTrailingSlash, replaceLang, replaceVersion, slash, withBase, withoutLang };
package/dist/logger.js CHANGED
@@ -1,3 +1,2 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__ from "@rsbuild/core";
2
- var __webpack_exports__logger = __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger;
3
- export { __webpack_exports__logger as logger };
1
+ import { logger } from "@rsbuild/core";
2
+ export { logger };
@@ -4,6 +4,7 @@ import type { MdxJsxExpressionAttribute } from 'mdast-util-mdx-jsx';
4
4
  import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
5
5
  import type { MdxJsxTextElement } from 'mdast-util-mdx-jsx';
6
6
  import type { PluggableList } from 'unified';
7
+ import type { RehypeShikiOptions } from '@shikijs/rehype';
7
8
  import type { RsbuildConfig } from '@rsbuild/core';
8
9
  import type { RsbuildPlugin } from '@rsbuild/core';
9
10
 
@@ -131,7 +132,7 @@ declare interface Config {
131
132
  * Whether to redirect to the closest locale when the user visits the site
132
133
  * @default 'auto'
133
134
  */
134
- localeRedirect?: 'auto' | 'never';
135
+ localeRedirect?: 'auto' | 'never' | 'only-default-lang';
135
136
  /**
136
137
  * Whether to show the fallback heading title when the heading title is not presented but `frontmatter.title` exists
137
138
  * @default true
@@ -207,6 +208,18 @@ declare interface FrontMatterMeta {
207
208
  [key: string]: unknown;
208
209
  }
209
210
 
211
+ /**
212
+ * Transform `config.icon` into final url path in the web app
213
+ *
214
+ * @param icon original icon in config
215
+ * @returns final url path in the web app
216
+ */
217
+ export declare function getIconUrlPath(icon: '' | undefined): undefined;
218
+
219
+ export declare function getIconUrlPath(icon: string | URL): string;
220
+
221
+ export declare function getIconUrlPath(icon: string | URL | undefined): string | undefined;
222
+
210
223
  export declare function getNodeAttribute(node: MdxJsxFlowElement | MdxJsxTextElement, attrName: string, attribute?: false): string | MdxJsxAttributeValueExpression | null | undefined;
211
224
 
212
225
  export declare function getNodeAttribute(node: MdxJsxFlowElement | MdxJsxTextElement, attrName: string, attribute: true): MdxJsxAttribute | MdxJsxExpressionAttribute | undefined;
@@ -245,6 +258,7 @@ declare interface Hero {
245
258
  export declare function loadFrontMatter<TFrontmatter extends Record<string, unknown> = FrontMatterMeta>(source: string, filepath: string, root: string, outputWarning?: boolean): {
246
259
  frontmatter: TFrontmatter;
247
260
  content: string;
261
+ emptyLinesSource: string;
248
262
  };
249
263
 
250
264
  declare interface Locale {
@@ -292,7 +306,7 @@ declare type LocalSearchOptions = SearchHooks & {
292
306
  versioned?: boolean;
293
307
  /**
294
308
  * If enabled, the search index will include code block content, which allows users to search code blocks.
295
- * @default false
309
+ * @default true
296
310
  */
297
311
  codeBlocks?: boolean;
298
312
  };
@@ -314,13 +328,14 @@ declare interface MarkdownOptions {
314
328
  */
315
329
  globalComponents?: string[];
316
330
  /**
317
- * Code highlighter, default is prism for performance reason
331
+ * @type import('@shikijs/rehype').RehypeShikiOptions
318
332
  */
319
- codeHighlighter?: 'prism' | 'shiki';
333
+ shiki?: Partial<PluginShikiOptions>;
320
334
  /**
321
- * Register prism languages
335
+ * Speed up build time by caching mdx parsing result in `rspress build`
336
+ * @default true
322
337
  */
323
- highlightLanguages?: (string | [string, string])[];
338
+ crossCompilerCache?: boolean;
324
339
  }
325
340
 
326
341
  export declare const mergeDocConfig: (...configs: UserConfig[]) => Promise<UserConfig>;
@@ -361,9 +376,8 @@ declare interface NavItemWithLinkAndChildren {
361
376
  * and should not be used in the runtime (usePageData).
362
377
  */
363
378
  declare interface PageIndexInfo {
364
- id: number;
365
- title: string;
366
379
  routePath: string;
380
+ title: string;
367
381
  toc: Header[];
368
382
  content: string;
369
383
  _flattenContent?: string;
@@ -378,6 +392,8 @@ declare interface PageIndexInfo {
378
392
 
379
393
  declare type PageType = 'home' | 'doc' | 'custom' | '404' | 'blank';
380
394
 
395
+ declare type PluginShikiOptions = RehypeShikiOptions;
396
+
381
397
  declare type RemoteSearchIndexInfo = string | {
382
398
  value: string;
383
399
  label: string;
@@ -476,6 +492,7 @@ declare interface RspressPlugin {
476
492
  addPages?: (config: UserConfig, isProd: boolean) => AdditionalPage[] | Promise<AdditionalPage[]>;
477
493
  /**
478
494
  * Add runtime modules
495
+ * @deprecated use [rsbuild-plugin-virtual-module](https://github.com/rspack-contrib/rsbuild-plugin-virtual-module) instead.
479
496
  */
480
497
  addRuntimeModules?: (config: UserConfig, isProd: boolean) => Record<string, string> | Promise<Record<string, string>>;
481
498
  /**
@@ -522,7 +539,7 @@ declare interface SidebarGroup {
522
539
  text: string;
523
540
  link?: string;
524
541
  tag?: string;
525
- items: (SidebarItem | SidebarDivider | SidebarGroup | string)[];
542
+ items: (SidebarGroup | SidebarItem | SidebarDivider | SidebarSectionHeader)[];
526
543
  collapsible?: boolean;
527
544
  collapsed?: boolean;
528
545
  /**
@@ -586,7 +603,7 @@ declare interface UserConfig<ThemeConfig = Config> {
586
603
  /**
587
604
  * Path to html icon file.
588
605
  */
589
- icon?: string;
606
+ icon?: string | URL;
590
607
  /**
591
608
  * Default language of the site.
592
609
  */
@@ -1,36 +1,54 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
2
- import * as __WEBPACK_EXTERNAL_MODULE_gray_matter_90c6cd0b__ from "gray-matter";
3
- import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__ from "@rsbuild/core";
1
+ import node_path from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ import gray_matter from "gray-matter";
4
+ import { logger } from "@rsbuild/core";
4
5
  const extractTextAndId = (title)=>{
5
6
  if (!title) return [
6
7
  '',
7
8
  ''
8
9
  ];
9
10
  const customIdReg = /\\?{#.*}/;
10
- const text = title.replace(customIdReg, '').trimEnd();
11
- const customId = title.match(customIdReg)?.[0]?.slice(2, -1) || '';
11
+ if (customIdReg.test(title)) {
12
+ const text = title.replace(customIdReg, '').trimEnd();
13
+ const customId = title.match(customIdReg)?.[0]?.slice(2, -1) || '';
14
+ return [
15
+ text,
16
+ customId
17
+ ];
18
+ }
12
19
  return [
13
- text,
14
- customId
20
+ title,
21
+ ''
15
22
  ];
16
23
  };
24
+ function getIconUrlPath(icon) {
25
+ if (!icon) return;
26
+ icon = icon.toString();
27
+ if (icon.startsWith('file://')) icon = fileURLToPath(icon);
28
+ if (!node_path.isAbsolute(icon)) return icon;
29
+ return `/${node_path.basename(icon)}`;
30
+ }
17
31
  function getNodeAttribute(node, attrName, attribute) {
18
32
  const found = node.attributes.find((attr)=>'name' in attr && attr.name === attrName);
19
33
  return attribute ? found : found?.value;
20
34
  }
21
35
  function loadFrontMatter(source, filepath, root, outputWarning = false) {
22
36
  try {
23
- const { content, data } = (0, __WEBPACK_EXTERNAL_MODULE_gray_matter_90c6cd0b__["default"])(source);
37
+ const { content, data } = gray_matter(source);
38
+ const rawFrontMatter = source.slice(0, source.length - content.length);
39
+ const emptyLinesSource = rawFrontMatter.length ? `${rawFrontMatter.replace(/[^\n]/g, '')}${content}` : content;
24
40
  return {
25
41
  content,
26
- frontmatter: data
42
+ frontmatter: data,
43
+ emptyLinesSource
27
44
  };
28
45
  } catch (e) {
29
- if (outputWarning) __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.warn(`Parse frontmatter error in ${__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].relative(root, filepath)}: \n`, e);
46
+ if (outputWarning) logger.warn(`Parse frontmatter error in ${node_path.relative(root, filepath)}: \n`, e);
30
47
  }
31
48
  return {
32
49
  content: '',
33
- frontmatter: {}
50
+ frontmatter: {},
51
+ emptyLinesSource: source
34
52
  };
35
53
  }
36
54
  const castArray = (value)=>Array.isArray(value) ? value : [
@@ -50,4 +68,4 @@ const mergeDocConfig = async (...configs)=>{
50
68
  ];
51
69
  });
52
70
  };
53
- export { extractTextAndId, getNodeAttribute, loadFrontMatter, mergeDocConfig };
71
+ export { extractTextAndId, getIconUrlPath, getNodeAttribute, loadFrontMatter, mergeDocConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress/shared",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.20",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/web-infra-dev/rspress.git",
@@ -32,21 +32,22 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@rsbuild/core": "1.3.9",
35
+ "@rsbuild/core": "~1.4.3",
36
+ "@shikijs/rehype": "^3.4.2",
36
37
  "gray-matter": "4.0.3",
37
38
  "lodash-es": "^4.17.21",
38
39
  "unified": "^11.0.5"
39
40
  },
40
41
  "devDependencies": {
41
- "@rslib/core": "0.6.5",
42
+ "@rslib/core": "0.10.4",
42
43
  "@types/jest": "~29.5.14",
43
44
  "@types/lodash-es": "^4.17.12",
44
- "@types/node": "^18.11.17",
45
- "@types/react": "^18.3.20",
45
+ "@types/node": "^22.8.1",
46
+ "@types/react": "^19.1.8",
46
47
  "mdast-util-mdx-jsx": "^3.2.0",
47
48
  "medium-zoom": "1.1.0",
48
49
  "rimraf": "^6.0.1",
49
- "rsbuild-plugin-publint": "^0.3.0",
50
+ "rsbuild-plugin-publint": "^0.3.2",
50
51
  "typescript": "^5.8.2"
51
52
  },
52
53
  "publishConfig": {