mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2025-09-15 12:58:48 +08:00
feat: 增加 waline avatarCDN 配置(最新評論也會更新) feat: archives 的 標題 在 menu 配置中獲取 fix: 修復文章頁 mathjax 因字體小導致顯示重疊的 bug close #587 fix: 修復 mathjax 在列表下,導致 列表沒有樣式的 bug close #586 improvement: 減少卡頓,apple 裝置首頁背景 background-attachment 設為 scroll
92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
/**
|
|
* Butterfly
|
|
* @example
|
|
* page_description()
|
|
* cloudTags(source, minfontsize, maxfontsize, limit)
|
|
*/
|
|
|
|
'use strict'
|
|
|
|
const { stripHTML, escapeHTML, prettyUrls } = require('hexo-util')
|
|
const crypto = require('crypto')
|
|
|
|
hexo.extend.helper.register('page_description', function () {
|
|
const { config, page } = this
|
|
let description = page.description || page.content || page.title || config.description
|
|
|
|
if (description) {
|
|
description = escapeHTML(stripHTML(description).substring(0, 150)
|
|
.trim()
|
|
).replace(/\n/g, ' ')
|
|
return description
|
|
}
|
|
})
|
|
|
|
hexo.extend.helper.register('cloudTags', function (options = {}) {
|
|
const env = this
|
|
let source = options.source
|
|
const minfontsize = options.minfontsize
|
|
const maxfontsize = options.maxfontsize
|
|
const limit = options.limit
|
|
const unit = options.unit || 'px'
|
|
|
|
let result = ''
|
|
if (limit > 0) {
|
|
source = source.limit(limit)
|
|
}
|
|
|
|
const sizes = []
|
|
source.sort('length').forEach(tag => {
|
|
const { length } = tag
|
|
if (sizes.includes(length)) return
|
|
sizes.push(length)
|
|
})
|
|
|
|
const length = sizes.length - 1
|
|
source.forEach(tag => {
|
|
const ratio = length ? sizes.indexOf(tag.length) / length : 0
|
|
const size = minfontsize + ((maxfontsize - minfontsize) * ratio)
|
|
let style = `font-size: ${parseFloat(size.toFixed(2))}${unit};`
|
|
const color = 'rgb(' + Math.floor(Math.random() * 201) + ', ' + Math.floor(Math.random() * 201) + ', ' + Math.floor(Math.random() * 201) + ')' // 0,0,0 -> 200,200,200
|
|
style += ` color: ${color}`
|
|
result += `<a href="${env.url_for(tag.path)}" style="${style}">${tag.name}</a>`
|
|
})
|
|
return result
|
|
})
|
|
|
|
hexo.extend.helper.register('urlNoIndex', function (url = null) {
|
|
return prettyUrls(url || this.url, { trailing_index: false, trailing_html: false })
|
|
})
|
|
|
|
hexo.extend.helper.register('md5', function (path) {
|
|
return crypto.createHash('md5').update(decodeURI(this.url_for(path))).digest('hex')
|
|
})
|
|
|
|
hexo.extend.helper.register('injectHtml', function (data) {
|
|
let result = ''
|
|
if (!data) return ''
|
|
for (let i = 0; i < data.length; i++) {
|
|
result += data[i]
|
|
}
|
|
return result
|
|
})
|
|
|
|
hexo.extend.helper.register('findArchivesTitle', function (menu) {
|
|
const defaultTitle = this._p('page.archives')
|
|
if (!menu) return defaultTitle
|
|
|
|
const loop = (m) => {
|
|
for (const key in m) {
|
|
if (typeof m[key] === 'object') {
|
|
loop(m[key])
|
|
}
|
|
|
|
if (/\/archives\//.test(m[key])) {
|
|
return key
|
|
}
|
|
}
|
|
}
|
|
|
|
return loop(menu) || defaultTitle
|
|
})
|