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

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.js CHANGED
@@ -36,7 +36,6 @@ class PluginDriver {
36
36
  async init() {
37
37
  this.clearPlugins();
38
38
  const config = this.#config;
39
- const markdownConfig = config.markdown || {};
40
39
  const themeConfig = config?.themeConfig || {};
41
40
  const enableLastUpdated = themeConfig?.lastUpdated || themeConfig?.locales?.some((locale)=>locale.lastUpdated);
42
41
  const mediumZoomConfig = config?.mediumZoom ?? true;
@@ -50,11 +49,9 @@ class PluginDriver {
50
49
  this.addPlugin(pluginMediumZoom('object' == typeof mediumZoomConfig ? mediumZoomConfig : void 0));
51
50
  }
52
51
  this.addPlugin(pluginContainerSyntax());
53
- const { pluginShiki } = await import("@rspress/plugin-shiki");
54
52
  (config.plugins || []).forEach((plugin)=>{
55
53
  this.addPlugin(plugin);
56
54
  });
57
- this.addPlugin(pluginShiki(markdownConfig.shiki));
58
55
  if (!haveNavSidebarConfig) {
59
56
  const { pluginAutoNavSidebar } = await import("@rspress/plugin-auto-nav-sidebar");
60
57
  this.addPlugin(pluginAutoNavSidebar());
@@ -182,7 +179,7 @@ const pluginVirtualModule = (pluginOptions = {})=>({
182
179
  }
183
180
  }
184
181
  });
185
- var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.13"}');
182
+ var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.14"}');
186
183
  const RSPRESS_VERSION = package_namespaceObject.i8;
187
184
  const isProduction = ()=>'production' === process.env.NODE_ENV;
188
185
  const importStatementRegex = /import\s+(.*?)\s+from\s+(['"])(.*?)(?:"|');?/gm;
@@ -567,7 +564,7 @@ function applyReplaceRulesToNestedObject(obj, replaceRules) {
567
564
  else if ('object' == typeof obj[key] && null !== obj[key]) obj[key] = applyReplaceRulesToNestedObject(obj[key], replaceRules);
568
565
  return obj;
569
566
  }
570
- async function extractPageData(replaceRules, alias, domain, root, routeService, highlighterLangs, searchCodeBlocks) {
567
+ async function extractPageData(replaceRules, alias, domain, root, routeService, searchCodeBlocks) {
571
568
  const pageData = await Promise.all(routeService.getRoutes().map(async (route, index)=>{
572
569
  const defaultIndexInfo = {
573
570
  id: index,
@@ -590,7 +587,7 @@ async function extractPageData(replaceRules, alias, domain, root, routeService,
590
587
  applyReplaceRulesToNestedObject(frontmatter, replaceRules);
591
588
  const { flattenContent } = await flattenMdxContent(applyReplaceRules(contentWithoutFrontMatter, replaceRules), route.absolutePath, alias);
592
589
  content = flattenContent.replace(importStatementRegex, '');
593
- const { html: rawHtml, title, toc: rawToc, languages } = await compile({
590
+ const { html: rawHtml, title, toc: rawToc } = await compile({
594
591
  value: content,
595
592
  filepath: route.absolutePath,
596
593
  development: 'production' !== process.env.NODE_ENV,
@@ -639,7 +636,6 @@ async function extractPageData(replaceRules, alias, domain, root, routeService,
639
636
  }
640
637
  });
641
638
  if (content.startsWith(title)) content = content.slice(title.length);
642
- languages.forEach((lang)=>highlighterLangs.add(lang));
643
639
  const toc = rawToc.map((item)=>{
644
640
  const match = item.id.match(/-(\d+)$/);
645
641
  let position = -1;
@@ -805,12 +801,11 @@ async function siteDataVMPlugin(context) {
805
801
  const userConfig = config;
806
802
  const tempSearchObj = Object.assign({}, userConfig.search);
807
803
  if (tempSearchObj) tempSearchObj.searchHooks = void 0;
808
- const highlightLanguages = new Set();
809
804
  const replaceRules = userConfig?.replaceRules || [];
810
805
  const searchConfig = userConfig?.search || {};
811
806
  const domain = searchConfig?.mode === 'remote' ? searchConfig.domain ?? '' : '';
812
807
  const searchCodeBlocks = 'codeBlocks' in searchConfig ? Boolean(searchConfig.codeBlocks) : true;
813
- const pages = await extractPageData(replaceRules, alias, domain, userDocRoot, routeService, highlightLanguages, searchCodeBlocks);
808
+ const pages = await extractPageData(replaceRules, alias, domain, userDocRoot, routeService, searchCodeBlocks);
814
809
  await pluginDriver.modifySearchIndexData(pages);
815
810
  const versioned = userConfig.search && 'remote' !== userConfig.search.mode && userConfig.search.versioned;
816
811
  const groupedPages = groupBy(pages, (page)=>{
@@ -866,7 +861,6 @@ async function siteDataVMPlugin(context) {
866
861
  shiki: {}
867
862
  }
868
863
  };
869
- if (Array.isArray(siteData.pages[0]?.extraHighlightLanguages)) siteData.pages[0].extraHighlightLanguages.forEach((lang)=>highlightLanguages.add(lang));
870
864
  return {
871
865
  [`${types_RuntimeModuleID.SiteData}.mjs`]: `export default ${JSON.stringify(siteData, null, 2)}`,
872
866
  [types_RuntimeModuleID.SearchIndexHash]: `export default ${JSON.stringify(indexHashByGroup, null, 2)}`
@@ -1128,7 +1122,7 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
1128
1122
  let render;
1129
1123
  try {
1130
1124
  const { default: ssrExports } = await import(pathToFileURL(ssrBundlePath).toString());
1131
- ({ render } = ssrExports);
1125
+ render = await ssrExports.render;
1132
1126
  } catch (e) {
1133
1127
  if (e instanceof Error) {
1134
1128
  logger.error(`Failed to load SSG bundle: ${external_picocolors_default.yellow(ssrBundlePath)}: ${e.message}`);
@@ -12,9 +12,9 @@ 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>;
package/dist/processor.js CHANGED
@@ -14,6 +14,8 @@ 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_default 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);
@@ -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
365
  const format = external_node_path_default.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 = [
@@ -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,
@@ -349,6 +414,10 @@ async function createMDXOptions(options) {
349
414
  }
350
415
  ]
351
416
  ] : [],
417
+ [
418
+ rehype_default,
419
+ createRehypeShikiOptions(showLineNumbers, shiki)
420
+ ],
352
421
  [
353
422
  external_rehype_external_links_default,
354
423
  {
@@ -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 = {
@@ -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.14",
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/index.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,18 +72,18 @@
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-last-updated": "2.0.0-beta.14",
81
+ "@rspress/plugin-medium-zoom": "2.0.0-beta.14",
82
+ "@rspress/plugin-auto-nav-sidebar": "2.0.0-beta.14",
83
+ "@rspress/runtime": "2.0.0-beta.14",
84
+ "@rspress/plugin-container-syntax": "2.0.0-beta.14",
85
+ "@rspress/shared": "2.0.0-beta.14",
86
+ "@rspress/theme-default": "2.0.0-beta.14"
82
87
  },
83
88
  "devDependencies": {
84
89
  "@microsoft/api-extractor": "^7.52.8",