推荐文章,支持时间倒序

This commit is contained in:
tangly1024.com
2023-07-31 15:38:18 +08:00
parent 386edbde10
commit ab111100bd
3 changed files with 41 additions and 17 deletions

View File

@@ -184,7 +184,7 @@ export function getNavPages({ allPages }) {
return post && post?.slug && (!post?.slug?.startsWith('http')) && post?.type === 'Post' && post?.status === 'Published'
})
return allNavPages.map(item => ({ id: item.id, title: item.title || '', pageCoverThumbnail: item.pageCoverThumbnail || '', category: item.category || null, tags: item.tags || null, summary: item.summary || null, slug: item.slug }))
return allNavPages.map(item => ({ id: item.id, title: item.title || '', pageCoverThumbnail: item.pageCoverThumbnail || '', category: item.category || null, tags: item.tags || null, summary: item.summary || null, slug: item.slug, lastEditedDate: item.lastEditedDate }))
}
/**

View File

@@ -160,22 +160,9 @@ function TopGroup(props) {
todayCardRef.current.coverUp()
}
let topPosts = []
// 默认展示最近更新
if (!CONFIG.HERO_RECOMMEND_POST_TAG || CONFIG.HERO_RECOMMEND_POST_TAG === '') {
topPosts = latestPosts
} else {
// 展示特定标签文章
for (const post of allNavPages) {
if (topPosts.length === 6) {
break
}
// 查找标签
if (post?.tags?.indexOf(CONFIG.HERO_RECOMMEND_POST_TAG) >= 0) {
topPosts.push(post)
}
}
}
// 获取置顶推荐文章
const topPosts = getTopPosts({ latestPosts, allNavPages })
return (
<div id='hero-right-wrapper' onMouseLeave={handleMouseLeave} className='flex-1 relative w-full'>
{/* 置顶推荐文章 */}
@@ -198,6 +185,42 @@ function TopGroup(props) {
)
}
/**
* 获取推荐置顶文章
*/
function getTopPosts({ latestPosts, allNavPages }) {
// 默认展示最近更新
if (!CONFIG.HERO_RECOMMEND_POST_TAG || CONFIG.HERO_RECOMMEND_POST_TAG === '') {
return latestPosts
}
// 显示包含‘推荐’标签的文章
let sortPosts = []
// 排序方式
if (JSON.parse(CONFIG.HERO_RECOMMEND_POST_SORT_BY_UPDATE_TIME)) {
sortPosts = Object.create(allNavPages).sort((a, b) => {
const dateA = new Date(a?.lastEditedDate)
const dateB = new Date(b?.lastEditedDate)
return dateB - dateA
})
} else {
sortPosts = Object.create(allNavPages)
}
const topPosts = []
for (const post of sortPosts) {
if (topPosts.length === 6) {
break
}
// 查找标签
if (post?.tags?.indexOf(CONFIG.HERO_RECOMMEND_POST_TAG) >= 0) {
topPosts.push(post)
}
}
return topPosts
}
/**
* 英雄区右侧,今日卡牌
* @returns

View File

@@ -24,6 +24,7 @@ const CONFIG = {
// 英雄区右侧推荐文章标签, 例如 [推荐] , 最多六篇文章; 若留空白'',则推荐最近更新文章
HERO_RECOMMEND_POST_TAG: '推荐',
HERO_RECOMMEND_POST_SORT_BY_UPDATE_TIME: false, // 推荐文章排序,为`true`时将强制按最后修改时间倒序
// 右侧个人资料卡牌欢迎语,点击可自动切换
INFOCARD_GREETINGS: [