slug 兼容性优化,支持用任意前缀访问文章不限于article

This commit is contained in:
tangly1024.com
2024-05-08 15:13:12 +08:00
parent f803b7ad06
commit de0908ea94
7 changed files with 209 additions and 151 deletions

90
lib/utils/post.js Normal file
View 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)
)
}