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

View File

@@ -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 {}
}