hexo-highlighter-shiki/README.md

137 lines
5.1 KiB
Markdown
Raw Permalink Normal View History

2024-10-19 17:47:59 +08:00
# Hexo-Highlight-Shiki
2024-10-21 19:41:58 +08:00
[![NPM Version](https://img.shields.io/npm/v/hexo-highlighter-shiki?style=flat)](https://www.npmjs.com/package/hexo-highlighter-shiki)
2024-10-19 17:47:59 +08:00
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
2024-10-21 22:31:44 +08:00
- 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).
- Support switching between multiple themes (you need to write the corresponding styles and scripts yourself).
2024-10-19 17:47:59 +08:00
- Support custom language.
2024-10-21 22:31:44 +08:00
- Support custom theme.
- Support transformers in [@shikijs/transformers](https://shiki.style/packages/transformers).
2024-10-19 17:47:59 +08:00
## Installation and Configuration
First, install the plugin:
```shell
npm install hexo-highlighter-shiki --save
2024-10-19 17:47:59 +08:00
```
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
```
2024-10-21 22:31:44 +08:00
and use
```shell
hexo clean && hexo generate
```
to enjoy code highlighting powered by Shiki.
2024-10-19 17:47:59 +08:00
## Configuration Options
The complete configuration is as follows:
```yaml
shiki:
theme: one-dark-pro # Please refer to https://shiki.style/themes for supported themes.
line_number: false # default: false
strip_indent: true # default: true
tab_replace: " " # default: " "
2024-10-21 22:44:35 +08:00
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-
2024-10-21 22:31:44 +08:00
transformers:
# List of transformers to be enabled.
# Please refer to https://shiki.style/packages/transformers for the list of supported transformers.
2024-10-21 22:44:35 +08:00
# 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
2024-10-21 22:31:44 +08:00
option:
2024-10-21 22:44:35 +08:00
# 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
2024-10-21 22:31:44 +08:00
exampleOption1: exampleValue1
exampleOption2: exampleValue2
2024-10-19 17:47:59 +08:00
additional:
themes: # List of the TextMate theme json to be added.
- path/to/theme.json
2024-10-19 17:47:59 +08:00
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
```
2024-10-21 22:44:35 +08:00
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.
### 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):
2024-10-21 22:31:44 +08:00
```markdown
{% codeblock lang:rust mark:2 %}
fn main() {
println!("Hello, world!");
}
{% endcodeblock %}
```
But if you want to mark some lines in backtick code blocks, you can use transformer:
```yaml
transformers:
- name: transformerNotationHighlight
option:
2024-10-21 22:44:35 +08:00
classActiveLine: marked # same to Hexo's code block tag, default: highlighted
classActivePre: "" # default: has-highlighted
2024-10-21 22:31:44 +08:00
```
and add some comment to your code block:
````markdown
```rust
fn main() {
println!("Hello, world!"); // [!code highlight]
}
```
````
The result is the same as the Hexo code block tag plugin.
## Bugs
### mathjax
2024-10-20 21:06:38 +08:00
If you are using [hexo-filter-mathjax](https://github.com/next-theme/hexo-filter-mathjax) or any other plugin that uses mathjax to render mathematical formulas locally, you may encounter an `Error: Can't find handler for document` when rendering articles that include code blocks and have mathjax rendering enabled. This is a problem with mathjax, as its LiteDOM adaptor cannot parse complex HTML fragments.
#### 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
- data.content = mathjax(data.content);
2024-10-21 22:44:35 +08:00
+ data.content = data.content.replace(
+ /<span\s+class="math\s+[^"]*">\\[\(\[].*?\\[\)\]]<\/span>/gs,
+ mathjax);
```
This will prevent rendering errors for complex HTML fragments that result in `Can't find handler for document`.
#### Related Issues:
- [Error in applying html the markdown file #26](https://github.com/next-theme/hexo-filter-mathjax/issues/26)
- [Can't find handler for document #265](https://github.com/mathjax/MathJax-src/issues/265)
2024-10-19 17:47:59 +08:00
## 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)