feat: 優化代碼高亮系統並修復樣式問題 (v5.5.4-b1)

- 簡化 config.pug 中的高亮設置檢測邏輯
- 重構滾動條樣式,提取為可重用的 $scrollbar-style
- 優化 JavaScript 代碼高亮邏輯,增加對默認高亮的支持
- 修復打賞彈窗的邊框圓角應用位置
- 改善代碼高亮工具的檢測和處理流程
This commit is contained in:
Jerry
2026-01-10 13:19:23 +08:00
parent c771efa9bc
commit b0951197a9
7 changed files with 40 additions and 35 deletions

View File

@@ -72,20 +72,16 @@
}) })
} }
let highlight = 'undefined' let highlightProvider = config.syntax_highlighter || (config.highlight.enable ? 'highlight.js' : config.prismjs.enable ? 'prismjs' : null)
let syntaxHighlighter = config.syntax_highlighter const { copy, language, height_limit, fullpage, macStyle, shrink } = theme.code_blocks
let highlightEnable = syntaxHighlighter ? ['highlight.js', 'prismjs'].includes(syntaxHighlighter) : (config.highlight.enable || config.prismjs.enable) let highlight = JSON.stringify({
if (highlightEnable) { plugin: highlightProvider,
const { copy, language, height_limit, fullpage, macStyle } = theme.code_blocks highlightCopy: copy,
highlight = JSON.stringify({ highlightLang: language,
plugin: syntaxHighlighter ? syntaxHighlighter : config.highlight.enable ? 'highlight.js' : 'prismjs', highlightHeightLimit: height_limit,
highlightCopy: copy, highlightFullpage: fullpage,
highlightLang: language, highlightMacStyle: macStyle
highlightHeightLimit: height_limit, })
highlightFullpage: fullpage,
highlightMacStyle: macStyle
})
}
script. script.
const GLOBAL_CONFIG = { const GLOBAL_CONFIG = {

View File

@@ -1,6 +1,6 @@
{ {
"name": "hexo-theme-butterfly", "name": "hexo-theme-butterfly",
"version": "5.5.3", "version": "5.5.4-b1",
"description": "A Simple and Card UI Design theme for Hexo", "description": "A Simple and Card UI Design theme for Hexo",
"main": "package.json", "main": "package.json",
"scripts": { "scripts": {

View File

@@ -24,6 +24,14 @@ wordWrap = $highlight_enable && !$highlight_line_number && hexo-config('code_blo
--hlscrollbar-bg: lighten(#121212, 5) --hlscrollbar-bg: lighten(#121212, 5)
--hlexpand-bg: linear-gradient(180deg, rgba(lighten(#121212, 2), .6), rgba(lighten(#121212, 2), .9)) --hlexpand-bg: linear-gradient(180deg, rgba(lighten(#121212, 2), .6), rgba(lighten(#121212, 2), .9))
$scrollbar-style
// scrollbar - firefox
@-moz-document url-prefix()
scrollbar-color: var(--hlscrollbar-bg) transparent
&::-webkit-scrollbar-thumb
background: var(--hlscrollbar-bg)
if $highlight_enable if $highlight_enable
@require 'highlight/index' @require 'highlight/index'
@@ -88,6 +96,11 @@ $code-block
&:hover &:hover
border-bottom-color: var(--hl-color) border-bottom-color: var(--hl-color)
&.default
pre
padding: 10px 20px
@extend $scrollbar-style
&.copy-true &.copy-true
user-select: all user-select: all
-webkit-user-select: all -webkit-user-select: all

View File

@@ -1,11 +1,6 @@
figure.highlight figure.highlight
table table
// scrollbar - firefox @extend $scrollbar-style
@-moz-document url-prefix()
scrollbar-color: var(--hlscrollbar-bg) transparent
&::-webkit-scrollbar-thumb
background: var(--hlscrollbar-bg)
pre .deletion pre .deletion
color: $highlight-deletion color: $highlight-deletion

View File

@@ -6,12 +6,7 @@ if $highlight_theme != false
.container .container
pre[class*='language-'] pre[class*='language-']
// scrollbar - firefox @extend $scrollbar-style
@-moz-document url-prefix()
scrollbar-color: var(--hlscrollbar-bg) transparent
&::-webkit-scrollbar-thumb
background: var(--hlscrollbar-bg)
&:not(.line-numbers) &:not(.line-numbers)
padding: 10px 20px padding: 10px 20px

View File

@@ -37,13 +37,13 @@
display: none display: none
padding: 0 0 15px padding: 0 0 15px
width: 100% width: 100%
addBorderRadius()
.reward-all .reward-all
display: inline-block display: inline-block
margin: 0 margin: 0
padding: 20px 10px padding: 20px 10px
background: var(--reward-pop) background: var(--reward-pop)
addBorderRadius()
&:before &:before
position: absolute position: absolute

View File

@@ -61,11 +61,14 @@ document.addEventListener('DOMContentLoaded', () => {
const { highlightCopy, highlightLang, highlightHeightLimit, highlightFullpage, highlightMacStyle, plugin } = highLight const { highlightCopy, highlightLang, highlightHeightLimit, highlightFullpage, highlightMacStyle, plugin } = highLight
const isHighlightShrink = GLOBAL_CONFIG_SITE.isHighlightShrink const isHighlightShrink = GLOBAL_CONFIG_SITE.isHighlightShrink
const isShowTool = highlightCopy || highlightLang || isHighlightShrink !== undefined || highlightFullpage || highlightMacStyle const isShowTool = highlightCopy || highlightLang || isHighlightShrink !== undefined || highlightFullpage || highlightMacStyle
const $figureHighlight = plugin === 'highlight.js' ? document.querySelectorAll('figure.highlight') : document.querySelectorAll('pre[class*="language-"]') const isNotHighlightJs = plugin !== 'highlight.js'
const isPrismjs = plugin === 'prismjs'
const $figureHighlight = isNotHighlightJs
? Array.from(document.querySelectorAll('code[class*="language-"]')).map(code => code.parentElement)
: document.querySelectorAll('figure.highlight')
if (!((isShowTool || highlightHeightLimit) && $figureHighlight.length)) return if (!((isShowTool || highlightHeightLimit) && $figureHighlight.length)) return
const isPrismjs = plugin === 'prismjs'
const highlightShrinkClass = isHighlightShrink === true ? 'closed' : '' const highlightShrinkClass = isHighlightShrink === true ? 'closed' : ''
const highlightShrinkEle = isHighlightShrink !== undefined ? '<i class="fas fa-angle-down expand"></i>' : '' const highlightShrinkEle = isHighlightShrink !== undefined ? '<i class="fas fa-angle-down expand"></i>' : ''
const highlightCopyEle = highlightCopy ? '<i class="fas fa-paste copy-button"></i>' : '' const highlightCopyEle = highlightCopy ? '<i class="fas fa-paste copy-button"></i>' : ''
@@ -133,7 +136,7 @@ document.addEventListener('DOMContentLoaded', () => {
const highlightCopyFn = (ele, clickEle) => { const highlightCopyFn = (ele, clickEle) => {
const $buttonParent = ele.parentNode const $buttonParent = ele.parentNode
$buttonParent.classList.add('copy-true') $buttonParent.classList.add('copy-true')
const preCodeSelector = isPrismjs ? 'pre code' : 'table .code pre' const preCodeSelector = isNotHighlightJs ? 'pre code' : 'table .code pre'
const codeElement = $buttonParent.querySelector(preCodeSelector) const codeElement = $buttonParent.querySelector(preCodeSelector)
if (!codeElement) return if (!codeElement) return
copy(codeElement.innerText, clickEle) copy(codeElement.innerText, clickEle)
@@ -213,20 +216,23 @@ document.addEventListener('DOMContentLoaded', () => {
fragment.appendChild(ele) fragment.appendChild(ele)
} }
isPrismjs ? item.parentNode.insertBefore(fragment, item) : item.insertBefore(fragment, item.firstChild) isNotHighlightJs ? item.parentNode.insertBefore(fragment, item) : item.insertBefore(fragment, item.firstChild)
} }
$figureHighlight.forEach(item => { $figureHighlight.forEach(item => {
let langName = '' let langName = ''
if (isPrismjs) btf.wrap(item, 'figure', { class: 'highlight' }) if (isNotHighlightJs) {
const newClassName = isPrismjs ? 'prismjs' : 'default'
btf.wrap(item, 'figure', { class: `highlight ${newClassName}` })
}
if (!highlightLang) { if (!highlightLang) {
createEle('', item) createEle('', item)
return return
} }
if (isPrismjs) { if (isNotHighlightJs) {
langName = item.getAttribute('data-language') || 'Code' langName = isPrismjs ? item.getAttribute('data-language') || 'Code' : item.querySelector('code').getAttribute('class').replace('language-', '')
} else { } else {
langName = item.getAttribute('class').split(' ')[1] langName = item.getAttribute('class').split(' ')[1]
if (langName === 'plain' || langName === undefined) langName = 'Code' if (langName === 'plain' || langName === undefined) langName = 'Code'