mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-08 15:10:37 +00:00
编译: 修复推荐文章编译失败BUG
This commit is contained in:
@@ -68,22 +68,25 @@ export async function getStaticProps ({ params: { slug } }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
|
||||||
* @param post
|
* @param post
|
||||||
* @param {*} allPosts
|
* @param {*} allPosts
|
||||||
* @param {*} count
|
* @param {*} count
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
function getRecommendPost (post, allPosts, count = 5) {
|
function getRecommendPost (post, allPosts, count = 5) {
|
||||||
let filteredPosts = Object.create(allPosts)
|
let filteredPosts = []
|
||||||
// 筛选同标签
|
for (const i in allPosts) {
|
||||||
|
const p = allPosts[i]
|
||||||
|
filteredPosts.push(Object.assign(p))
|
||||||
|
}
|
||||||
|
|
||||||
if (post.tags && post.tags.length) {
|
if (post.tags && post.tags.length) {
|
||||||
const currentTag = post.tags[0]
|
const currentTag = post.tags[0]
|
||||||
filteredPosts = filteredPosts.filter(
|
filteredPosts = filteredPosts.filter(
|
||||||
p => p && p.tags && p.tags.includes(currentTag) && p.slug !== post.slug && p.type === 'post'
|
p => p && p.slug !== post.slug && p.tags && p.tags?.includes(currentTag) && p.type === ['Post']
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
shuffleSort(filteredPosts)
|
|
||||||
|
|
||||||
// 筛选前5个
|
// 筛选前5个
|
||||||
if (filteredPosts.length > count) {
|
if (filteredPosts.length > count) {
|
||||||
@@ -92,21 +95,4 @@ function getRecommendPost (post, allPosts, count = 5) {
|
|||||||
return filteredPosts
|
return filteredPosts
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 洗牌乱序:从数组的最后位置开始,从前面随机一个位置,对两个数进行交换,直到循环完毕
|
|
||||||
* @param arr
|
|
||||||
* @returns {*}
|
|
||||||
*/
|
|
||||||
function shuffleSort (arr) {
|
|
||||||
let i = arr.length - 1
|
|
||||||
while (i > 0) {
|
|
||||||
const rIndex = Math.floor(Math.random() * i)
|
|
||||||
const temp = arr[rIndex]
|
|
||||||
arr[rIndex] = arr[i]
|
|
||||||
arr[i] = temp
|
|
||||||
i--
|
|
||||||
}
|
|
||||||
return arr
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Slug
|
export default Slug
|
||||||
|
|||||||
Reference in New Issue
Block a user