Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bump shiki to v1, migrate to official transformers #1672

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/fixtures/plugin-shiki/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@rspress/plugin-shiki": "workspace:*",
"@shikijs/transformers": "1.24.2",
"rspress": "workspace:*"
},
"devDependencies": {
Expand Down
22 changes: 12 additions & 10 deletions e2e/fixtures/plugin-shiki/rspress.config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import path from 'node:path';
import { defineConfig } from 'rspress/config';
import {
createTransformerDiff,
createTransformerErrorLevel,
createTransformerFocus,
createTransformerHighlight,
createTransformerLineNumber,
pluginShiki,
} from '@rspress/plugin-shiki';
import {
transformerNotationDiff,
transformerNotationErrorLevel,
transformerNotationFocus,
transformerNotationHighlight,
} from '@shikijs/transformers';
import path from 'node:path';
import { defineConfig } from 'rspress/config';

export default defineConfig({
root: path.join(__dirname, 'doc'),
plugins: [
pluginShiki({
transformers: [
createTransformerDiff(),
transformerNotationDiff(),
transformerNotationErrorLevel(),
transformerNotationHighlight(),
transformerNotationFocus(),
createTransformerLineNumber(),
createTransformerErrorLevel(),
createTransformerHighlight(),
createTransformerFocus(),
],
}),
],
Expand Down
87 changes: 21 additions & 66 deletions packages/document/docs/en/plugin/official-plugins/shiki.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ export default defineConfig({
This plugin supports passing in an object configuration. The properties of this object configuration are as follows:

```ts
interface PluginShikiOptions {
import type { BuiltinLanguage, BuiltinTheme, ShikiTransformer, SpecialLanguage } from 'shiki';

export interface PluginShikiOptions {
/**
* Code highlighting theme
* Code highlighting theme, @see https://shiki.style/themes
*/
theme: string;
theme: BuiltinTheme | 'css-variables';
/**
* Code highlighting language
* Code highlighting language, @see https://shiki.style/languages
*/
langs: string[];
langs: Array<BuiltinLanguage | SpecialLanguage>;
/**
* Add custom transformer
* Custom shiki transformer, @see https://shiki.style/guide/transformers
*/
transformers: Transformer[];
transformers: ShikiTransformer[];
}
```

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

A few Transformers are built into this plugin, including:

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

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

```ts title="rspress.config.ts"
import { defineConfig } from 'rspress/config';
import { pluginShiki, createTransformerDiff } from '@rspress/plugin-shiki';
import {
transformerNotationDiff,
transformerNotationErrorLevel,
transformerNotationFocus,
transformerNotationHighlight,
} from '@shikijs/transformers';

export default defineConfig({
plugins: [
pluginShiki({
transformers: [
// Add as needed
createTransformerDiff(),
// createTransformerLineNumber(),
// createTransformerErrorLevel(),
// createTransformerHighlight(),
// createTransformerFocus(),
createTransformerLineNumber(),
// transformerNotationDiff(),
// transformerNotationErrorLevel(),
// transformerNotationHighlight(),
// transformerNotationFocus(),
],
}),
],
});
```

Then let us introduce how to use the syntax corresponding to these Transformers.

#### Diff highlighting

Use the `diff` syntax in the markdown code block, such as:

```ts
export function foo() {
console.log('Diff remove'); // [!code --]
console.log('Diff add'); // [!code ++]
}
```

This will automatically apply the diff highlighting effect to the corresponding line of code.

#### Line number display

Use the `hl` syntax in the markdown code block, such as:

```ts
export function foo() {
console.log('Line number'); // [!code hl]
}
```

This will automatically display the line number for the corresponding line of code.

#### Error level display

Use the `error` or `warning` syntax in the markdown code block, such as:

```ts
export function foo() {
console.log('Error level'); // [!code error]
}
```

This will automatically display the error level for the corresponding line of code.

#### Line focus display

Use the `focus` syntax in the markdown code block, such as:

```ts
export function foo() {
console.log('Focus'); // [!code focus]
}
```

This will automatically display the focus effect for the corresponding line of code.
Please view [Shiki Transformers documentation](https://shiki.style/guide/transformers) for more information.
87 changes: 21 additions & 66 deletions packages/document/docs/zh/plugin/official-plugins/shiki.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ export default defineConfig({
该插件支持传入一个对象配置,该对象配置的属性如下:

```ts
interface PluginShikiOptions {
import type { BuiltinLanguage, BuiltinTheme, ShikiTransformer, SpecialLanguage } from 'shiki';

export interface PluginShikiOptions {
/**
* 代码高亮主题
* 代码高亮主题,@see https://shiki.style/themes
*/
theme: string;
theme: BuiltinTheme | 'css-variables';
/**
* 代码高亮的语言
* 代码高亮的语言,@see https://shiki.style/languages
*/
langs: string[];
langs: Array<BuiltinLanguage | SpecialLanguage>;
/**
* 添加自定义 transformer
* 自定义 shiki transformer,@see https://shiki.style/guide/transformers
*/
transformers: Transformer[];
transformers: ShikiTransformer[];
}
```

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

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

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

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

```ts title="rspress.config.ts"
import { defineConfig } from 'rspress/config';
import { pluginShiki, createTransformerDiff } from '@rspress/plugin-shiki';
import {
transformerNotationDiff,
transformerNotationErrorLevel,
transformerNotationFocus,
transformerNotationHighlight,
} from '@shikijs/transformers';

export default defineConfig({
plugins: [
pluginShiki({
transformers: [
// 按需加入即可
createTransformerDiff(),
// createTransformerLineNumber(),
// createTransformerErrorLevel(),
// createTransformerHighlight(),
// createTransformerFocus(),
createTransformerLineNumber(),
// transformerNotationDiff(),
// transformerNotationErrorLevel(),
// transformerNotationHighlight(),
// transformerNotationFocus(),
],
}),
],
});
```

接着我们来介绍一下如何使用这些 Transformer 对应的语法。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not remove exports in this plugin, you should reexport in this plugin and keep all docs here to avoid breaking change.

Furthermore, we can give an example in docs to introduce how to import other transformers in this plugin.

Copy link
Contributor Author

@JounQin JounQin Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transformers signatures are changed, so I believe we should make a breaking change for this plugin.

And those builtin transformers are from @shikijs/transformers, I didn't add it as direct dependency because maybe same users don't use them.

Furthermore, we can give an example in docs to introduce how to import other transformers in this plugin.

https://github.com/JounQin/rspress/blob/e11037c06d110575bda056f9a8200049d7d303b2/packages/document/docs/zh/plugin/official-plugins/shiki.mdx?plain=1#L71-L97

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaking change for now is not acceptable, and we can make this breaking change in Rspress 2.0 since all core and package are using same version for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want a canary version now, I can release for you for this plugin.

Copy link
Contributor Author

@JounQin JounQin Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand, all official plugin major versions are bound with rspress core? That would make plugin evolution too slow maybe.

since all core and package are using same version for now.

OK, I understand now. That seems a big challenge.

If you want a canary version now, I can release for you for this plugin.

Sweet. Or maybe I just fork the codes temporarily.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it seems the release workflow can not run in a forked repo, I think you can just patch this plugin using pnpm patch or other tools in package manager level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I mean to copy the codes into our own repo as a thirty-party plugin, I'm building a company level document tool, so local patch won't work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, this package is MIT license, just do it.


#### diff 高亮

在 markdown 的代码块中使用 `diff` 语法,比如:

```ts
export function foo() {
console.log('Diff remove'); // [!code --]
console.log('Diff add'); // [!code ++]
}
```

这样会自动对相应行的代码应用 diff 高亮效果。

#### 行号显示

在 markdown 的代码块中使用 `hl` 语法,比如:

```ts
export function foo() {
console.log('Line number'); // [!code hl]
}
```

这样会自动对相应行的代码显示行号。

#### 错误等级显示

在 markdown 的代码块中使用 `error` 或 `warning` 语法,比如:

```ts
export function foo() {
console.log('Error level'); // [!code error]
}
```

这样会自动对相应行的代码显示错误等级。

#### 行聚焦显示

在 markdown 的代码块中使用 `focus` 语法,比如:

```ts
export function foo() {
console.log('Focus'); // [!code focus]
}
```

这样会自动对相应行的代码显示聚焦效果。
请查看 [Shiki Transformers 文档](https://shiki.style/guide/transformers) 获取更多信息。
2 changes: 1 addition & 1 deletion packages/plugin-shiki/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"dependencies": {
"@rspress/shared": "workspace:*",
"hast-util-from-html": "2.0.3",
"shiki": "0.14.7",
"shiki": "1.24.2",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we lock dep versions in package.json.

"unist-util-visit": "5.0.0"
}
}
9 changes: 4 additions & 5 deletions packages/plugin-shiki/shiki.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* -------------------------------------------------------------------------- */

:root {
--shiki-color-text: #414141;
--shiki-color-background: transparent;
--shiki-foreground: #414141;
--shiki-background: transparent;
--shiki-token-constant: #1976d2;
--shiki-token-string: #31a94d;
--shiki-token-comment: rgb(182, 180, 180);
Expand All @@ -18,7 +18,7 @@
}

.dark {
--shiki-color-text: #cac7c7;
--shiki-foreground: #cac7c7;
--shiki-token-constant: #6fb0fa;
--shiki-token-string: #f9a86e;
--shiki-token-comment: #6a727b;
Expand All @@ -37,9 +37,8 @@
.diff,
.code-line-highlighted {
transition: background-color 0.5s;
margin: 0 -20px;
padding: 0 20px;
width: calc(100% + 40px);
width: 100%;
display: inline-block;
position: relative;
}
Expand Down
Loading
Loading