feat: can specify some languages individually in original_lang_name
to not convert or only convert certain languages
This commit is contained in:
parent
e1d2442f7c
commit
2a267a8651
19
README.md
19
README.md
@ -53,18 +53,18 @@ shiki:
|
||||
pre_style: true # Preserve the style of the <pre> tag. default: true
|
||||
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-
|
||||
transformers:
|
||||
# List of transformers to be enabled.
|
||||
# Please refer to https://shiki.style/packages/transformers for the list of supported transformers.
|
||||
transformers:
|
||||
# You can omit `name` and `option` when no options are required, directly using the string.
|
||||
- "example1"
|
||||
# When additional option are required, please explicitly set name and option.
|
||||
- name: example2
|
||||
option:
|
||||
# Options for the transformer.
|
||||
# Please check the @shikijs/transformer's source code to get the list of supported options
|
||||
# Source code of @shikijs/transformer:
|
||||
# https://github.com/shikijs/shiki/tree/main/packages/transformers/src/transformers
|
||||
option:
|
||||
exampleOption1: exampleValue1
|
||||
exampleOption2: exampleValue2
|
||||
additional:
|
||||
@ -87,6 +87,21 @@ shiki:
|
||||
```
|
||||
See [Dual Themes](https://shiki.style/guide/dual-themes) for how to switch between multiple themes.
|
||||
|
||||
You can also specify some languages individually in `original_lang_name` to not convert or only convert them:
|
||||
```yaml
|
||||
original_lang_name:
|
||||
# Set to true to exclude the languages listed in `langs`, otherwise only convert the languages in `langs`.
|
||||
exclude: true
|
||||
# Must be an array
|
||||
langs:
|
||||
- shell
|
||||
- bash
|
||||
- zsh
|
||||
# Change the original language name
|
||||
change_origin:
|
||||
fortran-free-form: fortran
|
||||
```
|
||||
Refer to [Languages | Shiki](https://shiki.style/languages) to view the original names (IDs) of languages.
|
||||
|
||||
### Example of Using Transformers
|
||||
if you want to mark some lines, you can use Hexo's code block tag plugin (which has been adapted by this plugin):
|
||||
|
@ -52,13 +52,15 @@ shiki:
|
||||
pre_style: true # 保留 <pre> 标签的样式,即主题的 `background-color`。
|
||||
default_color: light # 仅在同时使用多个主题时生效。默认值:light
|
||||
css_variable_prefix: --shiki- # 仅在同时使用多个主题时生效。默认值:--shiki-
|
||||
transformers:
|
||||
# 需要启用的转换器列表。请参阅 https://shiki.style/packages/transformers 以获取支持的转换器列表。
|
||||
- "example1" # 不需要设置选项时,可省略 `name` 与 `option`,直接使用字符串。
|
||||
- name: example2 # 需要设置选项时,请显式设置 name 与 option。
|
||||
option:
|
||||
transformers:
|
||||
# 不需要设置选项时,可省略 `name` 与 `option`,直接使用字符串。
|
||||
- "example1"
|
||||
# 需要设置选项时,请显式设置 name 与 option。
|
||||
- name: example2
|
||||
# 转换器的选项,请查看转换器的源码以获取支持的选项列表
|
||||
# 转换器源码:https://github.com/shikijs/shiki/tree/main/packages/transformers/src/transformers
|
||||
option:
|
||||
exampleOption1: exampleValue1
|
||||
exampleOption2: exampleValue2
|
||||
additional:
|
||||
@ -81,6 +83,22 @@ shiki:
|
||||
```
|
||||
在 [Dual Themes](https://shiki.style/guide/dual-themes) 中查看如何切换多个主题。
|
||||
|
||||
同时你还可以在 `original_lang_name` 中单独指定一些语言进行或不进行转换:
|
||||
```yaml
|
||||
original_lang_name:
|
||||
# 为真时将不转换 langs 内的语言,反之则只转换 langs 内的语言
|
||||
exclude: true
|
||||
# 必须为数组
|
||||
langs:
|
||||
- shell
|
||||
- bash
|
||||
- zsh
|
||||
# 改变语言原名
|
||||
change_origin:
|
||||
fortran-free-form: fortran
|
||||
```
|
||||
参阅 [Languages | Shiki](https://shiki.style/languages) 查看语言的原名(ID)。
|
||||
|
||||
### 转换器使用示例
|
||||
如果你想标记某些行,你可以使用 Hexo 的代码块标签插件(本插件对其做了适配):
|
||||
```markdown
|
||||
|
25
src/main.ts
25
src/main.ts
@ -100,7 +100,7 @@ export async function init(hexo: Hexo) {
|
||||
highlighter_options["langAlias"] = config.additional.lang_alias;
|
||||
}
|
||||
const highlighter = await createHighlighter(highlighter_options);
|
||||
const supportedLanguages = highlighter.getLoadedLanguages().reduce(
|
||||
const supported_languages = highlighter.getLoadedLanguages().reduce(
|
||||
(acc, lang) => {
|
||||
acc[lang] = true;
|
||||
return acc;
|
||||
@ -112,13 +112,30 @@ export async function init(hexo: Hexo) {
|
||||
}
|
||||
);
|
||||
|
||||
// 处理语言转换原名
|
||||
const useOringinalLangName = (lang) => {
|
||||
if (lang === "text") return false;
|
||||
if (typeof config.original_lang_name === "boolean") return config.original_lang_name;
|
||||
const exclude = config.original_lang_name.exclude || false;
|
||||
const langs = config.original_lang_name.langs || [];
|
||||
return exclude ? !langs.includes(lang) : langs.includes(lang);
|
||||
};
|
||||
|
||||
const originalLangName = (lang) => {
|
||||
const name = highlighter.getLanguage(lang).name;
|
||||
if (typeof config.original_lang_name === "boolean") return name;
|
||||
const origin = config.original_lang_name.change_origin || {};
|
||||
if (name in origin) return origin[name];
|
||||
return name;
|
||||
};
|
||||
|
||||
const hexoHighlighter = (code: string, options: HighlightOptions) => {
|
||||
var code = config.strip_indent ? (stripIndent(code) as string) : code;
|
||||
code = config.tab_replace ? code.replace(/\t/g, config.tab_replace) : code;
|
||||
|
||||
// 处理代码语言
|
||||
let lang = options.lang;
|
||||
if (!lang || !supportedLanguages[lang]) {
|
||||
if (!lang || !supported_languages[lang]) {
|
||||
lang = "text";
|
||||
}
|
||||
|
||||
@ -184,9 +201,7 @@ export async function init(hexo: Hexo) {
|
||||
const html = htmlTag(
|
||||
"figure",
|
||||
{
|
||||
class: `highlight ${
|
||||
config.original_lang_name && lang !== "text" ? highlighter.getLanguage(lang).name : lang
|
||||
}`,
|
||||
class: `highlight ${useOringinalLangName(lang) ? originalLangName(lang) : lang}`,
|
||||
},
|
||||
caption +
|
||||
htmlTag(
|
||||
|
Loading…
Reference in New Issue
Block a user