mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-10 21:17:07 +08:00
fix: 修復隨機封面死循環的問題
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hexo-theme-butterfly",
|
"name": "hexo-theme-butterfly",
|
||||||
"version": "5.3.0",
|
"version": "5.3.1",
|
||||||
"description": "A Simple and Card UI Design theme for Hexo",
|
"description": "A Simple and Card UI Design theme for Hexo",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -5,23 +5,32 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
hexo.extend.generator.register('post', locals => {
|
hexo.extend.generator.register('post', locals => {
|
||||||
const recentCovers = []
|
const previousIndexes = []
|
||||||
const randomCoverFn = () => {
|
|
||||||
const { cover: { default_cover: defaultCover } } = hexo.theme.config
|
const getRandomCover = defaultCover => {
|
||||||
if (!defaultCover) return false
|
if (!defaultCover) return false
|
||||||
if (!Array.isArray(defaultCover)) return defaultCover
|
if (!Array.isArray(defaultCover)) return defaultCover
|
||||||
const defaultCoverLen = defaultCover.length
|
|
||||||
const limit = 3
|
|
||||||
|
|
||||||
let num
|
const coverCount = defaultCover.length
|
||||||
|
|
||||||
|
if (coverCount === 1) {
|
||||||
|
return defaultCover[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxPreviousIndexes = coverCount === 2 ? 1 : (coverCount === 3 ? 2 : 3)
|
||||||
|
|
||||||
|
let index
|
||||||
do {
|
do {
|
||||||
num = Math.floor(Math.random() * defaultCoverLen)
|
index = Math.floor(Math.random() * coverCount)
|
||||||
} while (recentCovers.includes(num))
|
} while (previousIndexes.includes(index) && previousIndexes.length < coverCount)
|
||||||
|
|
||||||
recentCovers.push(num)
|
previousIndexes.push(index)
|
||||||
if (recentCovers.length > limit) recentCovers.shift()
|
if (previousIndexes.length > maxPreviousIndexes) {
|
||||||
|
previousIndexes.shift()
|
||||||
|
}
|
||||||
|
|
||||||
return defaultCover[num]
|
console.log(defaultCover[index])
|
||||||
|
return defaultCover[index]
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleImg = data => {
|
const handleImg = data => {
|
||||||
@@ -30,15 +39,20 @@ hexo.extend.generator.register('post', locals => {
|
|||||||
|
|
||||||
// Add path to top_img and cover if post_asset_folder is enabled
|
// Add path to top_img and cover if post_asset_folder is enabled
|
||||||
if (hexo.config.post_asset_folder) {
|
if (hexo.config.post_asset_folder) {
|
||||||
if (topImg && topImg.indexOf('/') === -1 && imgTestReg.test(topImg)) data.top_img = `${data.path}${topImg}`
|
if (topImg && topImg.indexOf('/') === -1 && imgTestReg.test(topImg)) {
|
||||||
if (coverVal && coverVal.indexOf('/') === -1 && imgTestReg.test(coverVal)) data.cover = `${data.path}${coverVal}`
|
data.top_img = `${data.path}${topImg}`
|
||||||
|
}
|
||||||
|
if (coverVal && coverVal.indexOf('/') === -1 && imgTestReg.test(coverVal)) {
|
||||||
|
data.cover = `${data.path}${coverVal}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (coverVal === false) return data
|
if (coverVal === false) return data
|
||||||
|
|
||||||
// If cover is not set, use random cover
|
// If cover is not set, use random cover
|
||||||
if (!coverVal) {
|
if (!coverVal) {
|
||||||
const randomCover = randomCoverFn()
|
const { cover: { default_cover: defaultCover } } = hexo.theme.config
|
||||||
|
const randomCover = getRandomCover(defaultCover)
|
||||||
data.cover = randomCover
|
data.cover = randomCover
|
||||||
coverVal = randomCover // update coverVal
|
coverVal = randomCover // update coverVal
|
||||||
}
|
}
|
||||||
@@ -50,11 +64,9 @@ hexo.extend.generator.register('post', locals => {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
return locals.posts.sort('date').map(post => {
|
return locals.posts.sort('date').map(post => ({
|
||||||
return {
|
|
||||||
data: handleImg(post),
|
data: handleImg(post),
|
||||||
layout: 'post',
|
layout: 'post',
|
||||||
path: post.path
|
path: post.path
|
||||||
}
|
}))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user