Version 1.0.0

This commit is contained in:
HPCesia 2024-10-19 17:47:59 +08:00
commit 7534436b7f
9 changed files with 305 additions and 0 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# VSCode
.vscode
# Build
dist
# Log
*.log
# Node
package-lock.json
node_modules

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024 HPCesia
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

60
README.md Normal file
View File

@ -0,0 +1,60 @@
# Hexo-Highlight-Shiki
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.
## Features
- Render code block with Shiki like Hexo default highlighter.
- Support dual or more themes.
- Support custom language.
## Installation and Configuration
First, install the plugin:
```shell
npm install hexo-highlight-shiki --save
```
Then switch the code highlighting engine in your `config.yml`:
```yaml
syntax_highlighter: shiki
```
Finally, configure `shiki`:
```yaml
shiki:
theme: github-dark # default: one-dark-pro
line_number: true # default: false
```
## Configuration Options
The complete configuration is as follows:
```yaml
shiki:
theme: one-dark-pro # see https://shiki.style/themes for supported themes.
line_number: false
strip_indent: true
tab_replace: " "
additional:
langs: # List of the TextMate grammar json of languages to be added.
- path/to/lang_grammar.json
lang_alias: # List of the alias of languages.
your_alias1: lang_name1
your_alias2: lang_name2
```
Additionally, you can specify multiple themes in the `theme` option:
```yaml
shiki:
theme:
light: one-light
dark: one-dark-pro
# ...
```
See [Dual Themes](https://shiki.style/guide/dual-themes) for how to switch between multiple themes.
## Acknowledgments
This plugin is developed based on
- [ArcticLampyrid/hexo-shiki-highlighter](https://github.com/ArcticLampyrid/hexo-shiki-highlighter)
- [nova1751/hexo-shiki-plugin](https://github.com/nova1751/hexo-shiki-plugin)

61
README_zh-CN.md Normal file
View File

@ -0,0 +1,61 @@
# Hexo-Highlight-Shiki
[English](README.md)丨简体中文
一个使用 [Shiki](https://github.com/shikijs/shiki) 作为代码块高亮器的 Hexo 插件。
需要 Hexo v7.0.0+。
## 功能
- 使用 Shiki 渲染代码块,类似于 Hexo 默认的高亮器。
- 支持双主题或更多主题。
- 支持自定义语言。
## 安装与配置
首先,安装本插件:
```shell
npm install hexo-highlight-shiki --save
```
然后在 `config.yml` 中切换代码高亮引擎:
```yaml
syntax_highlighter: shiki
```
最后配置 `shiki`
```yaml
shiki:
theme: one-dark-pro
```
## 配置项
完整配置如下:
```yaml
shiki:
theme: "one-dark-pro" # Theme, see https://shiki.style/themes for supported themes.
line_number: false
strip_indent: true
tab_replace: " "
additional:
langs: # List of the TextMate grammar json of languages to be added.
- path/to/lang_grammar.json
lang_alias: # List of the alias of languages.
your_alias1: lang_name1
your_alias2: lang_name2
```
额外地,你可以在 `theme` 选项中指定多个主题:
```yaml
shiki:
theme:
light: one-light
dark: one-dark-pro
# ...
```
在 [Dual Themes](https://shiki.style/guide/dual-themes) 中查看如何切换多个主题。
## 感谢
本插件基于
- [ArcticLampyrid/hexo-shiki-highlighter](https://github.com/ArcticLampyrid/hexo-shiki-highlighter)
- [nova1751/hexo-shiki-plugin](https://github.com/nova1751/hexo-shiki-plugin)
进行开发。

12
build.mjs Normal file
View File

@ -0,0 +1,12 @@
import { build } from 'esbuild';
import { promises as fs } from 'fs';
await build({
entryPoints: ['src/main.ts'],
bundle: true,
outdir: 'dist',
platform: 'node',
sourcemap: false,
tsconfig: 'tsconfig.json',
});
await fs.copyFile('src/index.js', 'dist/index.js');

32
package.json Normal file
View File

@ -0,0 +1,32 @@
{
"name": "hexo-highlight-shiki",
"version": "1.0.0",
"description": "A package for Hexo which use Shiki to render code highlight.",
"main": "dist/index.js",
"scripts": {
"build": "node ./build.mjs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/HPCesia/hexo-highlight-shiki.git"
},
"keywords": [
"hexo",
"shiki",
"code",
"highlight"
],
"author": "HPCesia",
"license": "MIT",
"devDependencies": {
"@types/node": "^22.7.6",
"esbuild": "^0.24.0",
"typescript": "^5.6.3"
},
"dependencies": {
"shiki": "^1.22.0"
},
"peerDependencies": {
"hexo": "^7.0.0"
}
}

1
src/index.js Normal file
View File

@ -0,0 +1 @@
await require("./main").init(hexo);

107
src/main.ts Normal file
View File

@ -0,0 +1,107 @@
"use strict";
import { bundledLanguages, createHighlighter, ShikiTransformer } from "shiki";
import type Hexo from "hexo";
import { HighlightOptions } from "hexo/dist/extend/syntax_highlight";
import { stripIndent, htmlTag } from "hexo-util";
import { readFileSync } from "fs";
export async function init(hexo: Hexo) {
const config = Object.assign(
{
theme: "one-dark-pro",
line_number: false,
strip_indent: true,
tab_replace: " ",
additional: {
langs: [],
lang_alias: {},
},
},
hexo.config.shiki || {}
);
let additional_langs = [];
if (config.additional.langs)
for (const extra_lang of [].concat(config.additional.langs)) {
additional_langs.push(JSON.parse(readFileSync(extra_lang, "utf8")));
}
const langs = [...Object.keys(bundledLanguages), ...additional_langs];
let highlighter_options = {
langs: langs,
themes: typeof config.theme === "string" ? [config.theme] : Object.values(config.theme),
};
if (config.additional.lang_alias && Object.keys(config.additional.lang_alias).length > 0) {
highlighter_options["langAliases"] = config.additional.lang_alias;
}
const highlighter = await createHighlighter(highlighter_options);
const supportedLanguages = highlighter.getLoadedLanguages().reduce(
(acc, lang) => {
acc[lang] = true;
return acc;
},
{
text: true,
plain: true,
ansi: true,
}
);
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]) {
lang = "text";
}
let pre = "";
const transformer = (): ShikiTransformer => {
return {
line(node, line) {
if (options.mark && options.mark.includes(line)) {
this.addClassToHast(node, "marked");
}
},
};
};
try {
if (config.theme === "string")
pre = highlighter.codeToHtml(code, {
lang,
theme: config.theme,
transformers: [transformer()],
});
else
pre = highlighter.codeToHtml(code, {
lang,
themes: config.theme,
transformers: [transformer()],
});
pre = pre.replace(/<pre[^>]*>/, "<pre>");
pre = pre.replace(/<\/?code>/, "");
} catch (error) {
console.warn(error);
pre = htmlTag("pre", {}, code);
}
let numbers = "";
if (config.line_number) {
for (let i = 0, len = code.split("\n").length; i < len; i++) {
numbers += htmlTag("span", { class: "line" }, `${1 + i}`, false) + "<br>";
}
numbers = htmlTag("pre", {}, numbers, false);
}
const td_code = htmlTag("td", { class: "code" }, pre, false);
const td_gutter = htmlTag("td", { class: "gutter" }, numbers, false);
const html = htmlTag(
"figure",
{ class: `highlight ${lang}` },
htmlTag(
"table",
{},
htmlTag("tbody", {}, htmlTag("tr", {}, td_gutter + td_code, false), false),
false
),
false
);
return html;
};
hexo.extend.highlight.register("shiki", hexoHighlighter);
}

11
tsconfig.json Normal file
View File

@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"types": [
"node"
]
}
}