Skip to content

Commit

Permalink
feat: bump shiki to v1, migrate to official transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 17, 2024
1 parent 5309967 commit e11037c
Show file tree
Hide file tree
Showing 24 changed files with 1,552 additions and 567 deletions.
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 对应的语法。

#### 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",
"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
9 changes: 6 additions & 3 deletions packages/plugin-shiki/src/rehypePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { visit } from 'unist-util-visit';
import type { Plugin } from 'unified';
import type { Text, Root, ElementContent } from 'hast';
import { fromHtml } from 'hast-util-from-html';
import type shiki from 'shiki';
import type { Highlighter } from 'shiki';

interface Options {
highlighter: shiki.Highlighter;
highlighter: Highlighter;
}

export const rehypePluginShiki: Plugin<[Options], Root> = function ({
Expand Down Expand Up @@ -49,7 +49,10 @@ export const rehypePluginShiki: Plugin<[Options], Root> = function ({
if (!lang) {
return;
}
const highlightedCode = highlighter.codeToHtml(codeContent, { lang });
const highlightedCode = highlighter.codeToHtml(codeContent, {
lang,
theme: 'css-variables',
});
const fragmentAst = fromHtml(highlightedCode, { fragment: true });
const preElement = fragmentAst.children[0] as unknown as any;
const codeElement = preElement.children[0];
Expand Down
Loading

0 comments on commit e11037c

Please sign in to comment.