Skip to content

Commit 8e62083

Browse files
JounQinSoonIter
andauthored
feat!(plugin-shiki): bump shiki to v3, migrate to official transformers (#1672)
Co-authored-by: SoonIter <[email protected]>
1 parent e3d6ec5 commit 8e62083

File tree

24 files changed

+360
-567
lines changed

24 files changed

+360
-567
lines changed

e2e/fixtures/plugin-shiki/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"@rspress/plugin-shiki": "workspace:*",
12+
"@shikijs/transformers": "^3.1.0",
1213
"rspress": "workspace:*"
1314
},
1415
"devDependencies": {

e2e/fixtures/plugin-shiki/rspress.config.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import path from 'node:path';
22
import {
3-
createTransformerDiff,
4-
createTransformerErrorLevel,
5-
createTransformerFocus,
6-
createTransformerHighlight,
73
createTransformerLineNumber,
84
pluginShiki,
95
} from '@rspress/plugin-shiki';
6+
import {
7+
transformerNotationDiff,
8+
transformerNotationErrorLevel,
9+
transformerNotationFocus,
10+
transformerNotationHighlight,
11+
} from '@shikijs/transformers';
1012
import { defineConfig } from 'rspress/config';
1113

1214
export default defineConfig({
1315
root: path.join(__dirname, 'doc'),
1416
plugins: [
1517
pluginShiki({
1618
transformers: [
17-
createTransformerDiff(),
19+
transformerNotationDiff(),
20+
transformerNotationErrorLevel(),
21+
transformerNotationHighlight(),
22+
transformerNotationFocus(),
1823
createTransformerLineNumber(),
19-
createTransformerErrorLevel(),
20-
createTransformerHighlight(),
21-
createTransformerFocus(),
2224
],
2325
}),
2426
],

packages/document/docs/en/plugin/official-plugins/shiki.mdx

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,26 @@ export default defineConfig({
3636
This plugin supports passing in an object configuration. The properties of this object configuration are as follows:
3737

3838
```ts
39-
interface PluginShikiOptions {
39+
import type {
40+
BuiltinLanguage,
41+
BuiltinTheme,
42+
ShikiTransformer,
43+
SpecialLanguage,
44+
} from 'shiki';
45+
46+
export interface PluginShikiOptions {
4047
/**
41-
* Code highlighting theme
48+
* Code highlighting theme, @see https://shiki.style/themes
4249
*/
43-
theme: string;
50+
theme: BuiltinTheme | 'css-variables';
4451
/**
45-
* Code highlighting language
52+
* Code highlighting language, @see https://shiki.style/languages
4653
*/
47-
langs: string[];
54+
langs: Array<BuiltinLanguage | SpecialLanguage>;
4855
/**
49-
* Add custom transformer
56+
* Custom shiki transformer, @see https://shiki.style/guide/transformers
5057
*/
51-
transformers: Transformer[];
58+
transformers: ShikiTransformer[];
5259
}
5360
```
5461

@@ -64,81 +71,34 @@ Transformer is a concept in this plugin, its function is to transform specific s
6471

6572
A few Transformers are built into this plugin, including:
6673

67-
- `createTransformerDiff`: Implementation of the diff highlighting effect of the code block.
6874
- `createTransformerLineNumber`: Implement the display of the line number of the code block.
69-
- `createTransformerErrorLevel`: Implement the display of the error level of the corresponding line of the code block, including `error` and `warning`.
70-
- `createTransformerHighlight`: Implement line highlighting display of the code block.
71-
- `createTransformerFocus`: Implement line focus display of the code block.
7275

7376
You can enable these Transformers by configuring the `transformers` attribute, such as:
7477

7578
```ts title="rspress.config.ts"
7679
import { defineConfig } from 'rspress/config';
7780
import { pluginShiki, createTransformerDiff } from '@rspress/plugin-shiki';
81+
import {
82+
transformerNotationDiff,
83+
transformerNotationErrorLevel,
84+
transformerNotationFocus,
85+
transformerNotationHighlight,
86+
} from '@shikijs/transformers';
7887

7988
export default defineConfig({
8089
plugins: [
8190
pluginShiki({
8291
transformers: [
8392
// Add as needed
84-
createTransformerDiff(),
85-
// createTransformerLineNumber(),
86-
// createTransformerErrorLevel(),
87-
// createTransformerHighlight(),
88-
// createTransformerFocus(),
93+
createTransformerLineNumber(),
94+
// transformerNotationDiff(),
95+
// transformerNotationErrorLevel(),
96+
// transformerNotationHighlight(),
97+
// transformerNotationFocus(),
8998
],
9099
}),
91100
],
92101
});
93102
```
94103

95-
Then let us introduce how to use the syntax corresponding to these Transformers.
96-
97-
#### Diff highlighting
98-
99-
Use the `diff` syntax in the markdown code block, such as:
100-
101-
```ts
102-
export function foo() {
103-
console.log('Diff remove'); // [!code --]
104-
console.log('Diff add'); // [!code ++]
105-
}
106-
```
107-
108-
This will automatically apply the diff highlighting effect to the corresponding line of code.
109-
110-
#### Line number display
111-
112-
Use the `hl` syntax in the markdown code block, such as:
113-
114-
```ts
115-
export function foo() {
116-
console.log('Line number'); // [!code hl]
117-
}
118-
```
119-
120-
This will automatically display the line number for the corresponding line of code.
121-
122-
#### Error level display
123-
124-
Use the `error` or `warning` syntax in the markdown code block, such as:
125-
126-
```ts
127-
export function foo() {
128-
console.log('Error level'); // [!code error]
129-
}
130-
```
131-
132-
This will automatically display the error level for the corresponding line of code.
133-
134-
#### Line focus display
135-
136-
Use the `focus` syntax in the markdown code block, such as:
137-
138-
```ts
139-
export function foo() {
140-
console.log('Focus'); // [!code focus]
141-
}
142-
```
143-
144-
This will automatically display the focus effect for the corresponding line of code.
104+
Please view [Shiki Transformers documentation](https://shiki.style/guide/transformers) for more information.

packages/document/docs/zh/plugin/official-plugins/shiki.mdx

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,26 @@ export default defineConfig({
3636
该插件支持传入一个对象配置,该对象配置的属性如下:
3737

3838
```ts
39-
interface PluginShikiOptions {
39+
import type {
40+
BuiltinLanguage,
41+
BuiltinTheme,
42+
ShikiTransformer,
43+
SpecialLanguage,
44+
} from 'shiki';
45+
46+
export interface PluginShikiOptions {
4047
/**
41-
* 代码高亮主题
48+
* 代码高亮主题@see https://shiki.style/themes
4249
*/
43-
theme: string;
50+
theme: BuiltinTheme | 'css-variables';
4451
/**
45-
* 代码高亮的语言
52+
* 代码高亮的语言@see https://shiki.style/languages
4653
*/
47-
langs: string[];
54+
langs: Array<BuiltinLanguage | SpecialLanguage>;
4855
/**
49-
* 添加自定义 transformer
56+
* 自定义 shiki transformer@see https://shiki.style/guide/transformers
5057
*/
51-
transformers: Transformer[];
58+
transformers: ShikiTransformer[];
5259
}
5360
```
5461

@@ -64,81 +71,34 @@ Transformer 是本插件中的一个概念,它的作用是对代码块的特
6471

6572
本插件中内置了一些 Transformer,包括:
6673

67-
- `createTransformerDiff`:实现代码块的 diff 高亮效果。
6874
- `createTransformerLineNumber`:实现代码块的行号显示。
69-
- `createTransformerErrorLevel`:实现代码块对应行的错误等级显示,包括 `error``warning`
70-
- `createTransformerHighlight`:实现代码块的行高亮显示。
71-
- `createTransformerFocus`: 实现代码块的行聚焦显示。
7275

7376
你可以通过配置 `transformers` 属性来启用这些 Transformer,比如:
7477

7578
```ts title="rspress.config.ts"
7679
import { defineConfig } from 'rspress/config';
7780
import { pluginShiki, createTransformerDiff } from '@rspress/plugin-shiki';
81+
import {
82+
transformerNotationDiff,
83+
transformerNotationErrorLevel,
84+
transformerNotationFocus,
85+
transformerNotationHighlight,
86+
} from '@shikijs/transformers';
7887

7988
export default defineConfig({
8089
plugins: [
8190
pluginShiki({
8291
transformers: [
8392
// 按需加入即可
84-
createTransformerDiff(),
85-
// createTransformerLineNumber(),
86-
// createTransformerErrorLevel(),
87-
// createTransformerHighlight(),
88-
// createTransformerFocus(),
93+
createTransformerLineNumber(),
94+
// transformerNotationDiff(),
95+
// transformerNotationErrorLevel(),
96+
// transformerNotationHighlight(),
97+
// transformerNotationFocus(),
8998
],
9099
}),
91100
],
92101
});
93102
```
94103

95-
接着我们来介绍一下如何使用这些 Transformer 对应的语法。
96-
97-
#### diff 高亮
98-
99-
在 markdown 的代码块中使用 `diff` 语法,比如:
100-
101-
```ts
102-
export function foo() {
103-
console.log('Diff remove'); // [!code --]
104-
console.log('Diff add'); // [!code ++]
105-
}
106-
```
107-
108-
这样会自动对相应行的代码应用 diff 高亮效果。
109-
110-
#### 行号显示
111-
112-
在 markdown 的代码块中使用 `hl` 语法,比如:
113-
114-
```ts
115-
export function foo() {
116-
console.log('Line number'); // [!code hl]
117-
}
118-
```
119-
120-
这样会自动对相应行的代码显示行号。
121-
122-
#### 错误等级显示
123-
124-
在 markdown 的代码块中使用 `error``warning` 语法,比如:
125-
126-
```ts
127-
export function foo() {
128-
console.log('Error level'); // [!code error]
129-
}
130-
```
131-
132-
这样会自动对相应行的代码显示错误等级。
133-
134-
#### 行聚焦显示
135-
136-
在 markdown 的代码块中使用 `focus` 语法,比如:
137-
138-
```ts
139-
export function foo() {
140-
console.log('Focus'); // [!code focus]
141-
}
142-
```
143-
144-
这样会自动对相应行的代码显示聚焦效果。
104+
请查看 [Shiki Transformers 文档](https://shiki.style/guide/transformers) 获取更多信息。

packages/plugin-shiki/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"dependencies": {
3232
"@rspress/shared": "workspace:*",
3333
"hast-util-from-html": "^1.0.2",
34-
"shiki": "^0.14.7",
34+
"shiki": "^3.1.0",
3535
"unist-util-visit": "^4.1.2"
3636
},
3737
"devDependencies": {

packages/plugin-shiki/shiki.css

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* -------------------------------------------------------------------------- */
55

66
:root {
7-
--shiki-color-text: #414141;
8-
--shiki-color-background: transparent;
7+
--shiki-foreground: #414141;
8+
--shiki-background: transparent;
99
--shiki-token-constant: #1976d2;
1010
--shiki-token-string: #31a94d;
1111
--shiki-token-comment: rgb(182, 180, 180);
@@ -18,7 +18,7 @@
1818
}
1919

2020
.dark {
21-
--shiki-color-text: #cac7c7;
21+
--shiki-foreground: #cac7c7;
2222
--shiki-token-constant: #6fb0fa;
2323
--shiki-token-string: #f9a86e;
2424
--shiki-token-comment: #6a727b;
@@ -37,9 +37,8 @@
3737
.diff,
3838
.code-line-highlighted {
3939
transition: background-color 0.5s;
40-
margin: 0 -20px;
4140
padding: 0 20px;
42-
width: calc(100% + 40px);
41+
width: 100%;
4342
display: inline-block;
4443
position: relative;
4544
}

packages/plugin-shiki/src/rehypePlugin.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import type { Element, ElementContent, Root, Text } from 'hast';
22
import { fromHtml } from 'hast-util-from-html';
3-
import type shiki from 'shiki';
3+
import type { BuiltinTheme, Highlighter } from 'shiki';
44
import type { Plugin } from 'unified';
55
import { visit } from 'unist-util-visit';
66

77
interface Options {
8-
highlighter: shiki.Highlighter;
8+
highlighter: Highlighter;
9+
theme: BuiltinTheme;
910
}
1011

11-
export const rehypePluginShiki: Plugin<[Options], Root> = function ({
12-
highlighter,
13-
}) {
14-
return (tree: Root) => {
12+
export const rehypePluginShiki: Plugin<[Options], Root> =
13+
({ highlighter, theme }) =>
14+
(tree: Root) => {
1515
visit(tree, 'element', (node, index, parent) => {
1616
// <pre><code>...</code></pre>
1717
if (
@@ -32,7 +32,7 @@ export const rehypePluginShiki: Plugin<[Options], Root> = function ({
3232
highlightLines = highlightMatch
3333
?.replace(/[{}]/g, '')
3434
.split(',')
35-
.map(item => {
35+
.flatMap(item => {
3636
const [start, end] = item.split('-');
3737
if (end) {
3838
return Array.from(
@@ -41,15 +41,17 @@ export const rehypePluginShiki: Plugin<[Options], Root> = function ({
4141
);
4242
}
4343
return Number(start);
44-
})
45-
.flat();
44+
});
4645
}
4746
// for example: language-js {1,2,3-5}
4847
const lang = codeClassName.split(' ')[0].split('-')[1];
4948
if (!lang) {
5049
return;
5150
}
52-
const highlightedCode = highlighter.codeToHtml(codeContent, { lang });
51+
const highlightedCode = highlighter.codeToHtml(codeContent, {
52+
lang,
53+
theme,
54+
});
5355
const fragmentAst = fromHtml(highlightedCode, { fragment: true });
5456
const preElement = fragmentAst.children[0] as Element;
5557
const codeElement = preElement.children[0] as Element;
@@ -81,4 +83,3 @@ export const rehypePluginShiki: Plugin<[Options], Root> = function ({
8183
}
8284
});
8385
};
84-
};

0 commit comments

Comments
 (0)