From 0da9e605ec74984608174c27a45a5f39333ad5cb Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 10 May 2024 18:23:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9APOST=5FURL=5FPREFIX?= =?UTF-8?q?=20=E5=92=8C=20PSEUDO=5FSTATIC=E6=94=AF=E6=8C=81=E5=9C=A8NOTION?= =?UTF-8?q?=5FCONFIG=E4=B8=AD=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db/getSiteData.js | 18 ++++-- lib/notion/getAllPosts.js | 52 ---------------- lib/notion/getPageProperties.js | 102 ++++++++++++++++++-------------- 3 files changed, 70 insertions(+), 102 deletions(-) delete mode 100644 lib/notion/getAllPosts.js diff --git a/lib/db/getSiteData.js b/lib/db/getSiteData.js index 37bd6d45..66e1d90f 100755 --- a/lib/db/getSiteData.js +++ b/lib/db/getSiteData.js @@ -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 }) diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js deleted file mode 100644 index dd221234..00000000 --- a/lib/notion/getAllPosts.js +++ /dev/null @@ -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 -} diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index 5326c7ea..83982e4e 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -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)