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 }) )} -
-
- - 💖 本 文 结 束 😚 感 谢 您 的 阅 读 💖 - + + +
+
———
+
💖 本 文 结 束 😚 感 谢 您 的 阅 读 💖
+
———
打赏一杯咖啡~