hexo-plugin-aurora
Advanced tools
Comparing version
@@ -0,1 +1,13 @@ | ||
## [1.8.2](https://github.com/auroral-ui/hexo-plugin-aurora/compare/v1.8.1...v1.8.2) (2023-09-13) | ||
### Bug Fixes | ||
* add warning for not configuring site language correctly ([4ad57b7](https://github.com/auroral-ui/hexo-plugin-aurora/commit/4ad57b7097261cacb6c1715a4a4c6425f9d0beca)) | ||
### Performance Improvements | ||
* improve performance of shiki progress ([6167806](https://github.com/auroral-ui/hexo-plugin-aurora/commit/6167806aa66319b41f4b70857a95595caa2d895e)) | ||
## [1.8.1](https://github.com/auroral-ui/hexo-plugin-aurora/compare/v1.8.0...v1.8.1) (2023-09-12) | ||
@@ -2,0 +14,0 @@ |
@@ -83,2 +83,12 @@ const chalk = require('chalk'); | ||
if (!Object.keys(QUOTE_LANG).includes(themeConfig.site.language)) { | ||
throwError( | ||
'Aurora Config Error', | ||
`site -> language ${chalk.cyan( | ||
`[${themeConfig.site.language}]` | ||
)} is not supported, please use the support language option instead. Check here: https://aurora.tridiamond.tech/en/configs/general.html#site-configs` | ||
); | ||
return; | ||
} | ||
const lang = themeConfig.site.language; | ||
@@ -85,0 +95,0 @@ const dataContent = data.content; |
const shiki = require('shiki'); | ||
const { renderToHtml, toShikiTheme } = require('shiki'); | ||
const { escapeHTML } = require('hexo-util'); | ||
const { BUNDLED_LANGUAGES, toShikiTheme } = require('shiki'); | ||
const appRootPath = require('app-root-path'); | ||
@@ -10,8 +9,26 @@ const { lex, parse } = require('fenceparser'); | ||
function defaultCodeBlock(code, lang) { | ||
return `<div class="language-${lang}"><button title="Copy code" class="copy"></button><span class="lang">${lang}</span>${escapeHTML( | ||
code | ||
)}</div>`; | ||
const styleRE = /<pre[^>]*(style=".*?")/; | ||
const preRE = /^<pre(.*?)>/; | ||
const lineNoRE = /:(no-)?line-numbers(=\d*)?$/; | ||
const defaultLang = 'txt'; | ||
function cleanup(str, bg) { | ||
let cleanedStr = str.replace( | ||
preRE, | ||
(_, attributes) => `<pre ${vPre}${attributes.replace(' tabindex="0"', '')}>` | ||
); | ||
if (bg) { | ||
cleanedStr = cleanedStr.replace(styleRE, (_, style) => | ||
_.replace(style, bg ? `style="background-color: ${bg}"` : '') | ||
); | ||
} | ||
return cleanedStr; | ||
} | ||
function renderCodeBlock(codeHtml, lang) { | ||
return `<div class="language-${lang}"><button title="Copy code" class="copy"></button><span class="lang">${lang}</span>${codeHtml}</div>`; | ||
} | ||
function filterIntoShiki( | ||
@@ -25,25 +42,22 @@ highlighter, | ||
if (highlighter.getLoadedLanguages().includes(lang)) { | ||
// This will return an array of tokens for the provided code. | ||
// A token represents a single part of the code, for example a keyword, a string, a comment, etc. | ||
const tokens = highlighter.codeToThemedTokens(code, lang, theme); | ||
// | ||
// // This will return an HTML string that represents the provided code. | ||
const codeHtml = renderToHtml(tokens, { | ||
bg: backgroundColor ?? highlighter.getBackgroundColor(theme) | ||
}); | ||
lang = lang.replace(lineNoRE, '').toLowerCase() || defaultLang; | ||
return `<div class="language-${lang}"><button title="Copy code" class="copy"></button><span class="lang">${lang}</span>${codeHtml}</div>`; | ||
if (lang) { | ||
const langLoaded = highlighter.getLoadedLanguages().includes(lang); | ||
if (!langLoaded && lang !== 'ansi' && lang !== 'txt') { | ||
throwWarning( | ||
'Aurora Code Filtering Warning', | ||
`Code language: ${chalk.cyan( | ||
`[${lang}]` | ||
)} is not loaded, falling back to '${defaultLang}' for syntax highlighting.` | ||
); | ||
lang = defaultLang; | ||
} | ||
} | ||
if (lang !== '') { | ||
throwWarning( | ||
'Aurora Code Filtering Warning', | ||
`Code language: ${chalk.cyan(`[${lang}]`)} is not supported.` | ||
); | ||
} else { | ||
lang = 'plaintext'; | ||
} | ||
return defaultCodeBlock(code, lang); | ||
const codeHtml = highlighter.codeToHtml(code, { | ||
lang, | ||
theme | ||
}); | ||
return cleanup(renderCodeBlock(codeHtml, lang), backgroundColor); | ||
} | ||
@@ -89,3 +103,4 @@ | ||
.getHighlighter({ | ||
theme: codeTheme | ||
theme: codeTheme, | ||
langs: [...BUNDLED_LANGUAGES] | ||
}) | ||
@@ -92,0 +107,0 @@ .then((hl) => (highlighter = hl)); |
{ | ||
"name": "hexo-plugin-aurora", | ||
"version": "1.8.1", | ||
"version": "1.8.2", | ||
"description": "A plugin for Hexo Aurora theme", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
72810
1.47%1907
1.11%