diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js new file mode 100644 index 00000000..69215527 --- /dev/null +++ b/lib/notion/getAllPosts.js @@ -0,0 +1,54 @@ +import BLOG from '@/blog.config' +import getAllPageIds from './getAllPageIds' +import getPageProperties from './getPageProperties' +import { getNotionPageData } from '@/lib/notion/getNotionData' +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, schema, null, tagOptions, notionPageData.siteInfo)) || 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) => { + const dateA = new Date(a?.date?.start_date || a.createdTime) + const dateB = new Date(b?.date?.start_date || b.createdTime) + return dateB - dateA + }) + } + return posts +} diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index f856c0ef..bf54561c 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -26,8 +26,6 @@ export async function getGlobalNotionData({ }) { // 获取Notion数据 const notionPageData = deepClone(await getNotionPageData({ pageId, from })) - notionPageData.siteInfo = getBlogInfo({ collection: notionPageData?.collection, block: notionPageData?.block }) - delete notionPageData.block delete notionPageData.collection delete notionPageData.collectionQuery @@ -238,6 +236,7 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { const viewIds = rawMetadata?.view_ids const collectionData = [] const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) + const siteInfo = getBlogInfo({ collection, block }) if (pageIds?.length === 0) { console.error('获取到的文章列表为空,请检查notion模板', collectionQuery, collection, collectionView, viewIds, pageRecordMap) } @@ -247,8 +246,10 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { if (!value) { continue } - const properties = (await getPageProperties(id, block, schema, tagOptions)) || null - collectionData.push(properties) + const properties = (await getPageProperties(id, block, schema, null, tagOptions, siteInfo)) || null + if (properties) { + collectionData.push(properties) + } } const allPages = collectionData.filter(post => { @@ -274,6 +275,7 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { const latestPosts = getLatestPosts({ allPosts, from, latestPostCount: 5 }) return { + siteInfo, allPages, allPosts, collection, diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index ed43c44e..f95969da 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -4,7 +4,7 @@ import BLOG from '@/blog.config' import formatDate from '../formatDate' import { defaultMapImageUrl } from 'react-notion-x' -async function getPageProperties(id, block, schema, authToken, tagOptions) { +async function getPageProperties(id, block, schema, authToken, tagOptions, siteInfo) { const rawProperties = Object.entries(block?.[id]?.value?.properties || []) const excludeProperties = ['date', 'select', 'multi_select', 'person'] const value = block[id]?.value @@ -63,7 +63,7 @@ async function getPageProperties(id, block, schema, authToken, tagOptions) { properties.createdTime = formatDate(new Date(value.created_time).toString(), BLOG.LANG) properties.lastEditedTime = formatDate(new Date(value?.last_edited_time).toString(), BLOG.LANG) properties.fullWidth = value.format?.page_full_width ?? false - properties.page_cover = getPostCover(id, block) ?? null + properties.page_cover = getPostCover(id, block) ?? siteInfo?.pageCover properties.content = value.content ?? [] properties.tagItems = properties?.tags?.map(tag => { return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' }