mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
slug 兼容性优化,支持用任意前缀访问文章不限于article
This commit is contained in:
90
lib/utils/post.js
Normal file
90
lib/utils/post.js
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* 文章相关工具
|
||||
*/
|
||||
import { checkContainHttp } 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 &&
|
||||
!checkContainHttp(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 &&
|
||||
!checkContainHttp(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 &&
|
||||
!checkContainHttp(slug)
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user