Compare commits

...

8 Commits
1.1.1 ... main

Author SHA1 Message Date
66d07bb2a5 bump version 2024-11-13 17:56:20 +08:00
28035a8513 更换 action 2024-11-13 17:53:31 +08:00
98ff7de56a 修复单主题时的 BUG 2024-11-13 17:41:38 +08:00
280c69586f Refine build configuration to exclude hexo from bundle. 2024-11-09 12:16:26 +08:00
HPCesia
68115abd81 chore: Update READMEs to specify Hexo dependency
- Added link to Hexo repository
- Clarified comment on `original_lang_name` in English version
2024-10-29 14:14:06 +08:00
HPCesia
c111ee0243 chore: bump package.json version 2024-10-29 13:54:55 +08:00
HPCesia
2a267a8651 feat: can specify some languages individually in original_lang_name to not convert or only convert certain languages 2024-10-29 13:51:55 +08:00
HPCesia
e1d2442f7c feat: support to show the original name of language in the class of figure tag 2024-10-24 16:11:45 +08:00
6 changed files with 96 additions and 30 deletions

View File

@ -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 }}

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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"
}
}

View File

@ -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",