From ab111100bd8a881107ac72ad671d35340e368e12 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Mon, 31 Jul 2023 15:38:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E8=8D=90=E6=96=87=E7=AB=A0=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=97=B6=E9=97=B4=E5=80=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getNotionData.js | 2 +- themes/heo/components/Hero.js | 55 +++++++++++++++++++++++++---------- themes/heo/config.js | 1 + 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 4e42d349..20f47be3 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -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 })) } /** diff --git a/themes/heo/components/Hero.js b/themes/heo/components/Hero.js index 4d8da6ca..b9269561 100644 --- a/themes/heo/components/Hero.js +++ b/themes/heo/components/Hero.js @@ -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 (
{/* 置顶推荐文章 */} @@ -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 diff --git a/themes/heo/config.js b/themes/heo/config.js index 2789fde3..f62be177 100644 --- a/themes/heo/config.js +++ b/themes/heo/config.js @@ -24,6 +24,7 @@ const CONFIG = { // 英雄区右侧推荐文章标签, 例如 [推荐] , 最多六篇文章; 若留空白'',则推荐最近更新文章 HERO_RECOMMEND_POST_TAG: '推荐', + HERO_RECOMMEND_POST_SORT_BY_UPDATE_TIME: false, // 推荐文章排序,为`true`时将强制按最后修改时间倒序 // 右侧个人资料卡牌欢迎语,点击可自动切换 INFOCARD_GREETINGS: [