From ce046bff22c14adda3a9e1f17b9cdca1b0fd8ffc Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Mon, 9 May 2022 16:05:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85PageProperties=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=96=87=E7=AB=A0=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getAllPosts.js | 23 +---------------------- lib/notion/getNotionData.js | 11 +++++++---- lib/notion/getPageProperties.js | 26 +++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js index 51d46193..c853ffdd 100644 --- a/lib/notion/getAllPosts.js +++ b/lib/notion/getAllPosts.js @@ -1,8 +1,6 @@ import BLOG from '@/blog.config' import getAllPageIds from './getAllPageIds' import getPageProperties from './getPageProperties' -import { defaultMapImageUrl } from 'react-notion-x' -import formatDate from '@/lib/formatDate' import { getNotionPageData } from '@/lib/notion/getNotionData' import { delCacheData } from '@/lib/cache/cache_manager' @@ -26,21 +24,11 @@ export async function getAllPosts({ notionPageData, from, pageType }) { const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) for (let i = 0; i < pageIds.length; i++) { const id = pageIds[i] - const properties = (await getPageProperties(id, block, schema)) || null const value = block[id]?.value if (!value) { continue } - properties.slug = properties.slug ?? properties.id - 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.content = value.content ?? [] - properties.tagItems = properties?.tags?.map(tag => { - return { name: tag, color: tagOptions.find(t => t.value === tag)?.color || 'gray' } - }) || [] - delete properties.content + const properties = (await getPageProperties(id, block, schema, tagOptions)) || null data.push(properties) } @@ -64,12 +52,3 @@ export async function getAllPosts({ notionPageData, from, pageType }) { } return posts } - -// 从Block获取封面图;优先取PageCover,否则取内容图片 -function getPostCover(id, block) { - const pageCover = block[id].value?.format?.page_cover - if (pageCover) { - if (pageCover.startsWith('/')) return 'https://www.notion.so' + pageCover - if (pageCover.startsWith('http')) return defaultMapImageUrl(pageCover, block[id].value) - } -} diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index d0021994..cc7e8c8c 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -49,13 +49,14 @@ export async function getGlobalNotionData({ */ function getLatestPosts({ allPosts, from, latestPostCount }) { const latestPosts = Object.create(allPosts).sort((a, b) => { + console.log('排序过程', a, b) const dateA = new Date(a?.lastEditedTime || a?.createdTime || a?.date?.start_date) const dateB = new Date(b?.lastEditedTime || b?.createdTime || b?.date?.start_date) // const dateA = new Date(a.date?.start_date) // const dateB = new Date(b.date?.start_date) return dateB - dateA }) - + console.log('排序结果', latestPosts) return latestPosts.slice(0, latestPostCount) } @@ -225,9 +226,11 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { } for (let i = 0; i < pageIds.length; i++) { const id = pageIds[i] - const properties = (await getPageProperties(id, block, schema)) || null - properties.slug = properties.slug ?? properties.id - delete properties.content + const value = block[id]?.value + if (!value) { + continue + } + const properties = (await getPageProperties(id, block, schema, tagOptions)) || null data.push(properties) } diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index 3dcc7735..ed43c44e 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -1,9 +1,13 @@ import { getTextContent, getDateValue } from 'notion-utils' import { NotionAPI } from 'notion-client' +import BLOG from '@/blog.config' +import formatDate from '../formatDate' +import { defaultMapImageUrl } from 'react-notion-x' -async function getPageProperties(id, block, schema, authToken) { +async function getPageProperties(id, block, schema, authToken, tagOptions) { const rawProperties = Object.entries(block?.[id]?.value?.properties || []) const excludeProperties = ['date', 'select', 'multi_select', 'person'] + const value = block[id]?.value const properties = {} for (let i = 0; i < rawProperties.length; i++) { const [key, val] = rawProperties[i] @@ -54,7 +58,27 @@ async function getPageProperties(id, block, schema, authToken) { } } } + + properties.slug = properties.slug ?? properties.id + 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.content = value.content ?? [] + properties.tagItems = properties?.tags?.map(tag => { + return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' } + }) || [] + delete properties.content return properties + + // 从Block获取封面图;优先取PageCover,否则取内容图片 + function getPostCover(id, block) { + const pageCover = block[id].value?.format?.page_cover + if (pageCover) { + if (pageCover.startsWith('/')) return 'https://www.notion.so' + pageCover + if (pageCover.startsWith('http')) return defaultMapImageUrl(pageCover, block[id].value) + } + } } export { getPageProperties as default }