为Hexo增加随机图API支持
将根目录下创建scripts/filters/random_cover.js文件,内容为
/**
* Butterfly
* ramdom cover
*/
'use strict'
hexo.extend.filter.register('before_post_render', function (data) {
const { config } = this
if (config.post_asset_folder) {
const imgTestReg = /\.(png|jpe?g|gif|svg|webp)(\?.*)?$/
const topImg = data.top_img
const cover = data.cover
if (topImg && topImg.indexOf('/') === -1 && imgTestReg.test(topImg)) data.top_img = data.path + topImg
if (cover && cover.indexOf('/') === -1) data.cover = data.path + cover
}
if (data.cover === false) {
data.randomcover = randomCover()
return data
}
data.cover = data.cover || randomCover()
return data
},999)// 改成最高优先级,最后执行
function randomCover () {
const theme = hexo.theme.config
let cover
let num
if (theme.cover && theme.cover.default_cover) {
if (!Array.isArray(theme.cover.default_cover)) {
cover = theme.cover.default_cover
} else {
num = Math.floor(Math.random() * theme.cover.default_cover.length)
cover = theme.cover.default_cover[num]
}
} else {
cover = theme.default_top_img || 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
}
if(theme.cover.suffix){
const randomNum = Date.now().toString(36) + Math.random().toString(36).slice(2)
if(theme.cover.suffix == 1)
cover = cover + '?' + randomNum
else if(theme.cover.suffix == 2)
cover = cover + '&' + randomNum
}
return cover
}
打开butterfly主题配置文件:在 cover: 插入suffix: 1 并保存(目的是在链接后面加入后缀 ?随机数 0 是不使用后缀;1 是?加随机数;2 是&加随机数)
参考文章
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 雯欂の修仙笔记!