hexo-theme-butterfly/scripts/helpers/page.js
Jerry 8014ddb65e feat: 不再支持 butterfly.yml 寫法
improvement: 文章頁標題改為h1(seo)
improvement: 改用hexo 內置的 injector 插入 inject 的代碼
improvement: canonical 改為hexo提供的函數生成
improvement: 優化手機toc 的特效和按鈕出現的時機
remove: 刪除搜索不必要的配置
remove: 移除豆瓣配置,如需配置,請使用 hexo-butterfly-douban
fix: 修復上一個版本導致的搜索按鈕在pjax下無法點擊的bugs close #387
fix: 修復上一個版本更新的toc z-index 過低的bugs
2020-10-15 00:14:38 +08:00

64 lines
1.9 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, 200)
.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 () {
return prettyUrls(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')
})