feat: Support to use transformers in https://shiki.style/packages/transformers

This commit is contained in:
HPCesia 2024-10-21 18:03:37 +08:00
parent c03510a8a7
commit 2635edc901
3 changed files with 61 additions and 3 deletions

View File

@ -34,13 +34,20 @@ shiki:
The complete configuration is as follows: The complete configuration is as follows:
```yaml ```yaml
shiki: shiki:
theme: one-dark-pro # see https://shiki.style/themes for supported themes. theme: one-dark-pro # Please refer to https://shiki.style/themes for supported themes.
line_number: false # default: false line_number: false # default: false
strip_indent: true # default: true strip_indent: true # default: true
tab_replace: " " # default: " " tab_replace: " " # default: " "
pre_style: true # Preserve the style of the <pre> tag, i.e., the theme's `background-color`. default: true pre_style: true # Preserve the style of the <pre> tag, i.e., the theme's `background-color`. default: true
default_color: light # Only take effect when using multiple themes. default: light default_color: light # Only take effect when using multiple themes. default: light
css_variable_prefix: --shiki- # Only take effect when using multiple themes. default: --shiki- css_variable_prefix: --shiki- # Only take effect when using multiple themes. default: --shiki-
transformers: # List of transformers to be enabled. Please refer to https://shiki.style/packages/transformers for the list of supported transformers.
- "transformerNotationDiff" # You can omit name and option when no settings are required, directly using the string.
- name: transformerNotationFocus # When settings are required, please explicitly set name and option.
option: # Options for the transformer, please check the transformer's source code to get the list of supported options
# Source code of transformers: https://github.com/shikijs/shiki/tree/main/packages/transformers/src/transformers
classActiveLine: focused
classActivePre: has-focused
additional: additional:
themes: # List of the TextMate theme json to be added. themes: # List of the TextMate theme json to be added.
- path/to/theme.json - path/to/theme.json

View File

@ -40,6 +40,13 @@ shiki:
pre_style: true # 保留 <pre> 标签的样式,即主题的 `background-color` pre_style: true # 保留 <pre> 标签的样式,即主题的 `background-color`
default_color: light # 仅在同时使用多个主题时生效。默认值light default_color: light # 仅在同时使用多个主题时生效。默认值light
css_variable_prefix: --shiki- # 仅在同时使用多个主题时生效。默认值:--shiki- css_variable_prefix: --shiki- # 仅在同时使用多个主题时生效。默认值:--shiki-
transformers: # 需要启用的转换器列表。请参阅 https://shiki.style/packages/transformers 以获取支持的转换器列表。
- "transformerNotationDiff" # 不需要设置选项时,可省略 name 与 option直接使用字符串。
- name: transformerNotationFocus # 需要设置选项时,请显式设置 name 与 option。
option: # 转换器的选项,请查看转换器的源码以获取支持的选项列表
# 转换器源码https://github.com/shikijs/shiki/tree/main/packages/transformers/src/transformers
classActiveLine: focused
classActivePre: has-focused
additional: additional:
themes: # 要添加的主题的 TextMate 主题 json 列表。 themes: # 要添加的主题的 TextMate 主题 json 列表。
langs: # 要添加的语言的 TextMate 语法 json 列表。 langs: # 要添加的语言的 TextMate 语法 json 列表。

View File

@ -4,6 +4,33 @@ import type Hexo from "hexo";
import { HighlightOptions } from "hexo/dist/extend/syntax_highlight"; import { HighlightOptions } from "hexo/dist/extend/syntax_highlight";
import { stripIndent, htmlTag } from "hexo-util"; import { stripIndent, htmlTag } from "hexo-util";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import {
transformerCompactLineOptions,
transformerMetaHighlight,
transformerMetaWordHighlight,
transformerNotationDiff,
transformerNotationErrorLevel,
transformerNotationFocus,
transformerNotationHighlight,
transformerNotationWordHighlight,
transformerRemoveLineBreak,
transformerRemoveNotationEscape,
transformerRenderWhitespace,
} from "@shikijs/transformers";
const supported_transformers = {
transformerCompactLineOptions,
transformerMetaHighlight,
transformerMetaWordHighlight,
transformerNotationDiff,
transformerNotationErrorLevel,
transformerNotationFocus,
transformerNotationHighlight,
transformerNotationWordHighlight,
transformerRemoveLineBreak,
transformerRemoveNotationEscape,
transformerRenderWhitespace,
};
export async function init(hexo: Hexo) { export async function init(hexo: Hexo) {
const config = Object.assign( const config = Object.assign(
@ -15,6 +42,7 @@ export async function init(hexo: Hexo) {
pre_style: true, pre_style: true,
default_color: "light", default_color: "light",
css_variable_prefix: "--shiki-", css_variable_prefix: "--shiki-",
transformers: [],
additional: { additional: {
themes: [], themes: [],
langs: [], langs: [],
@ -44,6 +72,22 @@ export async function init(hexo: Hexo) {
) )
); );
// 处理 transformers
const transformers = config.transformers
.map((transformer) => {
if (typeof transformer === "string") {
let tfm = supported_transformers[transformer];
if (!tfm) return null;
return tfm();
}
let tfm = supported_transformers[transformer["name"]];
if (!tfm) return null;
let option = transformer["option"];
if (!option) return tfm();
return tfm(option);
})
.filter((tfm) => tfm !== null);
// 创建 highlighter // 创建 highlighter
let highlighter_options = { let highlighter_options = {
langs: langs, langs: langs,
@ -91,13 +135,13 @@ export async function init(hexo: Hexo) {
pre = highlighter.codeToHtml(code, { pre = highlighter.codeToHtml(code, {
lang, lang,
theme: config.theme, theme: config.theme,
transformers: [transformerMarkedLine()], transformers: [transformerMarkedLine()].concat(transformers),
}); });
else else
pre = highlighter.codeToHtml(code, { pre = highlighter.codeToHtml(code, {
lang, lang,
themes: config.theme, themes: config.theme,
transformers: [transformerMarkedLine()], transformers: [transformerMarkedLine()].concat(transformers),
defaultColor: config.default_color, defaultColor: config.default_color,
cssVariablePrefix: config.css_variable_prefix, cssVariablePrefix: config.css_variable_prefix,
}); });