slug 封装重复代码

This commit is contained in:
tangly1024.com
2023-07-24 15:35:04 +08:00
parent d80da58f1f
commit df42d7136d
2 changed files with 2 additions and 38 deletions

View File

@@ -3,7 +3,7 @@ import { getPostBlocks } from '@/lib/notion'
import { getGlobalData } from '@/lib/notion/getNotionData'
import { idToUuid } from 'notion-utils'
import { getNotion } from '@/lib/notion/getNotion'
import Slug from '.'
import Slug, { getRecommendPost } from '.'
/**
* 根据notion的slug访问页面
@@ -84,39 +84,4 @@ export async function getStaticProps({ params: { prefix, slug } }) {
}
}
/**
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
* @param post
* @param {*} allPosts
* @param {*} count
* @returns
*/
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]
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 (recommendPosts.length > count) {
recommendPosts = recommendPosts.slice(0, count)
}
return recommendPosts
}
export default PrefixSlug

View File

@@ -96,7 +96,6 @@ export async function getStaticPaths() {
}
export async function getStaticProps({ params: { prefix } }) {
// let fullSlug = slug.join('/')
let fullSlug = prefix
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
if (!fullSlug.endsWith('.html')) {
@@ -161,7 +160,7 @@ export async function getStaticProps({ params: { prefix } }) {
* @param {*} count
* @returns
*/
function getRecommendPost(post, allPosts, count = 6) {
export function getRecommendPost(post, allPosts, count = 6) {
let recommendPosts = []
const postIds = []
const currentTags = post?.tags || []