@rspress/core 2.0.0-beta.13 → 2.0.0-beta.15

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/loader.d.ts CHANGED
@@ -6,6 +6,13 @@ import type { Rspack as Rspack_2 } from '@rsbuild/core';
6
6
  import type { RspressPlugin } from '@rspress/shared';
7
7
  import type { UserConfig } from '@rspress/shared';
8
8
 
9
+ declare interface InitOptions {
10
+ scanDir: string;
11
+ config: UserConfig;
12
+ runtimeTempDir: string;
13
+ pluginDriver: PluginDriver;
14
+ }
15
+
9
16
  declare function mdxLoader(this: Rspack_2.LoaderContext<MdxLoaderOptions>, source: string): Promise<void>;
10
17
  export default mdxLoader;
11
18
 
@@ -45,17 +52,23 @@ declare class PluginDriver {
45
52
  _runSerialAsyncHook<H extends RspressPluginHookKeys>(hookName: H, ...args: Parameters<Required<RspressPlugin>[H]>): RspressPlugin;
46
53
  }
47
54
 
55
+ declare class RoutePage {
56
+ routeMeta: RouteMeta;
57
+ static create(routeMeta: RouteMeta): RoutePage;
58
+ constructor(routeMeta: RouteMeta);
59
+ }
60
+
48
61
  declare class RouteService {
49
62
  #private;
50
- routeData: Map<string, RouteMeta>;
63
+ routeData: Map<string, RoutePage>;
64
+ static create(options: InitOptions): Promise<RouteService>;
51
65
  constructor(scanDir: string, userConfig: UserConfig, tempDir: string, pluginDriver: PluginDriver);
52
66
  get extensions(): readonly string[];
53
- init(): Promise<void>;
54
- addRoute(routeInfo: RouteMeta): void;
67
+ addRoute(routeMeta: RouteMeta): Promise<void>;
55
68
  removeRoute(filePath: string): void;
56
69
  getRoutes(): RouteMeta[];
70
+ getRoutePages(): RoutePage[];
57
71
  isExistRoute(routePath: string): boolean;
58
- isEmpty(): boolean;
59
72
  generateRoutesCode(): string;
60
73
  generateRoutesCodeByRouteMeta(routeMeta: RouteMeta[]): string;
61
74
  getRoutePathParts(routePath: string): readonly [string, string, string];
@@ -64,6 +77,7 @@ declare class RouteService {
64
77
  lang: string;
65
78
  version: string;
66
79
  };
80
+ getRoutePageByRoutePath(routePath: string): RoutePage | undefined;
67
81
  }
68
82
 
69
83
  declare type RspressPluginHookKeys = 'beforeBuild' | 'afterBuild' | 'addPages' | 'addRuntimeModules' | 'routeGenerated' | 'routeServiceGenerated' | 'addSSGRoutes' | 'extendPageData' | 'modifySearchIndexData';
@@ -12,13 +12,20 @@ declare interface CompileOptions {
12
12
  filepath: string;
13
13
  checkDeadLinks: boolean;
14
14
  docDirectory: string;
15
- config: UserConfig;
16
- routeService: RouteService;
17
- pluginDriver: PluginDriver;
15
+ config: UserConfig | null;
16
+ routeService: RouteService | null;
17
+ pluginDriver: PluginDriver | null;
18
18
  }
19
19
 
20
20
  export declare function compileWithCrossCompilerCache(options: CompileOptions): Promise<string>;
21
21
 
22
+ declare interface InitOptions {
23
+ scanDir: string;
24
+ config: UserConfig;
25
+ runtimeTempDir: string;
26
+ pluginDriver: PluginDriver;
27
+ }
28
+
22
29
  declare class PluginDriver {
23
30
  #private;
24
31
  constructor(config: UserConfig, isProd: boolean);
@@ -47,17 +54,23 @@ declare class PluginDriver {
47
54
  _runSerialAsyncHook<H extends RspressPluginHookKeys>(hookName: H, ...args: Parameters<Required<RspressPlugin>[H]>): RspressPlugin;
48
55
  }
49
56
 
57
+ declare class RoutePage {
58
+ routeMeta: RouteMeta;
59
+ static create(routeMeta: RouteMeta): RoutePage;
60
+ constructor(routeMeta: RouteMeta);
61
+ }
62
+
50
63
  declare class RouteService {
51
64
  #private;
52
- routeData: Map<string, RouteMeta>;
65
+ routeData: Map<string, RoutePage>;
66
+ static create(options: InitOptions): Promise<RouteService>;
53
67
  constructor(scanDir: string, userConfig: UserConfig, tempDir: string, pluginDriver: PluginDriver);
54
68
  get extensions(): readonly string[];
55
- init(): Promise<void>;
56
- addRoute(routeInfo: RouteMeta): void;
69
+ addRoute(routeMeta: RouteMeta): Promise<void>;
57
70
  removeRoute(filePath: string): void;
58
71
  getRoutes(): RouteMeta[];
72
+ getRoutePages(): RoutePage[];
59
73
  isExistRoute(routePath: string): boolean;
60
- isEmpty(): boolean;
61
74
  generateRoutesCode(): string;
62
75
  generateRoutesCodeByRouteMeta(routeMeta: RouteMeta[]): string;
63
76
  getRoutePathParts(routePath: string): readonly [string, string, string];
@@ -66,6 +79,7 @@ declare class RouteService {
66
79
  lang: string;
67
80
  version: string;
68
81
  };
82
+ getRoutePageByRoutePath(routePath: string): RoutePage | undefined;
69
83
  }
70
84
 
71
85
  declare type RspressPluginHookKeys = 'beforeBuild' | 'afterBuild' | 'addPages' | 'addRuntimeModules' | 'routeGenerated' | 'routeServiceGenerated' | 'addSSGRoutes' | 'extendPageData' | 'modifySearchIndexData';
package/dist/processor.js CHANGED
@@ -1,19 +1,21 @@
1
1
  import __rslib_shim_module__ from 'module';
2
2
  /*#__PURE__*/ import.meta.url;
3
- import external_node_path_default from "node:path";
3
+ import node_path from "node:path";
4
4
  import { createProcessor, nodeTypes } from "@mdx-js/mdx";
5
5
  import { logger } from "@rspress/shared/logger";
6
6
  import { extractTextAndId, getNodeAttribute, loadFrontMatter } from "@rspress/shared/node-utils";
7
- import external_node_os_default from "node:os";
8
- import external_rehype_external_links_default from "rehype-external-links";
9
- import external_rehype_raw_default from "rehype-raw";
10
- import external_remark_gfm_default from "remark-gfm";
11
- import external_github_slugger_default from "github-slugger";
7
+ import node_os from "node:os";
8
+ import rehype_external_links from "rehype-external-links";
9
+ import rehype_raw from "rehype-raw";
10
+ import remark_gfm from "remark-gfm";
11
+ import github_slugger from "github-slugger";
12
12
  import { headingRank } from "hast-util-heading-rank";
13
13
  import { visit } from "unist-util-visit";
14
14
  import { addLeadingSlash, cleanUrl, isExternalUrl, isProduction, normalizeHref, parseUrl, slash as shared_slash } from "@rspress/shared";
15
15
  import { DEFAULT_PAGE_EXTENSIONS } from "@rspress/shared/constants";
16
16
  import { visitChildren } from "unist-util-visit-children";
17
+ import rehype from "@shikijs/rehype";
18
+ import { createCssVariablesTheme } from "shiki";
17
19
  function applyReplaceRules(code = '', replaceRules = []) {
18
20
  let result = code;
19
21
  for (const rule of replaceRules)result = result.replace(rule.search, rule.replace);
@@ -23,15 +25,15 @@ function escapeMarkdownHeadingIds(content) {
23
25
  const markdownHeadingRegexp = /(?:^|\n)#{1,6}(?!#).*/g;
24
26
  return content.replace(markdownHeadingRegexp, (substring)=>substring.replace('{#', '\\{#').replace('\\\\{#', '\\{#'));
25
27
  }
26
- const isWindows = 'win32' === external_node_os_default.platform();
28
+ const isWindows = 'win32' === node_os.platform();
27
29
  function slash(p) {
28
30
  return p.replace(/\\/g, '/');
29
31
  }
30
32
  function normalizePath(id) {
31
- return external_node_path_default.posix.normalize(isWindows ? slash(id) : id);
33
+ return node_path.posix.normalize(isWindows ? slash(id) : id);
32
34
  }
33
35
  const rehypeHeaderAnchor = ()=>{
34
- const slugger = new external_github_slugger_default();
36
+ const slugger = new github_slugger();
35
37
  return (tree)=>{
36
38
  visit(tree, 'element', (node)=>{
37
39
  if (!headingRank(node)) return;
@@ -110,7 +112,7 @@ const getASTNodeImport = (name, from)=>({
110
112
  });
111
113
  const remarkBuiltin = ({ globalComponents })=>(tree)=>{
112
114
  const demos = globalComponents.map((componentPath)=>{
113
- const filename = external_node_path_default.parse(componentPath).name;
115
+ const filename = node_path.parse(componentPath).name;
114
116
  const componentName = filename[0].toUpperCase() + filename.slice(1);
115
117
  return getASTNodeImport(componentName, componentPath);
116
118
  });
@@ -120,7 +122,7 @@ const IGNORE_REGEXP = /^(https?|mailto|tel|#)/;
120
122
  function checkLinks(links, filepath, root, routeService) {
121
123
  const errorInfos = [];
122
124
  links.filter((link)=>!IGNORE_REGEXP.test(link)).map((link)=>normalizePath(link)).forEach((link)=>{
123
- const relativePath = external_node_path_default.relative(root, filepath);
125
+ const relativePath = node_path.relative(root, filepath);
124
126
  if (!routeService.isExistRoute(cleanUrl(link))) errorInfos.push(`Internal link to "${link}" is dead, check it in "${relativePath}"`);
125
127
  });
126
128
  if (errorInfos.length > 0) {
@@ -138,7 +140,7 @@ const remarkCheckDeadLinks = (checkLink)=>{
138
140
  if (!url) return;
139
141
  if (internalLinks.has(url)) return;
140
142
  if (!url.startsWith('http://') && !url.startsWith('https://')) {
141
- const { routePath: normalizeUrl } = routeService.normalizeRoutePath(url.split(external_node_path_default.sep).join('/')?.split('#')[0]);
143
+ const { routePath: normalizeUrl } = routeService.normalizeRoutePath(url.split(node_path.sep).join('/')?.split('#')[0]);
142
144
  internalLinks.add(normalizeUrl);
143
145
  }
144
146
  });
@@ -150,9 +152,9 @@ function normalizeLink(nodeUrl, routeService, relativePath, cleanUrls) {
150
152
  if (nodeUrl.startsWith('#')) return `#${nodeUrl.slice(1)}`;
151
153
  let { url, hash } = parseUrl(nodeUrl);
152
154
  if (isExternalUrl(url)) return url + (hash ? `#${hash}` : '');
153
- const extname = external_node_path_default.extname(url);
155
+ const extname = node_path.extname(url);
154
156
  if ((routeService?.extensions ?? DEFAULT_PAGE_EXTENSIONS).includes(extname)) url = url.replace(new RegExp(`\\${extname}$`), '');
155
- if (url.startsWith('.')) url = external_node_path_default.posix.join(shared_slash(external_node_path_default.dirname(relativePath)), url);
157
+ if (url.startsWith('.')) url = node_path.posix.join(shared_slash(node_path.dirname(relativePath)), url);
156
158
  else if (routeService) {
157
159
  const [pathVersion, pathLang] = routeService.getRoutePathParts(shared_slash(relativePath));
158
160
  const [urlVersion, urlLang, urlPath] = routeService.getRoutePathParts(url);
@@ -176,12 +178,12 @@ const remarkPluginNormalizeLink = ({ root, cleanUrls, routeService })=>(tree, fi
176
178
  const images = [];
177
179
  visit(tree, 'link', (node)=>{
178
180
  const { url: nodeUrl } = node;
179
- const relativePath = external_node_path_default.relative(root, file.path);
181
+ const relativePath = node_path.relative(root, file.path);
180
182
  node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
181
183
  });
182
184
  visit(tree, 'definition', (node)=>{
183
185
  const { url: nodeUrl } = node;
184
- const relativePath = external_node_path_default.relative(root, file.path);
186
+ const relativePath = node_path.relative(root, file.path);
185
187
  node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
186
188
  });
187
189
  const getMdxSrcAttribute = (tempVar)=>({
@@ -243,7 +245,7 @@ const remarkPluginNormalizeLink = ({ root, cleanUrls, routeService })=>(tree, fi
243
245
  const parseToc = (tree)=>{
244
246
  let title = '';
245
247
  const toc = [];
246
- const slugger = new external_github_slugger_default();
248
+ const slugger = new github_slugger();
247
249
  visitChildren((node)=>{
248
250
  if ('heading' !== node.type || !node.depth || !node.children) return;
249
251
  if (node.depth >= 1 && node.depth < 5) {
@@ -295,12 +297,75 @@ const rehypeCodeMeta = ()=>(tree)=>{
295
297
  }
296
298
  });
297
299
  };
300
+ const SHIKI_TRANSFORMER_LINE_NUMBER = 'shiki-transformer:line-number';
301
+ function transformerLineNumber(options = {}) {
302
+ const { classActiveLine = 'line-number', classActivePre = 'has-line-number' } = options;
303
+ return {
304
+ name: SHIKI_TRANSFORMER_LINE_NUMBER,
305
+ pre (pre) {
306
+ return this.addClassToHast(pre, classActivePre);
307
+ },
308
+ line (node) {
309
+ this.addClassToHast(node, classActiveLine);
310
+ }
311
+ };
312
+ }
313
+ const SHIKI_TRANSFORMER_ADD_TITLE = 'shiki-transformer:add-title';
314
+ function parseTitleFromMeta(meta) {
315
+ if (!meta) return '';
316
+ let result = meta;
317
+ const highlightReg = /{[\d,-]*}/i;
318
+ const highlightMeta = highlightReg.exec(meta)?.[0];
319
+ if (highlightMeta) result = meta.replace(highlightReg, '').trim();
320
+ result = result.split('=')[1] ?? '';
321
+ return result?.replace(/["'`]/g, '');
322
+ }
323
+ function transformerAddTitle() {
324
+ return {
325
+ name: SHIKI_TRANSFORMER_ADD_TITLE,
326
+ pre (pre) {
327
+ const title = parseTitleFromMeta(this.options.meta?.__raw);
328
+ if (title.length > 0) pre.properties = {
329
+ ...pre.properties,
330
+ title
331
+ };
332
+ return pre;
333
+ }
334
+ };
335
+ }
336
+ const cssVariablesTheme = createCssVariablesTheme({
337
+ name: 'css-variables',
338
+ variablePrefix: '--shiki-',
339
+ variableDefaults: {},
340
+ fontStyle: true
341
+ });
342
+ function createRehypeShikiOptions(showLineNumbers, options) {
343
+ const { transformers = [], ...restOptions } = options || {};
344
+ const newTransformers = [
345
+ transformerAddTitle(),
346
+ ...transformers
347
+ ];
348
+ if (showLineNumbers && !newTransformers.some((transformerItem)=>transformerItem.name === SHIKI_TRANSFORMER_LINE_NUMBER)) newTransformers.push(transformerLineNumber());
349
+ return {
350
+ theme: cssVariablesTheme,
351
+ defaultLanguage: 'txt',
352
+ lazy: true,
353
+ langs: [
354
+ 'tsx',
355
+ 'ts',
356
+ 'js'
357
+ ],
358
+ ...restOptions,
359
+ addLanguageClass: true,
360
+ transformers: newTransformers
361
+ };
362
+ }
298
363
  async function createMDXOptions(options) {
299
364
  const { docDirectory, config, checkDeadLinks, routeService, filepath, pluginDriver } = options;
300
- const format = external_node_path_default.extname(filepath).slice(1);
365
+ const format = node_path.extname(filepath).slice(1);
301
366
  const cleanUrls = config?.route?.cleanUrls ?? false;
302
- const { remarkPlugins: remarkPluginsFromConfig = [], rehypePlugins: rehypePluginsFromConfig = [], globalComponents: globalComponentsFromConfig = [] } = config?.markdown || {};
303
- const rspressPlugins = pluginDriver.getPlugins();
367
+ const { remarkPlugins: remarkPluginsFromConfig = [], rehypePlugins: rehypePluginsFromConfig = [], globalComponents: globalComponentsFromConfig = [], showLineNumbers = false, shiki } = config?.markdown || {};
368
+ const rspressPlugins = pluginDriver?.getPlugins() ?? [];
304
369
  const remarkPluginsFromPlugins = rspressPlugins.flatMap((plugin)=>plugin.markdown?.remarkPlugins || []);
305
370
  const rehypePluginsFromPlugins = rspressPlugins.flatMap((plugin)=>plugin.markdown?.rehypePlugins || []);
306
371
  const globalComponents = [
@@ -311,7 +376,7 @@ async function createMDXOptions(options) {
311
376
  providerImportSource: '@mdx-js/react',
312
377
  format,
313
378
  remarkPlugins: [
314
- external_remark_gfm_default,
379
+ remark_gfm,
315
380
  remarkPluginToc,
316
381
  [
317
382
  remarkPluginNormalizeLink,
@@ -321,7 +386,7 @@ async function createMDXOptions(options) {
321
386
  routeService
322
387
  }
323
388
  ],
324
- checkDeadLinks && [
389
+ checkDeadLinks && routeService && [
325
390
  remarkCheckDeadLinks,
326
391
  {
327
392
  root: docDirectory,
@@ -343,14 +408,18 @@ async function createMDXOptions(options) {
343
408
  ...'md' === format ? [
344
409
  rehypeCodeMeta,
345
410
  [
346
- external_rehype_raw_default,
411
+ rehype_raw,
347
412
  {
348
413
  passThrough: nodeTypes
349
414
  }
350
415
  ]
351
416
  ] : [],
352
417
  [
353
- external_rehype_external_links_default,
418
+ rehype,
419
+ createRehypeShikiOptions(showLineNumbers, shiki)
420
+ ],
421
+ [
422
+ rehype_external_links,
354
423
  {
355
424
  target: '_blank',
356
425
  rel: 'noopener noreferrer'
@@ -363,7 +432,6 @@ async function createMDXOptions(options) {
363
432
  }
364
433
  async function compile(options) {
365
434
  const { source, filepath, docDirectory, checkDeadLinks, config, routeService, pluginDriver } = options;
366
- config.root;
367
435
  const mdxOptions = await createMDXOptions({
368
436
  checkDeadLinks,
369
437
  config,
@@ -373,7 +441,8 @@ async function compile(options) {
373
441
  routeService
374
442
  });
375
443
  const { frontmatter, emptyLinesSource } = loadFrontMatter(source, filepath, docDirectory, true);
376
- const replacedSource = applyReplaceRules(emptyLinesSource, config.replaceRules);
444
+ const { replaceRules } = config ?? {};
445
+ const replacedSource = applyReplaceRules(emptyLinesSource, replaceRules);
377
446
  const preprocessedContent = escapeMarkdownHeadingIds(replacedSource);
378
447
  try {
379
448
  let pageMeta = {
@@ -404,7 +473,7 @@ async function compile(options) {
404
473
  ${compileResult}
405
474
  MDXContent.__RSPRESS_PAGE_META = {};
406
475
 
407
- MDXContent.__RSPRESS_PAGE_META["${encodeURIComponent(normalizePath(external_node_path_default.relative(docDirectory, filepath)))}"] = ${JSON.stringify(pageMeta)};
476
+ MDXContent.__RSPRESS_PAGE_META["${encodeURIComponent(normalizePath(node_path.relative(docDirectory, filepath)))}"] = ${JSON.stringify(pageMeta)};
408
477
  `;
409
478
  return result;
410
479
  } catch (e) {
@@ -1,8 +1,8 @@
1
1
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
2
  import { DataContext, useLocation } from "@rspress/runtime";
3
3
  import { Layout } from "@theme";
4
- import external_react_default, { useContext, useLayoutEffect } from "react";
5
- import external_virtual_global_components_default from "virtual-global-components";
4
+ import react, { useContext, useLayoutEffect } from "react";
5
+ import virtual_global_components from "virtual-global-components";
6
6
  import { initPageData } from "./initPageData.js";
7
7
  function App() {
8
8
  const { setData: setPageData, data } = useContext(DataContext);
@@ -29,15 +29,15 @@ function App() {
29
29
  return /*#__PURE__*/ jsxs(Fragment, {
30
30
  children: [
31
31
  /*#__PURE__*/ jsx(Layout, {}),
32
- !hideGlobalUIComponents && external_virtual_global_components_default.map((componentInfo, index)=>{
32
+ !hideGlobalUIComponents && virtual_global_components.map((componentInfo, index)=>{
33
33
  if (Array.isArray(componentInfo)) {
34
34
  const [component, props] = componentInfo;
35
- return /*#__PURE__*/ external_react_default.createElement(component, {
35
+ return /*#__PURE__*/ react.createElement(component, {
36
36
  key: index,
37
37
  ...props
38
38
  });
39
39
  }
40
- return /*#__PURE__*/ external_react_default.createElement(componentInfo, {
40
+ return /*#__PURE__*/ react.createElement(componentInfo, {
41
41
  key: index
42
42
  });
43
43
  })
@@ -1,18 +1,18 @@
1
1
  import { isEqualPath, pathnameToRouteService } from "@rspress/runtime";
2
2
  import { MDX_OR_MD_REGEXP, cleanUrl } from "@rspress/shared";
3
- import external_virtual_site_data_default from "virtual-site-data";
3
+ import virtual_site_data from "virtual-site-data";
4
4
  async function initPageData(routePath) {
5
5
  const matchedRoute = pathnameToRouteService(routePath);
6
6
  if (matchedRoute) {
7
7
  const mod = await matchedRoute.preload();
8
8
  const pagePath = cleanUrl(matchedRoute.filePath);
9
9
  const normalize = (p)=>p.replace(/\/$/, '').toLowerCase();
10
- const extractPageInfo = external_virtual_site_data_default.pages.find((page)=>isEqualPath(normalize(page.routePath), normalize(matchedRoute.path)));
10
+ const extractPageInfo = virtual_site_data.pages.find((page)=>isEqualPath(normalize(page.routePath), normalize(matchedRoute.path)));
11
11
  const encodedPagePath = encodeURIComponent(pagePath);
12
12
  const meta = mod.default.__RSPRESS_PAGE_META?.[encodedPagePath] || {};
13
13
  const { toc = [], title = '', frontmatter = {}, ...rest } = MDX_OR_MD_REGEXP.test(matchedRoute.filePath) ? meta : mod;
14
14
  return {
15
- siteData: external_virtual_site_data_default,
15
+ siteData: virtual_site_data,
16
16
  page: {
17
17
  ...rest,
18
18
  pagePath,
@@ -24,21 +24,21 @@ async function initPageData(routePath) {
24
24
  }
25
25
  };
26
26
  }
27
- let lang = external_virtual_site_data_default.lang || '';
28
- let version = external_virtual_site_data_default.multiVersion?.default || '';
29
- if (external_virtual_site_data_default.lang && 'undefined' != typeof window) {
30
- const path = location.pathname.replace(external_virtual_site_data_default.base, '').split('/').slice(0, 2);
31
- if (external_virtual_site_data_default.locales.length) {
32
- const result = external_virtual_site_data_default.locales.find(({ lang })=>path.includes(lang));
27
+ let lang = virtual_site_data.lang || '';
28
+ let version = virtual_site_data.multiVersion?.default || '';
29
+ if (virtual_site_data.lang && 'undefined' != typeof window) {
30
+ const path = location.pathname.replace(virtual_site_data.base, '').split('/').slice(0, 2);
31
+ if (virtual_site_data.locales.length) {
32
+ const result = virtual_site_data.locales.find(({ lang })=>path.includes(lang));
33
33
  if (result) lang = result.lang;
34
34
  }
35
- if (external_virtual_site_data_default.multiVersion.versions) {
36
- const result = external_virtual_site_data_default.multiVersion.versions.find((version)=>path.includes(version));
35
+ if (virtual_site_data.multiVersion.versions) {
36
+ const result = virtual_site_data.multiVersion.versions.find((version)=>path.includes(version));
37
37
  if (result) version = result;
38
38
  }
39
39
  }
40
40
  return {
41
- siteData: external_virtual_site_data_default,
41
+ siteData: virtual_site_data,
42
42
  page: {
43
43
  pagePath: '',
44
44
  pageType: '404',
@@ -0,0 +1,38 @@
1
+ import type { ShikiTransformer } from 'shiki';
2
+
3
+ declare interface ITransformerLineNumberOptions {
4
+ classActivePre?: string;
5
+ classActiveLine?: string;
6
+ }
7
+
8
+ export declare const SHIKI_TRANSFORMER_ADD_TITLE = "shiki-transformer:add-title";
9
+
10
+ export declare const SHIKI_TRANSFORMER_LINE_NUMBER = "shiki-transformer:line-number";
11
+
12
+ export declare const SHIKI_TRANSFORMER_META_HIGHLIGHT = "shiki-transformer:compatible-meta-highlight";
13
+
14
+ export declare function transformerAddTitle(): ShikiTransformer;
15
+
16
+ /**
17
+ * Allow using `{1,3-5}` in the code snippet meta to mark highlighted lines.
18
+ * @deprecated This is a workaround when migrating to Rspress 2.0, you should migrate to `import { transformerNotationHighlight } from '@shikijs/transformers'` instead.
19
+ */
20
+ export declare function transformerCompatibleMetaHighlight(options?: TransformerMetaHighlightOptions): ShikiTransformer;
21
+
22
+ export declare function transformerLineNumber(options?: ITransformerLineNumberOptions): ShikiTransformer;
23
+
24
+ declare interface TransformerMetaHighlightOptions {
25
+ /**
26
+ * Class for highlighted lines
27
+ *
28
+ * @default 'highlighted'
29
+ */
30
+ classActiveLine?: string;
31
+ /**
32
+ *
33
+ * @default 'has-highlighted'
34
+ */
35
+ classActivePre?: string;
36
+ }
37
+
38
+ export { }
@@ -0,0 +1,79 @@
1
+ /*! For license information please see shiki-transformers.js.LICENSE.txt */
2
+ import __rslib_shim_module__ from 'module';
3
+ /*#__PURE__*/ import.meta.url;
4
+ const SHIKI_TRANSFORMER_LINE_NUMBER = 'shiki-transformer:line-number';
5
+ function transformerLineNumber(options = {}) {
6
+ const { classActiveLine = 'line-number', classActivePre = 'has-line-number' } = options;
7
+ return {
8
+ name: SHIKI_TRANSFORMER_LINE_NUMBER,
9
+ pre (pre) {
10
+ return this.addClassToHast(pre, classActivePre);
11
+ },
12
+ line (node) {
13
+ this.addClassToHast(node, classActiveLine);
14
+ }
15
+ };
16
+ }
17
+ const SHIKI_TRANSFORMER_ADD_TITLE = 'shiki-transformer:add-title';
18
+ function parseTitleFromMeta(meta) {
19
+ if (!meta) return '';
20
+ let result = meta;
21
+ const highlightReg = /{[\d,-]*}/i;
22
+ const highlightMeta = highlightReg.exec(meta)?.[0];
23
+ if (highlightMeta) result = meta.replace(highlightReg, '').trim();
24
+ result = result.split('=')[1] ?? '';
25
+ return result?.replace(/["'`]/g, '');
26
+ }
27
+ function transformerAddTitle() {
28
+ return {
29
+ name: SHIKI_TRANSFORMER_ADD_TITLE,
30
+ pre (pre) {
31
+ const title = parseTitleFromMeta(this.options.meta?.__raw);
32
+ if (title.length > 0) pre.properties = {
33
+ ...pre.properties,
34
+ title
35
+ };
36
+ return pre;
37
+ }
38
+ };
39
+ }
40
+ /**
41
+ * these codes are copied from @shiki/transformers, transformerMetaHighlight
42
+ * @source https://github.com/shikijs/shiki/blob/f5cf06f55a0b4643d7b02f15ee2c5033be6f1245/packages/transformers/src/transformers/meta-highlight.ts#L56
43
+ * @license MIT
44
+ */ function parseMetaHighlightString(meta) {
45
+ if (!meta) return null;
46
+ const match = meta.match(/\{([\d,-]+)\}/);
47
+ if (!match) return null;
48
+ const lines = match[1].split(',').flatMap((v)=>{
49
+ const num = v.split('-').map((v)=>Number.parseInt(v, 10));
50
+ if (1 === num.length) return [
51
+ num[0]
52
+ ];
53
+ return Array.from({
54
+ length: num[1] - num[0] + 1
55
+ }, (_, i)=>i + num[0]);
56
+ });
57
+ return lines;
58
+ }
59
+ const symbol = Symbol('highlighted-lines');
60
+ const SHIKI_TRANSFORMER_META_HIGHLIGHT = 'shiki-transformer:compatible-meta-highlight';
61
+ function transformerCompatibleMetaHighlight(options = {}) {
62
+ const { classActiveLine = 'highlighted', classActivePre = 'has-highlighted' } = options;
63
+ return {
64
+ name: '@shikijs/transformers:meta-highlight',
65
+ line (node, line) {
66
+ if (!this.options.meta?.__raw) return;
67
+ const meta = this.meta;
68
+ meta[symbol] ??= parseMetaHighlightString(this.options.meta.__raw);
69
+ const lines = meta[symbol] ?? [];
70
+ if (lines.includes(line)) this.addClassToHast(node, classActiveLine);
71
+ return node;
72
+ },
73
+ pre (hast) {
74
+ this.addClassToHast(hast, classActivePre);
75
+ return hast;
76
+ }
77
+ };
78
+ }
79
+ export { SHIKI_TRANSFORMER_ADD_TITLE, SHIKI_TRANSFORMER_LINE_NUMBER, SHIKI_TRANSFORMER_META_HIGHLIGHT, transformerAddTitle, transformerCompatibleMetaHighlight, transformerLineNumber };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * these codes are copied from @shiki/transformers, transformerMetaHighlight
3
+ * @source https://github.com/shikijs/shiki/blob/f5cf06f55a0b4643d7b02f15ee2c5033be6f1245/packages/transformers/src/transformers/meta-highlight.ts#L56
4
+ * @license MIT
5
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress/core",
3
- "version": "2.0.0-beta.13",
3
+ "version": "2.0.0-beta.15",
4
4
  "description": "The Rspress Documentation Framework",
5
5
  "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
6
  "repository": {
@@ -28,6 +28,10 @@
28
28
  "./theme": {
29
29
  "default": "./theme.ts"
30
30
  },
31
+ "./shiki-transformers": {
32
+ "types": "./dist/shiki-transformers.d.ts",
33
+ "default": "./dist/shiki-transformers.js"
34
+ },
31
35
  "./runtime": {
32
36
  "default": "./runtime.ts"
33
37
  }
@@ -48,6 +52,7 @@
48
52
  "@rsbuild/core": "~1.3.22",
49
53
  "@rsbuild/plugin-react": "~1.3.2",
50
54
  "@rspress/mdx-rs": "0.6.6",
55
+ "@shikijs/rehype": "^3.4.2",
51
56
  "@types/unist": "^3.0.3",
52
57
  "@unhead/react": "^2.0.10",
53
58
  "enhanced-resolve": "5.18.1",
@@ -67,22 +72,22 @@
67
72
  "remark": "^15.0.1",
68
73
  "remark-gfm": "^4.0.1",
69
74
  "rspack-plugin-virtual-module": "1.0.1",
75
+ "shiki": "^3.4.2",
70
76
  "tinyglobby": "^0.2.14",
71
77
  "unified": "^11.0.5",
72
78
  "unist-util-visit": "^5.0.0",
73
79
  "unist-util-visit-children": "^3.0.0",
74
- "@rspress/plugin-auto-nav-sidebar": "2.0.0-beta.13",
75
- "@rspress/plugin-container-syntax": "2.0.0-beta.13",
76
- "@rspress/plugin-medium-zoom": "2.0.0-beta.13",
77
- "@rspress/plugin-shiki": "2.0.0-beta.13",
78
- "@rspress/plugin-last-updated": "2.0.0-beta.13",
79
- "@rspress/runtime": "2.0.0-beta.13",
80
- "@rspress/shared": "2.0.0-beta.13",
81
- "@rspress/theme-default": "2.0.0-beta.13"
80
+ "@rspress/plugin-auto-nav-sidebar": "2.0.0-beta.15",
81
+ "@rspress/plugin-medium-zoom": "2.0.0-beta.15",
82
+ "@rspress/plugin-container-syntax": "2.0.0-beta.15",
83
+ "@rspress/plugin-last-updated": "2.0.0-beta.15",
84
+ "@rspress/runtime": "2.0.0-beta.15",
85
+ "@rspress/shared": "2.0.0-beta.15",
86
+ "@rspress/theme-default": "2.0.0-beta.15"
82
87
  },
83
88
  "devDependencies": {
84
89
  "@microsoft/api-extractor": "^7.52.8",
85
- "@rslib/core": "0.9.2",
90
+ "@rslib/core": "0.10.0",
86
91
  "@rspress-dev/plugin-preview": "link:../plugin-preview",
87
92
  "@types/hast": "^3.0.4",
88
93
  "@types/html-to-text": "^9.0.4",
@@ -90,11 +95,11 @@
90
95
  "@types/lodash-es": "^4.17.12",
91
96
  "@types/mdast": "^4.0.4",
92
97
  "@types/node": "^22.8.1",
93
- "@types/react": "^19.1.6",
98
+ "@types/react": "^19.1.8",
94
99
  "@types/react-dom": "^19.1.6",
95
100
  "memfs": "^4.17.2",
96
101
  "p-map": "^7.0.3",
97
- "postcss": "8.5.4",
102
+ "postcss": "8.5.5",
98
103
  "rehype-stringify": "^10.0.1",
99
104
  "remark-mdx": "^3.1.0",
100
105
  "remark-parse": "^11.0.0",