Files
NotionNext/lib/utils/post.js
2024-06-15 10:34:55 +08:00

91 lines
1.9 KiB
JavaScript

/**
* 文章相关工具
*/
import { checkStartWithHttp } from '.'
/**
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
* @param post
* @param {*} allPosts
* @param {*} count
* @returns
*/
export 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
}
/**
* 确认slug中不包含 / 符号
* @param {*} row
* @returns
*/
export function checkSlugHasNoSlash(row) {
let slug = row.slug
if (slug.startsWith('/')) {
slug = slug.substring(1)
}
return (
(slug.match(/\//g) || []).length === 0 &&
!checkStartWithHttp(slug) &&
row.type.indexOf('Menu') < 0
)
}
/**
* 检查url中包含一个 /
* @param {*} row
* @returns
*/
export function checkSlugHasOneSlash(row) {
let slug = row.slug
if (slug.startsWith('/')) {
slug = slug.substring(1)
}
return (
(slug.match(/\//g) || []).length === 1 &&
!checkStartWithHttp(slug) &&
row.type.indexOf('Menu') < 0
)
}
/**
* 检查url中包含两个及以上的 /
* @param {*} row
* @returns
*/
export function checkSlugHasMorThanTwoSlash(row) {
let slug = row.slug
if (slug.startsWith('/')) {
slug = slug.substring(1)
}
return (
(slug.match(/\//g) || []).length >= 2 &&
row.type.indexOf('Menu') < 0 &&
!checkStartWithHttp(slug)
)
}