hexo-theme-butterfly/scripts/helpers/page.js
Jerry 4d34208391 💥 支持雙評論系統配置/評論配置整理
 增加mac light 代碼框
 增加文章過期提醒配置
 文章copyright 可單獨配置
 card-category 中category名稱和數字限制一行顯示
 簡繁轉換優化,placeholder文字也會被轉換
🐛 修復配置PWA參數時,如果網站是子目錄時,會出現加載不到文件的bugs
調整當card-category有收縮按鈕時的顯示佈局
2020-06-25 23:13:55 +08:00

59 lines
1.7 KiB
JavaScript

/**
* Butterfly
* @example
* page_description()
* injectHtml(data)
* cloudTags(source, minfontsize, maxfontsize, limit)
*/
'use strict'
const { stripHTML, escapeHTML } = require('hexo-util')
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
const source = options.source
const minfontsize = options.minfontsize
const maxfontsize = options.maxfontsize
const limit = options.limit
let result = ''
const tagLimit = limit === 0 ? source.length : limit
source.sort('name').limit(tagLimit).forEach(function (tags) {
const fontSize = Math.floor(Math.random() * (maxfontsize - minfontsize) + minfontsize) + 'px'
const color = 'rgb(' + Math.floor(Math.random() * 201) + ', ' + Math.floor(Math.random() * 201) + ', ' + Math.floor(Math.random() * 201) + ')' // 0,0,0 -> 200,200,200
result += `<a href='${env.url_for(tags.path)}' style='font-size:${fontSize}; color:${color}'>${tags.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
})