feat: support to show the original name of language in the class of figure tag

This commit is contained in:
HPCesia 2024-10-24 16:11:45 +08:00
parent 6788dc4839
commit e1d2442f7c
3 changed files with 20 additions and 3 deletions

View File

@ -45,6 +45,11 @@ shiki:
line_number: false # default: false line_number: false # default: false
strip_indent: true # default: true strip_indent: true # default: true
tab_replace: " " # default: " " 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 pre_style: true # Preserve the style of the <pre> tag. default: true
default_color: light # Only take effect when using multiple themes. default: light 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- css_variable_prefix: --shiki- # Only take effect when using multiple themes. default: --shiki-
@ -117,7 +122,7 @@ If you are using [hexo-filter-mathjax](https://github.com/next-theme/hexo-filter
#### Solution #### 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: 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 = mathjax(data.content);
+ data.content = data.content.replace( + data.content = data.content.replace(
+ /<span\s+class="math\s+[^"]*">\\[\(\[].*?\\[\)\]]<\/span>/gs, + /<span\s+class="math\s+[^"]*">\\[\(\[].*?\\[\)\]]<\/span>/gs,

View File

@ -44,6 +44,11 @@ shiki:
line_number: false line_number: false
strip_indent: true strip_indent: true
tab_replace: " " 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` pre_style: true # 保留 <pre> 标签的样式,即主题的 `background-color`
default_color: light # 仅在同时使用多个主题时生效。默认值light default_color: light # 仅在同时使用多个主题时生效。默认值light
css_variable_prefix: --shiki- # 仅在同时使用多个主题时生效。默认值:--shiki- css_variable_prefix: --shiki- # 仅在同时使用多个主题时生效。默认值:--shiki-
@ -110,7 +115,7 @@ fn main() {
#### 解决方法 #### 解决方法
以 hexo-filter-mathjax 插件为例,修改源代码中的[这一行](https://github.com/next-theme/hexo-filter-mathjax/blob/20dc61352f8cf4d19425ad1833eb72b467c212ef/index.js#L20C3-L20C40): 以 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 = mathjax(data.content);
+ data.content = data.content.replace( + data.content = data.content.replace(
+ /<span\s+class="math\s+[^"]*">\\[\(\[].*?\\[\)\]]<\/span>/gs, + /<span\s+class="math\s+[^"]*">\\[\(\[].*?\\[\)\]]<\/span>/gs,

View File

@ -39,10 +39,13 @@ export async function init(hexo: Hexo) {
line_number: false, line_number: false,
strip_indent: true, strip_indent: true,
tab_replace: " ", tab_replace: " ",
original_lang_name: false,
pre_style: true, pre_style: true,
default_color: "light", default_color: "light",
css_variable_prefix: "--shiki-", css_variable_prefix: "--shiki-",
transformers: [], transformers: [],
// TODO: 增加 `wrap` 选项,与 Hexo 的 highlight.js 渲染行为一致
// TODO: 增加 `beautify` 选项以适配更多主题
additional: { additional: {
themes: [], themes: [],
langs: [], langs: [],
@ -180,7 +183,11 @@ export async function init(hexo: Hexo) {
// 合并标签 // 合并标签
const html = htmlTag( const html = htmlTag(
"figure", "figure",
{ class: `highlight ${lang}` }, {
class: `highlight ${
config.original_lang_name && lang !== "text" ? highlighter.getLanguage(lang).name : lang
}`,
},
caption + caption +
htmlTag( htmlTag(
"table", "table",