From 299866d57027427bc59157da50ef1638df6ecbda Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Fri, 5 Nov 2021 13:58:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E6=8E=A8=E8=8D=90=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/BlogPostListScroll.js | 2 +- components/PostsCategories.js | 2 +- components/RecommendPosts.js | 47 ++++++++++++++++++++++++++++++++ pages/article/[slug].js | 10 +++++-- 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 components/RecommendPosts.js diff --git a/components/BlogPostListScroll.js b/components/BlogPostListScroll.js index 20c05bfb..e1092954 100644 --- a/components/BlogPostListScroll.js +++ b/components/BlogPostListScroll.js @@ -74,7 +74,7 @@ const BlogPostListScroll = ({ posts = [], tags, currentSearch, currentCategory, {/* 文章列表 */}
{postsToShow.map(post => ( - + ))}
diff --git a/components/PostsCategories.js b/components/PostsCategories.js index 76668adb..4e662cae 100644 --- a/components/PostsCategories.js +++ b/components/PostsCategories.js @@ -11,7 +11,7 @@ const PostsCategories = ({ currentCategory, categories }) => {
{Object.keys(categories).map(category => { return -
{category} {categories[category]}
+
{category}({categories[category]})
})}
diff --git a/components/RecommendPosts.js b/components/RecommendPosts.js new file mode 100644 index 00000000..4315c25f --- /dev/null +++ b/components/RecommendPosts.js @@ -0,0 +1,47 @@ +import React from 'react' +import Link from 'next/link' + +/** + * 洗牌乱序:从数组的最后位置开始,从前面随机一个位置,对两个数进行交换,直到循环完毕 + * @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 +} + +const RecommendPosts = ({ currentPost, totalPosts }) => { + let filteredPosts = totalPosts + const currentTag = currentPost.tags[0] + + // 筛选同标签 + if (currentPost.tags && currentPost.tags.length) { + filteredPosts = totalPosts.filter( + post => post && post.tags && post.tags.includes(currentTag) && post.slug !== currentPost.slug + ) + } + shuffleSort(filteredPosts) + + // 筛选前5个 + if (filteredPosts.length > 5) { + filteredPosts = filteredPosts.slice(0, 5) + } + + return
+

推荐文章

+
    + {filteredPosts.map(post => ( +
  • {post.title}
  • + ))} +
+
+} +export default RecommendPosts diff --git a/pages/article/[slug].js b/pages/article/[slug].js index 9c36f133..84269ca5 100644 --- a/pages/article/[slug].js +++ b/pages/article/[slug].js @@ -22,6 +22,7 @@ import 'prismjs/components/prism-markup' import 'prismjs/components/prism-python' import 'prismjs/components/prism-javascript' import 'prismjs/components/prism-typescript' +import RecommendPosts from '@/components/RecommendPosts' const mapPageUrl = id => { return 'https://www.notion.so/' + id.replace(/-/g, '') @@ -105,9 +106,12 @@ const ArticleDetail = ({ post, blockMap, tags, prev, next, posts, categories }) )} -
-
- - 💖 本 文 结 束 😚 感 谢 您 的 阅 读 💖 - + + +
+
———
+
💖 本 文 结 束 😚 感 谢 您 的 阅 读 💖
+
———
打赏一杯咖啡~