hexo-theme-butterfly/scripts/helpers/page.js
Jerry 83b7232841 feat: 只支持 hexo 5.0.0 及以上版本
feat: toc改為aside顯示,去掉sidebar
feat: 手機toc ui 修改
feat: 去掉 toc auto_open 配置
feat: 文章標題改為居中
improvement: 完善note標籤在readmode上的顯示
improvement: 精簡css 和 js
improvement: pjax 優化
fix: 修復當文章頁top_img設為false時,閲讀模式下文章內容不顯示的bugs
2020-10-11 18:25:58 +08:00

79 lines
2.2 KiB
JavaScript

/**
* Butterfly
* @example
* page_description()
* injectHtml(data)
* cloudTags(source, minfontsize, maxfontsize, limit)
*/
'use strict'
const { stripHTML, escapeHTML } = 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('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('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 () {
const { permalink } = hexo.config
let url = this.url.replace(/index\.html$/, '')
if (!permalink.endsWith('.html')) {
url = url.replace(/\.html$/, '')
}
return url
})
hexo.extend.helper.register('md5', function (path) {
return crypto.createHash('md5').update(decodeURI(this.url_for(path))).digest('hex')
})