feat: 修改 toc 打開效果

feat: 適配 hexo-blog-encrypt
fix: 修復 hexo 新版本下,prismjs 的問題
improvement: 在 pjax 關閉的情況下。減少不必要的全局變量
improvement: 更新依賴版本
improvement: 頁面進入效果優化
improvement: 添加平滑滾動
improvement: 兼容性優化
improvement: 優化 lighthouse 分數
improvement: 優化代碼
This commit is contained in:
Jerry
2024-04-02 23:44:46 +08:00
Unverified
parent ee6b137e94
commit 9e0dce6c7a
41 changed files with 601 additions and 536 deletions

View File

@@ -3,13 +3,15 @@ script.
const abcjsInit = () => {
const abcjsFn = () => {
document.querySelectorAll(".abc-music-sheet").forEach(ele => {
if (ele.children.length > 0) return
ABCJS.renderAbc(ele, ele.innerHTML, {responsive: 'resize'})
})
}
typeof ABCJS === 'object' ? abcjsFn()
: getScript('!{url_for(theme.asset.abcjs_basic_js)}').then(abcjsFn)
: btf.getScript('!{url_for(theme.asset.abcjs_basic_js)}').then(abcjsFn)
}
window.pjax ? abcjsInit() : window.addEventListener('load', abcjsInit)
btf.addGlobalFn('encrypt', abcjsInit, 'abcjs')
})()

View File

@@ -1,3 +1,23 @@
link(rel='stylesheet' href=url_for(theme.asset.aplayer_css) media="print" onload="this.media='all'")
script(src=url_for(theme.asset.aplayer_js))
script(src=url_for(theme.asset.meting_js))
script(src=url_for(theme.asset.meting_js))
if theme.pjax.enable
script.
(() => {
const destroyAplayer = () => {
if (window.aplayers) {
for (let i = 0; i < window.aplayers.length; i++) {
if (!window.aplayers[i].options.fixed) {
window.aplayers[i].destroy()
}
}
}
}
const loadMeting = () => {
typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()
}
btf.addGlobalFn('pjaxSend', destroyAplayer, 'destroyAplayer')
btf.addGlobalFn('pjaxComplete', loadMeting, 'loadMeting')
})()

View File

@@ -28,7 +28,7 @@ script.
if (typeof twikoo === 'object') {
runTwikoo()
} else {
getScript('!{url_for(theme.asset.twikoo)}').then(runTwikoo)
btf.getScript('!{url_for(theme.asset.twikoo)}').then(runTwikoo)
}
}

View File

@@ -13,7 +13,7 @@ script.
}
if (typeof Valine === 'function') initValine()
else getScript('!{url_for(theme.asset.valine)}').then(initValine)
else btf.getScript('!{url_for(theme.asset.valine)}').then(initValine)
}
window.pjax ? loadValine() : window.addEventListener('load', loadValine)

View File

@@ -31,8 +31,8 @@ script.
const loadArtalk = async () => {
if (typeof Artalk === 'object') initArtalk()
else {
await getCSS('!{theme.asset.artalk_css}')
await getScript('!{theme.asset.artalk_js}')
await btf.getCSS('!{theme.asset.artalk_css}')
await btf.getScript('!{theme.asset.artalk_js}')
initArtalk()
}
}

View File

@@ -28,8 +28,8 @@ script.
const loadDisqusjs = async() => {
if (window.disqusJsLoad) initDisqusjs()
else {
await getCSS('!{url_for(theme.asset.disqusjs_css)}')
await getScript('!{url_for(theme.asset.disqusjs)}')
await btf.getCSS('!{url_for(theme.asset.disqusjs_css)}')
await btf.getScript('!{url_for(theme.asset.disqusjs)}')
initDisqusjs()
window.disqusJsLoad = true
}

View File

@@ -19,8 +19,8 @@ script.
const loadGitalk = async() => {
if (typeof Gitalk === 'function') initGitalk()
else {
await getCSS('!{url_for(theme.asset.gitalk_css)}')
await getScript('!{url_for(theme.asset.gitalk)}')
await btf.getCSS('!{url_for(theme.asset.gitalk_css)}')
await btf.getScript('!{url_for(theme.asset.gitalk)}')
initGitalk()
}
}

View File

@@ -33,7 +33,7 @@ script.
const loadTwikoo = () => {
if (typeof twikoo === 'object') setTimeout(init,0)
else getScript('!{url_for(theme.asset.twikoo)}').then(init)
else btf.getScript('!{url_for(theme.asset.twikoo)}').then(init)
}
if ('!{use[0]}' === 'Twikoo' || !!{lazyload}) {

View File

@@ -23,7 +23,7 @@ script.
const loadValine = async () => {
if (typeof Valine === 'function') initValine()
else {
await getScript('!{url_for(theme.asset.valine)}')
await btf.getScript('!{url_for(theme.asset.valine)}')
initValine()
}
}

View File

@@ -22,14 +22,16 @@ script.
btf.addGlobalFn('pjax', destroyWaline, 'destroyWaline')
}
const loadWaline = async () => {
const loadWaline = () => {
if (initFn) initWaline(initFn)
else {
await getCSS('!{url_for(theme.asset.waline_css)}')
const { init } = await import('!{url_for(theme.asset.waline_js)}')
initFn = init || Waline.init
initWaline(initFn)
window.walineFn = initFn
btf.getCSS('!{url_for(theme.asset.waline_css)}')
.then(() => import('!{url_for(theme.asset.waline_js)}'))
.then(({ init }) => {
initFn = init || Waline.init
initWaline(initFn)
window.walineFn = initFn
})
}
}

View File

@@ -1,9 +1,2 @@
link(rel="stylesheet" type="text/css" href=url_for(theme.asset.katex))
script(src=url_for(theme.asset.katex_copytex))
script.
(() => {
document.querySelectorAll('#article-container span.katex-display').forEach(item => {
btf.wrap(item, 'div', { class: 'katex-wrap'})
})
})()
script(src=url_for(theme.asset.katex_copytex))

View File

@@ -1,38 +1,45 @@
//- Mathjax 3
script.
if (!window.MathJax) {
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
tags: 'ams'
},
chtml: {
scale: 1.1
},
options: {
renderActions: {
findScript: [10, doc => {
for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
const display = !!node.type.match(/; *mode=display/)
const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
const text = document.createTextNode('')
node.parentNode.replaceChild(text, node)
math.start = {node: text, delim: '', n: 0}
math.end = {node: text, delim: '', n: 0}
doc.math.push(math)
(() => {
const loadMathjax = () => {
if (!window.MathJax) {
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
tags: 'ams'
},
chtml: {
scale: 1.1
},
options: {
renderActions: {
findScript: [10, doc => {
for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
const display = !!node.type.match(/; *mode=display/)
const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
const text = document.createTextNode('')
node.parentNode.replaceChild(text, node)
math.start = {node: text, delim: '', n: 0}
math.end = {node: text, delim: '', n: 0}
doc.math.push(math)
}
}, '']
}
}, '']
}
}
const script = document.createElement('script')
script.src = '!{url_for(theme.asset.mathjax)}'
script.id = 'MathJax-script'
script.async = true
document.head.appendChild(script)
} else {
MathJax.startup.document.state(0)
MathJax.texReset()
MathJax.typesetPromise()
}
}
const script = document.createElement('script')
script.src = '!{url_for(theme.asset.mathjax)}'
script.id = 'MathJax-script'
script.async = true
document.head.appendChild(script)
} else {
MathJax.startup.document.state(0)
MathJax.texReset()
MathJax.typesetPromise()
}
btf.addGlobalFn('encrypt', loadMathjax, 'mathjax')
window.pjax ? loadMathjax() : window.addEventListener('load', loadMathjax)
})()

View File

@@ -1,12 +1,10 @@
script.
(() => {
const $mermaid = document.querySelectorAll('#article-container .mermaid-wrap')
if ($mermaid.length === 0) return
const runMermaid = () => {
const runMermaid = (ele) => {
window.loadMermaid = true
const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? '!{theme.mermaid.theme.dark}' : '!{theme.mermaid.theme.light}'
Array.from($mermaid).forEach((item, index) => {
Array.from(ele).forEach((item, index) => {
const mermaidSrc = item.firstElementChild
const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
const mermaidID = 'mermaid-' + index
@@ -29,10 +27,14 @@ script.
}
const loadMermaid = () => {
window.loadMermaid ? runMermaid() : getScript('!{url_for(theme.asset.mermaid)}').then(runMermaid)
const $mermaid = document.querySelectorAll('#article-container .mermaid-wrap')
if ($mermaid.length === 0) return
const runMermaidFn = () => runMermaid($mermaid)
btf.addGlobalFn('themeChange', runMermaidFn, 'mermaid')
window.loadMermaid ? runMermaidFn() : btf.getScript('!{url_for(theme.asset.mermaid)}').then(runMermaidFn)
}
btf.addGlobalFn('themeChange', runMermaid, 'mermaid')
btf.addGlobalFn('encrypt', loadMermaid, 'mermaid')
window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
})()

View File

@@ -81,7 +81,7 @@ script.
'date': e.date,
}
})
saveToLocal.set('artalk-newest-comments', JSON.stringify(artalk), !{theme.newest_comments.storage}/(60*24))
btf.saveToLocal.set('artalk-newest-comments', JSON.stringify(artalk), !{theme.newest_comments.storage}/(60*24))
generateHtml(artalk)
} catch (e) {
console.log(e)
@@ -92,7 +92,7 @@ script.
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('artalk-newest-comments')
const data = btf.saveToLocal.get('artalk-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
@@ -102,5 +102,6 @@ script.
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
btf.addGlobalFn('pjaxComplete', newestCommentInit, 'artalk_newestComment')
})

View File

@@ -28,7 +28,7 @@ script.
}
})
saveToLocal.set('disqus-newest-comments', JSON.stringify(disqusArray), !{theme.newest_comments.storage}/(60*24))
btf.saveToLocal.set('disqus-newest-comments', JSON.stringify(disqusArray), !{theme.newest_comments.storage}/(60*24))
generateHtml(disqusArray)
}).catch(e => {
const $dom = document.querySelector('#card-newest-comments .aside-list')
@@ -65,7 +65,7 @@ script.
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('disqus-newest-comments')
const data = btf.saveToLocal.get('disqus-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
@@ -75,7 +75,7 @@ script.
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
btf.addGlobalFn('pjaxComplete', newestCommentInit, 'disqus_newestComment')
})

View File

@@ -35,7 +35,7 @@ script.
}
})
saveToLocal.set('github-newest-comments', JSON.stringify(array), !{theme.newest_comments.storage}/(60*24))
btf.saveToLocal.set('github-newest-comments', JSON.stringify(array), !{theme.newest_comments.storage}/(60*24))
generateHtml(array)
});
}
@@ -94,7 +94,7 @@ script.
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('github-newest-comments')
const data = btf.saveToLocal.get('github-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
@@ -104,7 +104,7 @@ script.
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
btf.addGlobalFn('pjaxComplete', newestCommentInit, 'github_newestComment')
})

View File

@@ -56,7 +56,7 @@ script.
'date': e.time,
}
})
saveToLocal.set('remark42-newest-comments', JSON.stringify(remark42), !{theme.newest_comments.storage}/(60*24))
btf.saveToLocal.set('remark42-newest-comments', JSON.stringify(remark42), !{theme.newest_comments.storage}/(60*24))
generateHtml(remark42)
}).catch(e => {
const $dom = document.querySelector('#card-newest-comments .aside-list')
@@ -66,7 +66,7 @@ script.
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('remark42-newest-comments')
const data = btf.saveToLocal.get('remark42-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
@@ -76,5 +76,5 @@ script.
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
btf.addGlobalFn('pjaxComplete', newestCommentInit, 'remark42_newestComment')
})

View File

@@ -32,7 +32,7 @@ script.
}
})
saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), !{theme.newest_comments.storage}/(60*24))
btf.saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), !{theme.newest_comments.storage}/(60*24))
generateHtml(twikooArray)
}).catch(function (err) {
const $dom = document.querySelector('#card-newest-comments .aside-list')
@@ -43,7 +43,7 @@ script.
if (typeof twikoo === 'object') {
runTwikoo()
} else {
getScript('!{url_for(theme.asset.twikoo)}').then(runTwikoo)
btf.getScript('!{url_for(theme.asset.twikoo)}').then(runTwikoo)
}
}
@@ -76,7 +76,7 @@ script.
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('twikoo-newest-comments')
const data = btf.saveToLocal.get('twikoo-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
@@ -86,7 +86,7 @@ script.
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
btf.addGlobalFn('pjaxComplete', newestCommentInit, 'twikoo_newestComment')
})

View File

@@ -75,7 +75,7 @@ script.
'date': e.updatedAt,
}
})
saveToLocal.set('valine-newest-comments', JSON.stringify(valineArray), !{theme.newest_comments.storage}/(60*24))
btf.saveToLocal.set('valine-newest-comments', JSON.stringify(valineArray), !{theme.newest_comments.storage}/(60*24))
generateHtml(valineArray)
}).catch(e => {
const $dom = document.querySelector('#card-newest-comments .aside-list')
@@ -85,7 +85,7 @@ script.
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('valine-newest-comments')
const data = btf.saveToLocal.get('valine-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
@@ -95,5 +95,5 @@ script.
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
btf.addGlobalFn('pjaxComplete', newestCommentInit, 'valine_newestComment')
})

View File

@@ -56,7 +56,7 @@ script.
'date': e.time || e.insertedAt
}
})
saveToLocal.set('waline-newest-comments', JSON.stringify(walineArray), !{theme.newest_comments.storage}/(60*24))
btf.saveToLocal.set('waline-newest-comments', JSON.stringify(walineArray), !{theme.newest_comments.storage}/(60*24))
generateHtml(walineArray)
} catch (err) {
console.error(err)
@@ -67,7 +67,7 @@ script.
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('waline-newest-comments')
const data = btf.saveToLocal.get('waline-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
@@ -77,5 +77,5 @@ script.
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
btf.addGlobalFn('pjaxComplete', newestCommentInit, 'waline_newestComment')
})

View File

@@ -1,20 +1,23 @@
script.
function panguFn () {
if (typeof pangu === 'object') pangu.autoSpacingPage()
else {
getScript('!{url_for(theme.asset.pangu)}')
.then(() => {
pangu.autoSpacingPage()
})
(() => {
const panguFn = () => {
if (typeof pangu === 'object') pangu.autoSpacingPage()
else {
btf.getScript('!{url_for(theme.asset.pangu)}')
.then(() => {
pangu.autoSpacingPage()
})
}
}
}
function panguInit () {
if (!{theme.pangu.field === 'post'}){
GLOBAL_CONFIG_SITE.isPost && panguFn()
} else {
panguFn()
const panguInit = () => {
if (!{theme.pangu.field === 'post'}){
GLOBAL_CONFIG_SITE.isPost && panguFn()
} else {
panguFn()
}
}
}
document.addEventListener('DOMContentLoaded', panguInit)
btf.addGlobalFn('pjaxComplete', panguInit, 'pangu')
document.addEventListener('DOMContentLoaded', panguInit)
})()

View File

@@ -24,34 +24,25 @@ script.
scrollRestoration: false
})
const triggerPjaxFn = (val) => {
if (!val) return
Object.values(val).forEach(fn => { fn() })
}
document.addEventListener('pjax:send', function () {
// removeEventListener
btf.removeGlobalFnEvent('pjax')
btf.removeGlobalFnEvent('themeChange')
document.getElementById('rightside').classList.remove('rightside-show')
if (window.aplayers) {
for (let i = 0; i < window.aplayers.length; i++) {
if (!window.aplayers[i].options.fixed) {
window.aplayers[i].destroy()
}
}
}
typeof typed === 'object' && typed.destroy()
//reset readmode
const $bodyClassList = document.body.classList
$bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')
typeof disqusjs === 'object' && disqusjs.destroy()
triggerPjaxFn(window.globalFn.pjaxSend)
})
document.addEventListener('pjax:complete', function () {
window.refreshFn()
document.addEventListener('pjax:complete', () => {
document.querySelectorAll('script[data-pjax]').forEach(item => {
const newScript = document.createElement('script')
const content = item.text || item.textContent || item.innerHTML || ""
@@ -60,20 +51,7 @@ script.
item.parentNode.replaceChild(newScript, item)
})
GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()
typeof panguInit === 'function' && panguInit()
// google analytics
typeof gtag === 'function' && gtag('config', '!{theme.google_analytics}', {'page_path': window.location.pathname});
// baidu analytics
typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);
typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()
// prismjs
typeof Prism === 'object' && Prism.highlightAll()
triggerPjaxFn(window.globalFn.pjaxComplete)
})
document.addEventListener('pjax:error', e => {

View File

@@ -1,5 +1,23 @@
if config.prismjs && config.prismjs.enable && !config.prismjs.preprocess
script(src=url_for(theme.asset.prismjs_js))
script(src=url_for(theme.asset.prismjs_autoloader))
if config.prismjs.line_number
script(src=url_for(theme.asset.prismjs_lineNumber_js))
- const { prismjs_js, prismjs_autoloader, prismjs_lineNumber_js } = theme.asset
- const { prismjs, syntax_highlighter } = config
- const { enable, preprocess, line_number } = prismjs
if (syntax_highlighter === 'prismjs' || enable) && !preprocess
script.
(() => {
window.Prism = window.Prism || {}
window.Prism.manual = true
const highlightAll = () => {
window.Prism.highlightAll()
}
window.addEventListener('load', highlightAll)
btf.addGlobalFn('pjaxComplete', highlightAll, 'prismjs')
btf.addGlobalFn('encrypt', highlightAll, 'prismjs')
})()
script(src=url_for(prismjs_js))
script(src=url_for(prismjs_autoloader))
if (line_number)
script(src=url_for(prismjs_lineNumber_js))

View File

@@ -17,13 +17,14 @@ script.
if (typeof Typed === 'function') {
subtitleType()
} else {
getScript('!{url_for(theme.asset.typed)}').then(subtitleType)
btf.getScript('!{url_for(theme.asset.typed)}').then(subtitleType)
}
} else {
subtitleType()
}
}
}
btf.addGlobalFn('pjaxSend', () => { typed.destroy() }, 'typedDestroy')
case source
when 1
@@ -47,7 +48,7 @@ case source
when 2
script.
function subtitleType () {
getScript('https://yijuzhan.com/api/word.php?m=js').then(() => {
btf.getScript('https://yijuzhan.com/api/word.php?m=js').then(() => {
const con = str[0]
if (!{effect}) {
const from = '出自 ' + str[1]
@@ -64,7 +65,7 @@ case source
when 3
script.
function subtitleType () {
getScript('https://sdk.jinrishici.com/v2/browser/jinrishici.js').then(() => {
btf.getScript('https://sdk.jinrishici.com/v2/browser/jinrishici.js').then(() => {
jinrishici.load(result =>{
if (!{effect}) {
const sub = !{JSON.stringify(subContent)}