优化:POST_URL_PREFIX 和 PSEUDO_STATIC支持在NOTION_CONFIG中配置

This commit is contained in:
tangly1024.com
2024-05-10 18:23:27 +08:00
parent 021ea08f47
commit 0da9e605ec
3 changed files with 70 additions and 102 deletions

View File

@@ -4,11 +4,14 @@ import { getAllCategories } from '@/lib/notion/getAllCategories'
import getAllPageIds from '@/lib/notion/getAllPageIds'
import { getAllTags } from '@/lib/notion/getAllTags'
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
import getPageProperties from '@/lib/notion/getPageProperties'
import getPageProperties, {
adjustPageProperties
} from '@/lib/notion/getPageProperties'
import { getPostBlocks, getSingleBlock } from '@/lib/notion/getPostBlocks'
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
import { deepClone } from '@/lib/utils'
import { idToUuid } from 'notion-utils'
import { siteConfig } from '../config'
import { extractLangId, extractLangPrefix } from '../utils/pageId'
export { getAllTags } from '../notion/getAllTags'
@@ -442,14 +445,19 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
}
}
// 文章计数
let postCount = 0
// 站点配置优先读取配置表格否则读取blog.config.js 文件
const NOTION_CONFIG = (await getConfigMapFromConfigPage(collectionData)) || {}
// 处理每一条数据的字段
collectionData.forEach(function (element) {
adjustPageProperties(element, NOTION_CONFIG)
})
const siteInfo = getSiteInfo({ collection, block, pageId })
// 文章计数
let postCount = 0
// 查找所有的Post和Page
const allPages = collectionData.filter(post => {
if (post?.type === 'Post' && post.status === 'Published') {
@@ -464,7 +472,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
})
// Sort by date
if (BLOG.POSTS_SORT_BY === 'date') {
if (siteConfig('POSTS_SORT_BY', '', NOTION_CONFIG) === 'date') {
allPages.sort((a, b) => {
return b?.publishDate - a?.publishDate
})

View File

@@ -1,52 +0,0 @@
import BLOG from '@/blog.config'
import getAllPageIds from './getAllPageIds'
import getPageProperties from './getPageProperties'
import { getNotionPageData } from '@/lib/db/getSiteData'
import { delCacheData } from '@/lib/cache/cache_manager'
/**
* 获取所有文章列表
* @param notionPageData
* @param from
* @param pageType 页面类型数组 ['Post','Page']
* @returns {Promise<*[]>}
*/
export async function getAllPosts({ notionPageData, from, pageType }) {
if (!notionPageData) {
notionPageData = await getNotionPageData({ from })
}
if (!notionPageData) {
return []
}
const { block, schema, tagOptions, collectionQuery, collectionId, collectionView, viewIds } = notionPageData
const data = []
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
for (let i = 0; i < pageIds.length; i++) {
const id = pageIds[i]
const value = block[id]?.value
if (!value) {
continue
}
const properties = (await getPageProperties(id, block[id].value, schema, null, tagOptions)) || null
data.push(properties)
}
// remove all the the items doesn't meet requirements
const posts = data.filter(post => {
return post.title && post?.status?.[0] === 'Published' && pageType.indexOf(post?.type?.[0]) > -1
})
if (!posts || posts.length === 0) {
const cacheKey = 'page_block_' + BLOG.NOTION_PAGE_ID
await delCacheData(cacheKey)
}
// Sort by date
if (BLOG.POSTS_SORT_BY === 'date') {
posts.sort((a, b) => {
return b?.publishDate - a?.publishDate
})
}
return posts
}

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 { siteConfig } from '../config'
import { checkContainHttp, sliceUrlFromHttp } from '../utils'
import { mapImgUrl } from './mapImage'
@@ -117,49 +118,6 @@ export default async function getPageProperties(
}
}) || []
delete properties.content
// 处理URL
// 1.按照用户配置的URL_PREFIX 转换一下slug
// 2.为文章添加一个href字段存储最终调整的路径
if (properties.type === 'Post') {
if (BLOG.POST_URL_PREFIX) {
properties.slug = generateCustomizeSlug(properties)
}
properties.href = properties.slug ?? properties.id
} else if (properties.type === 'Page') {
properties.href = properties.slug ?? properties.id
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
// 菜单路径为空、作为可展开菜单使用
properties.href = properties.slug ?? '#'
properties.name = properties.title ?? ''
}
// 开启伪静态路径
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
if (
!properties?.href?.endsWith('.html') &&
!properties?.href?.startsWith('http')
) {
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)
: ''
return properties
}
@@ -202,6 +160,55 @@ function mapProperties(properties) {
}
}
/**
* 过滤处理页面数据
* 过滤处理过程会用到NOTION_CONFIG中的配置
*/
export function adjustPageProperties(properties, NOTION_CONFIG) {
// 处理URL
// 1.按照用户配置的URL_PREFIX 转换一下slug
// 2.为文章添加一个href字段存储最终调整的路径
if (properties.type === 'Post') {
if (siteConfig('POST_URL_PREFIX', '', NOTION_CONFIG)) {
properties.slug = generateCustomizeSlug(properties, NOTION_CONFIG)
}
properties.href = properties.slug ?? properties.id
} else if (properties.type === 'Page') {
properties.href = properties.slug ?? properties.id
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
// 菜单路径为空、作为可展开菜单使用
properties.href = properties.slug ?? '#'
properties.name = properties.title ?? ''
}
// 开启伪静态路径
if (siteConfig('PSEUDO_STATIC', false, NOTION_CONFIG)) {
if (
!properties?.href?.endsWith('.html') &&
!properties?.href?.startsWith('http')
) {
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)
: ''
}
/**
* 获取自定义URL
* 可以根据变量生成URL
@@ -209,9 +216,14 @@ function mapProperties(properties) {
* @param {*} postProperties
* @returns
*/
function generateCustomizeSlug(postProperties) {
function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
let fullPrefix = ''
const allSlugPatterns = BLOG.POST_URL_PREFIX.split('/')
const allSlugPatterns = siteConfig(
'POST_URL_PREFIX',
'',
NOTION_CONFIG
).split('/')
allSlugPatterns.forEach((pattern, idx) => {
if (pattern === '%year%' && postProperties?.publishDay) {
const formatPostCreatedDate = new Date(postProperties?.publishDay)