Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
66d07bb2a5 | |||
28035a8513 | |||
98ff7de56a | |||
280c69586f | |||
|
68115abd81 | ||
|
c111ee0243 | ||
|
2a267a8651 | ||
|
e1d2442f7c |
22
.github/workflows/npm-publish.yml
vendored
22
.github/workflows/npm-publish.yml
vendored
@ -15,18 +15,12 @@ jobs:
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 20
|
||||
- run: npm ci
|
||||
- run: npm run build
|
||||
|
||||
publish-npm:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 20
|
||||
registry-url: https://registry.npmjs.org/
|
||||
- run: npm publish
|
||||
registry-url: https://registry.npmjs.org
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
- name: Build
|
||||
run: npm run build
|
||||
- name: Publish package to npm
|
||||
run: npm publish
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
||||
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
|
||||
|
29
README.md
29
README.md
@ -5,7 +5,7 @@ English丨[简体中文](README_zh-CN.md)
|
||||
|
||||
A hexo plugin to use [Shiki](https://github.com/shikijs/shiki) as code block highlighter.
|
||||
|
||||
Hexo v7.0.0+ is required.
|
||||
[Hexo](https://github.com/hexojs/hexo) v7.0.0+ is required.
|
||||
|
||||
## Features
|
||||
- Use [Shiki](https://github.com/shikijs/shiki) to render code blocks, and the format is similar to Hexo's default code highlighting (so you don't need to make significant changes to the theme you are currently using).
|
||||
@ -45,21 +45,26 @@ shiki:
|
||||
line_number: false # default: false
|
||||
strip_indent: true # default: true
|
||||
tab_replace: " " # default: " "
|
||||
# Use the original language name in the figure tag
|
||||
# For example, when the language of code block is 'js':
|
||||
# original_lang_name: false => <figure class="highlighter js">
|
||||
# original_lang_name: true => <figure class="highlighter javascript">
|
||||
original_lang_name: false
|
||||
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:
|
||||
@ -82,6 +87,22 @@ 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):
|
||||
@ -117,7 +138,7 @@ If you are using [hexo-filter-mathjax](https://github.com/next-theme/hexo-filter
|
||||
|
||||
#### Solution
|
||||
For example, if you are using the hexo-filter-mathjax plugin, modify the [this line](https://github.com/next-theme/hexo-filter-mathjax/blob/20dc61352f8cf4d19425ad1833eb72b467c212ef/index.js#L20C3-L20C40) in the source code:
|
||||
```js
|
||||
```diff
|
||||
- data.content = mathjax(data.content);
|
||||
+ data.content = data.content.replace(
|
||||
+ /<span\s+class="math\s+[^"]*">\\[\(\[].*?\\[\)\]]<\/span>/gs,
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
一个使用 [Shiki](https://github.com/shikijs/shiki) 作为代码块高亮器的 Hexo 插件。
|
||||
|
||||
需要 Hexo v7.0.0+。
|
||||
需要 [Hexo](https://github.com/hexojs/hexo) v7.0.0+。
|
||||
|
||||
## 功能
|
||||
- 使用 [Shiki](https://github.com/shikijs/shiki) 渲染代码块,且与 Hexo 默认的代码高亮渲染的格式相似(因此你不需要对你现在使用的主题做很大的修改)。
|
||||
@ -44,16 +44,23 @@ shiki:
|
||||
line_number: false
|
||||
strip_indent: true
|
||||
tab_replace: " "
|
||||
# 在 figure 标签中类名使用语言原名
|
||||
# 例如代码块语言为 'js' 时:
|
||||
# original_lang_name: false => <figure class="highlighter js">
|
||||
# original_lang_name: true => <figure class="highlighter javascript">
|
||||
original_lang_name: false
|
||||
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:
|
||||
@ -76,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
|
||||
@ -110,7 +133,7 @@ fn main() {
|
||||
|
||||
#### 解决方法
|
||||
以 hexo-filter-mathjax 插件为例,修改源代码中的[这一行](https://github.com/next-theme/hexo-filter-mathjax/blob/20dc61352f8cf4d19425ad1833eb72b467c212ef/index.js#L20C3-L20C40):
|
||||
```js
|
||||
```diff
|
||||
- data.content = mathjax(data.content);
|
||||
+ data.content = data.content.replace(
|
||||
+ /<span\s+class="math\s+[^"]*">\\[\(\[].*?\\[\)\]]<\/span>/gs,
|
||||
|
@ -4,6 +4,10 @@ import { promises as fs } from 'fs';
|
||||
await build({
|
||||
entryPoints: ['src/main.ts'],
|
||||
bundle: true,
|
||||
external:[
|
||||
'hexo-util',
|
||||
'hexo'
|
||||
],
|
||||
outdir: 'dist',
|
||||
platform: 'node',
|
||||
sourcemap: false,
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hexo-highlighter-shiki",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.2",
|
||||
"description": "A package for Hexo which use Shiki to render code highlight.",
|
||||
"main": "dist/index.js",
|
||||
"files": [
|
||||
@ -36,6 +36,7 @@
|
||||
"typescript": "^5.6.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"hexo": "^7.0.0"
|
||||
"hexo": "^7.0.0",
|
||||
"hexo-util": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
31
src/main.ts
31
src/main.ts
@ -39,10 +39,13 @@ export async function init(hexo: Hexo) {
|
||||
line_number: false,
|
||||
strip_indent: true,
|
||||
tab_replace: " ",
|
||||
original_lang_name: false,
|
||||
pre_style: true,
|
||||
default_color: "light",
|
||||
css_variable_prefix: "--shiki-",
|
||||
transformers: [],
|
||||
// TODO: 增加 `wrap` 选项,与 Hexo 的 highlight.js 渲染行为一致
|
||||
// TODO: 增加 `beautify` 选项以适配更多主题
|
||||
additional: {
|
||||
themes: [],
|
||||
langs: [],
|
||||
@ -66,6 +69,7 @@ export async function init(hexo: Hexo) {
|
||||
for (const extra_theme of [].concat(config.additional.themes)) {
|
||||
additional_themes.push(JSON.parse(readFileSync(extra_theme, "utf8")));
|
||||
}
|
||||
// 处理主题
|
||||
const themes = additional_themes.concat(
|
||||
(typeof config.theme === "string" ? [config.theme] : Object.values(config.theme)).filter(
|
||||
(theme) => theme in bundledThemes
|
||||
@ -97,7 +101,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;
|
||||
@ -109,13 +113,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";
|
||||
}
|
||||
|
||||
@ -131,7 +152,7 @@ export async function init(hexo: Hexo) {
|
||||
};
|
||||
};
|
||||
try {
|
||||
if (config.theme === "string")
|
||||
if (typeof config.theme === "string")
|
||||
pre = highlighter.codeToHtml(code, {
|
||||
lang,
|
||||
theme: config.theme,
|
||||
@ -180,7 +201,9 @@ export async function init(hexo: Hexo) {
|
||||
// 合并标签
|
||||
const html = htmlTag(
|
||||
"figure",
|
||||
{ class: `highlight ${lang}` },
|
||||
{
|
||||
class: `highlight ${useOringinalLangName(lang) ? originalLangName(lang) : lang}`,
|
||||
},
|
||||
caption +
|
||||
htmlTag(
|
||||
"table",
|
||||
|
Loading…
Reference in New Issue
Block a user