mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
slug 兼容性优化,支持用任意前缀访问文章不限于article
This commit is contained in:
@@ -156,19 +156,11 @@ function getCustomNav({ allPages }) {
|
||||
if (allPages && allPages.length > 0) {
|
||||
allPages.forEach(p => {
|
||||
p.to = p.slug
|
||||
if (p?.slug?.indexOf('http') === 0) {
|
||||
p.target = '_blank'
|
||||
} else {
|
||||
p.target = '_self'
|
||||
if (p?.slug?.indexOf('/') !== 0) {
|
||||
p.to = '/' + p.slug
|
||||
}
|
||||
}
|
||||
customNav.push({
|
||||
icon: p.icon || null,
|
||||
name: p.title,
|
||||
to: p.slug,
|
||||
target: '_blank',
|
||||
href: p.href,
|
||||
target: p.target,
|
||||
show: true
|
||||
})
|
||||
})
|
||||
@@ -192,15 +184,6 @@ function getCustomMenu({ collectionData, NOTION_CONFIG }) {
|
||||
if (menuPages && menuPages.length > 0) {
|
||||
menuPages.forEach(e => {
|
||||
e.show = true
|
||||
if (e?.slug?.indexOf('http') === 0) {
|
||||
e.target = '_blank'
|
||||
e.to = e.slug
|
||||
} else {
|
||||
e.target = '_self'
|
||||
if (e?.slug?.indexOf('http') !== 0 && e?.slug?.indexOf('/') !== 0) {
|
||||
e.to = '/' + e.slug
|
||||
}
|
||||
}
|
||||
if (e.type === BLOG.NOTION_PROPERTY_NAME.type_menu) {
|
||||
menus.push(e)
|
||||
} else if (e.type === BLOG.NOTION_PROPERTY_NAME.type_sub_menu) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { getDateValue, getTextContent } from 'notion-utils'
|
||||
import formatDate from '../utils/formatDate'
|
||||
// import { createHash } from 'crypto'
|
||||
import md5 from 'js-md5'
|
||||
import { checkContainHttp, sliceUrlFromHttp } from '../utils'
|
||||
import { mapImgUrl } from './mapImage'
|
||||
|
||||
/**
|
||||
@@ -106,7 +107,7 @@ export default async function getPageProperties(
|
||||
properties.pageCover = mapImgUrl(value?.format?.page_cover, value) ?? ''
|
||||
properties.pageCoverThumbnail =
|
||||
mapImgUrl(value?.format?.page_cover, value, 'block') ?? ''
|
||||
properties.ext = converToJSON(properties?.ext)
|
||||
properties.ext = convertToJSON(properties?.ext)
|
||||
properties.content = value.content ?? []
|
||||
properties.tagItems =
|
||||
properties?.tags?.map(tag => {
|
||||
@@ -117,28 +118,41 @@ export default async function getPageProperties(
|
||||
}) || []
|
||||
delete properties.content
|
||||
|
||||
// 处理URL
|
||||
// 处理URL ,为文章添加一个href字段,专门用来跳转访问
|
||||
if (properties.type === 'Post') {
|
||||
properties.slug = BLOG.POST_URL_PREFIX
|
||||
properties.href = BLOG.POST_URL_PREFIX
|
||||
? generateCustomizeUrl(properties)
|
||||
: properties.slug ?? properties.id
|
||||
} else if (properties.type === 'Page') {
|
||||
properties.slug = properties.slug ?? properties.id
|
||||
properties.href = properties.slug ?? properties.id
|
||||
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
|
||||
// 菜单路径为空、作为可展开菜单使用
|
||||
properties.to = properties.slug ?? '#'
|
||||
properties.href = properties.slug ?? '#'
|
||||
properties.name = properties.title ?? ''
|
||||
}
|
||||
|
||||
// 开启伪静态路径
|
||||
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
|
||||
if (
|
||||
!properties?.slug?.endsWith('.html') &&
|
||||
!properties?.slug?.startsWith('http')
|
||||
!properties?.href?.endsWith('.html') &&
|
||||
!properties?.href?.startsWith('http')
|
||||
) {
|
||||
properties.slug += '.html'
|
||||
properties.href += '.html'
|
||||
}
|
||||
}
|
||||
|
||||
// 最终检查超链接
|
||||
properties.href = checkContainHttp(properties?.href)
|
||||
? sliceUrlFromHttp(properties?.href)
|
||||
: `/${properties.href}`
|
||||
|
||||
// 设置链接在页内或新页面打开
|
||||
if (properties.href?.indexOf('http') === 0) {
|
||||
properties.target = '_blank'
|
||||
} else {
|
||||
properties.target = '_self'
|
||||
}
|
||||
|
||||
// 密码字段md5
|
||||
properties.password = properties.password
|
||||
? md5(properties.slug + properties.password)
|
||||
@@ -151,7 +165,7 @@ export default async function getPageProperties(
|
||||
* @param {*} str
|
||||
* @returns
|
||||
*/
|
||||
function converToJSON(str) {
|
||||
function convertToJSON(str) {
|
||||
if (!str) {
|
||||
return {}
|
||||
}
|
||||
|
||||
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