From 447c353a5d627b07a9d62b793a5a2ca10dac74e0 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Mon, 21 Mar 2022 17:32:01 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8E=A8=E8=8D=90?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E6=95=B0=E9=87=8F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog.config.js | 1 + pages/article/[slug].js | 39 +++++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/blog.config.js b/blog.config.js index 0b62db37..312d4603 100644 --- a/blog.config.js +++ b/blog.config.js @@ -26,6 +26,7 @@ const BLOG = { POST_LIST_STYLE: 'page', // ['page','scroll] 文章列表样式:页码分页、单页滚动加载 POST_LIST_PREVIEW: process.env.NEXT_PUBLIC_POST_PREVIEW || 'false', // 是否在列表加载文章预览 POST_PREVIEW_LINES: 12, // 预览博客行数 + POST_RECOMMEND_COUNT: 6, // 推荐文章数量 POSTS_PER_PAGE: 6, // post counts per page POSTS_SORT_BY: 'notion', // 排序方式 'date'按时间,'notion'由notion控制 diff --git a/pages/article/[slug].js b/pages/article/[slug].js index 9eb1bd65..e3f71f51 100644 --- a/pages/article/[slug].js +++ b/pages/article/[slug].js @@ -51,7 +51,7 @@ export async function getStaticProps ({ params: { slug } }) { const prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0] const next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0] - const recommendPosts = getRecommendPost(post, allPosts) + const recommendPosts = getRecommendPost(post, allPosts, BLOG.POST_RECOMMEND_COUNT) return { props: { @@ -76,25 +76,32 @@ export async function getStaticProps ({ params: { slug } }) { * @param {*} count * @returns */ -function getRecommendPost (post, allPosts, count = 5) { - let filteredPosts = [] - for (const i in allPosts) { +function getRecommendPost (post, allPosts, count = 6) { + let recommendPosts = [] + const postIds = [] + const currentTags = post.tags + for (let i = 0; i < allPosts.length; i++) { const p = allPosts[i] - filteredPosts.push(Object.assign(p)) + if (p.id === post.id || p.type.indexOf('Post') < 0) { + continue + } + + for (let j = 0; j < currentTags.length; j++) { + const t = currentTags[j] + if (postIds.indexOf(p.id) > -1) { + continue + } + if (p.tags && p.tags.indexOf(t) > -1) { + recommendPosts.push(p) + postIds.push(p.id) + } + } } - if (post.tags && post.tags.length) { - const currentTag = post.tags[0] - filteredPosts = filteredPosts.filter( - p => p && p.slug !== post.slug && p.tags && p.tags?.includes(currentTag) && p.type === ['Post'] - ) + if (recommendPosts.length > count) { + recommendPosts = recommendPosts.slice(0, count) } - - // 筛选前5个 - if (filteredPosts.length > count) { - filteredPosts = filteredPosts.slice(0, count) - } - return filteredPosts + return recommendPosts } export default Slug From 437ad8ad59ec8a1d0397f3c3f9b9097d13339734 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Mon, 21 Mar 2022 17:32:13 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getNotionData.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 94990a8f..8fd9db59 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -54,8 +54,10 @@ async function getLatestPosts ({ notionPageData, from, latestPostCount }) { let latestPosts = Object.create(allPosts) // 时间排序 latestPosts.sort((a, b) => { - const dateA = new Date(a?.lastEditedTime || a.createdTime) - const dateB = new Date(b?.lastEditedTime || b.createdTime) + // const dateA = new Date(a?.lastEditedTime || a.createdTime) + // const dateB = new Date(b?.lastEditedTime || b.createdTime) + const dateA = new Date(a.date?.start_date) + const dateB = new Date(b.date?.start_date) return dateB - dateA }) From 61c50eaf4cf145c0646bf04a5278706d3e8ccc81 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Mon, 21 Mar 2022 17:32:35 +0800 Subject: [PATCH 3/4] =?UTF-8?q?hexo=E4=B8=BB=E9=A2=98=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AE=9E=E7=94=A8=E7=BB=84=E4=BB=B6=E3=80=81=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/hexo/LayoutBase.js | 6 +-- themes/hexo/components/ArticleAdjacent.js | 10 ++--- themes/hexo/components/ArticleCopyright.js | 2 +- themes/hexo/components/ArticleDetail.js | 2 + themes/hexo/components/ArticleRecommend.js | 52 ++++++++++++++++++++++ themes/hexo/components/LatestPostsGroup.js | 17 +++++-- themes/hexo/components/SideRight.js | 8 ++-- themes/hexo/config_hexo.js | 3 ++ 8 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 themes/hexo/components/ArticleRecommend.js diff --git a/themes/hexo/LayoutBase.js b/themes/hexo/LayoutBase.js index 3f0872ea..0c4b7600 100644 --- a/themes/hexo/LayoutBase.js +++ b/themes/hexo/LayoutBase.js @@ -51,10 +51,10 @@ const LayoutBase = (props) => { {headerSlot} -
+
-
-
+
+
{onLoading ? : children}
diff --git a/themes/hexo/components/ArticleAdjacent.js b/themes/hexo/components/ArticleAdjacent.js index b7e36420..3ce2d17d 100644 --- a/themes/hexo/components/ArticleAdjacent.js +++ b/themes/hexo/components/ArticleAdjacent.js @@ -10,15 +10,15 @@ export default function ArticleAdjacent ({ prev, next }) { if (!prev || !next || !CONFIG_HEXO.ARTICLE_ADJACENT) { return <> } - return
+ return
- - {prev.title} + + {prev.title} - {next.title} - + {next.title} +
diff --git a/themes/hexo/components/ArticleCopyright.js b/themes/hexo/components/ArticleCopyright.js index 42a27552..aadf73b9 100644 --- a/themes/hexo/components/ArticleCopyright.js +++ b/themes/hexo/components/ArticleCopyright.js @@ -16,7 +16,7 @@ export default function ArticleCopyright () { }) const { locale } = useGlobal() - return
+ return
  • {locale.COMMON.AUTHOR}: diff --git a/themes/hexo/components/ArticleDetail.js b/themes/hexo/components/ArticleDetail.js index f0fa966b..38c7e073 100644 --- a/themes/hexo/components/ArticleDetail.js +++ b/themes/hexo/components/ArticleDetail.js @@ -10,6 +10,7 @@ import { useEffect, useRef } from 'react' import { Code, Collection, CollectionRow, Equation, NotionRenderer } from 'react-notion-x' import ArticleAdjacent from './ArticleAdjacent' import ArticleCopyright from './ArticleCopyright' +import ArticleRecommend from './ArticleRecommend' /** * @@ -67,6 +68,7 @@ export default function ArticleDetail (props) {
+ diff --git a/themes/hexo/components/ArticleRecommend.js b/themes/hexo/components/ArticleRecommend.js new file mode 100644 index 00000000..a0503694 --- /dev/null +++ b/themes/hexo/components/ArticleRecommend.js @@ -0,0 +1,52 @@ +import Link from 'next/link' +import CONFIG_HEXO from '../config_hexo' +import BLOG from '@/blog.config' +import { useGlobal } from '@/lib/global' + +/** + * 关联推荐文章 + * @param {prev,next} param0 + * @returns + */ +export default function ArticleRecommend ({ recommendPosts }) { + if (!recommendPosts || !CONFIG_HEXO.ARTICLE_RECOMMEND) { + return <> + } + const { locale } = useGlobal() + return ( +
+
+
+ + {locale.COMMON.RELATE_POSTS} +
+
+
+ {recommendPosts.map(post => { + const headerImage = post?.page_cover + ? `url("${post.page_cover}")` + : `url("/${CONFIG_HEXO.HOME_BANNER_IMAGE}")` + + return ( + + + +
+
+
+
{post.date?.start_date}
+
{post.title}
+
+
+
+
+ + ) + })} +
+
+ ) +} diff --git a/themes/hexo/components/LatestPostsGroup.js b/themes/hexo/components/LatestPostsGroup.js index 5829570c..5b0425ef 100644 --- a/themes/hexo/components/LatestPostsGroup.js +++ b/themes/hexo/components/LatestPostsGroup.js @@ -2,6 +2,7 @@ import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' import Link from 'next/link' import { useRouter } from 'next/router' +import CONFIG_HEXO from '../config_hexo' /** * 最新文章列表 @@ -19,16 +20,24 @@ const LatestPostsGroup = ({ posts }) => { return <>
-
{locale.COMMON.LATEST_POSTS}
+
{locale.COMMON.LATEST_POSTS}
{posts.map(post => { const selected = currentPath === `${BLOG.PATH}/article/${post.slug}` + const headerImage = post?.page_cover + ? `url("${post.page_cover}")` + : `url("/${CONFIG_HEXO.HOME_BANNER_IMAGE}")` + return ( -
-
{post.title}
+
+
+
+
{post.title}
+
{post.date?.start_date}
+
diff --git a/themes/hexo/components/SideRight.js b/themes/hexo/components/SideRight.js index ddddb89d..5114c1aa 100644 --- a/themes/hexo/components/SideRight.js +++ b/themes/hexo/components/SideRight.js @@ -5,7 +5,7 @@ import TagGroups from './TagGroups' import Catalog from './Catalog' import { InfoCard } from './InfoCard' import { AnalyticsCard } from './AnalyticsCard' - +import CONFIG_HEXO from '../config_hexo' export default function SideRight (props) { const { post, currentCategory, categories, latestPosts, tags, @@ -13,9 +13,9 @@ export default function SideRight (props) { } = props return ( -
+
- + { CONFIG_HEXO.WIDGET_ANALYTICS && } {showCategory && ( @@ -33,7 +33,7 @@ export default function SideRight (props) { )} - {latestPosts && + {CONFIG_HEXO.WIDGET_LATEST_POSTS && latestPosts && } diff --git a/themes/hexo/config_hexo.js b/themes/hexo/config_hexo.js index 8b5b6723..819f181b 100644 --- a/themes/hexo/config_hexo.js +++ b/themes/hexo/config_hexo.js @@ -16,7 +16,10 @@ const CONFIG_HEXO = { ARTICLE_ADJACENT: true, // 显示上一篇下一篇文章推荐 ARTICLE_COPYRIGHT: true, // 显示文章版权声明 + ARTICLE_RECOMMEND: true, // 文章关联推荐 + WIDGET_LATEST_POSTS: true, // 显示最新文章卡 + WIDGET_ANALYTICS: false, // 显示统计卡 WIDGET_TO_TOP: true, WIDGET_TO_COMMENT: true, // 跳到评论区 WIDGET_DARK_MODE: true, // 夜间模式 From 23869b8089886e3c1b480a08711579f8c3994359 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Mon, 21 Mar 2022 17:37:30 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=8E=A8=E8=8D=90=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/hexo/components/ArticleRecommend.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/hexo/components/ArticleRecommend.js b/themes/hexo/components/ArticleRecommend.js index a0503694..2bde709c 100644 --- a/themes/hexo/components/ArticleRecommend.js +++ b/themes/hexo/components/ArticleRecommend.js @@ -9,7 +9,7 @@ import { useGlobal } from '@/lib/global' * @returns */ export default function ArticleRecommend ({ recommendPosts }) { - if (!recommendPosts || !CONFIG_HEXO.ARTICLE_RECOMMEND) { + if (!CONFIG_HEXO.ARTICLE_RECOMMEND || !recommendPosts || recommendPosts.length === 0) { return <> } const { locale } = useGlobal()