From adac140892cc6e9e3311eb166b78566f65b8af55 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Mon, 9 May 2022 13:52:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=8E=B7=E5=8F=96=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getAllPageIds.js | 22 +++++++++++++--------- lib/notion/getAllPosts.js | 24 ++++++++++++------------ lib/notion/getNotionData.js | 12 ++++++++++-- lib/notion/getPostBlocks.js | 8 ++++++-- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/lib/notion/getAllPageIds.js b/lib/notion/getAllPageIds.js index 5eb15570..fb4ae35e 100644 --- a/lib/notion/getAllPageIds.js +++ b/lib/notion/getAllPageIds.js @@ -1,24 +1,28 @@ -import { idToUuid } from 'notion-utils' -export default function getAllPageIds (collectionQuery, viewId) { - if (!collectionQuery) { +export default function getAllPageIds (collectionQuery, collectionId, collectionView, viewIds) { + if (!collectionQuery && !collectionView) { return [] } - const views = Object.values(collectionQuery)[0] + let views = collectionQuery[collectionId] if (!views) { - return [] + views = collectionView } let pageIds = [] - if (viewId) { - const vId = idToUuid(viewId) - pageIds = views[vId]?.blockIds - } else { + if (collectionQuery && Object.values(collectionQuery).length > 0) { const pageSet = new Set() Object.values(views).forEach(view => { view?.blockIds?.forEach(id => pageSet.add(id)) // group视图 view?.collection_group_results?.blockIds?.forEach(id => pageSet.add(id)) // table视图 }) pageIds = [...pageSet] + console.log('从collectionQuery获取博客', collectionQuery) + } else if (viewIds && viewIds.length > 0) { + const ids = collectionView[viewIds[0]].value.page_sort + console.log('从viewId获取博客 ', viewIds) + for (const id of ids) { + pageIds.push(id) + } } + console.log('文章列表', pageIds) return pageIds } diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js index 47198789..51d46193 100644 --- a/lib/notion/getAllPosts.js +++ b/lib/notion/getAllPosts.js @@ -21,22 +21,22 @@ export async function getAllPosts({ notionPageData, from, pageType }) { return [] } - const pageBlock = notionPageData.block - const schema = notionPageData.schema - const tagOptions = notionPageData.tags - const collectionQuery = notionPageData.collectionQuery - + const { block, schema, tagOptions, collectionQuery, collectionId, collectionView, viewIds } = notionPageData const data = [] - const pageIds = getAllPageIds(collectionQuery) + const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) for (let i = 0; i < pageIds.length; i++) { const id = pageIds[i] - const properties = (await getPageProperties(id, pageBlock, schema)) || null + 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(pageBlock[id].value?.created_time).toString(), BLOG.LANG) - properties.lastEditedTime = formatDate(new Date(pageBlock[id].value?.last_edited_time).toString(), BLOG.LANG) - properties.fullWidth = pageBlock[id].value?.format?.page_full_width ?? false - properties.page_cover = getPostCover(id, pageBlock) ?? null - properties.content = pageBlock[id].value?.content ?? [] + 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' } }) || [] diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 0b2d5ba1..3d2941a2 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -211,13 +211,18 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { } const collection = Object.values(pageRecordMap.collection)[0]?.value + const collectionId = rawMetadata?.collection_id const collectionQuery = pageRecordMap.collection_query + const collectionView = pageRecordMap.collection_view const schema = collection?.schema const tagOptions = getTagOptions(schema) const categoryOptions = getCategoryOptions(schema) - + const viewIds = rawMetadata?.view_ids const data = [] - const pageIds = getAllPageIds(collectionQuery) + const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) + if (pageIds?.length === 0) { + console.error('获取到的文章列表为空,请检查notion模板', collectionQuery, collection, pageRecordMap) + } for (let i = 0; i < pageIds.length; i++) { const id = pageIds[i] const properties = (await getPageProperties(id, block, schema)) || null @@ -243,6 +248,9 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { return { collection, collectionQuery, + collectionId, + collectionView, + viewIds, block, schema, tagOptions, diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index 67ff7d5b..e59d617f 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -13,7 +13,11 @@ export async function getPostBlocks(id, from, slice) { console.warn('[请求API]:', `from:${from}`, `id:${id}`) pageBlock = await getPageWithRetry(id, from) - console.warn('[请求完成]: 结果', `${pageBlock ? '成功' : '失败'}`, `from:${from}`, `id:${id}`) + if (pageBlock) { + console.info('[请求成功]:', `from:${from}`, `id:${id}`) + } else { + console.error('[请求失败]:', `from:${from}`, `id:${id}`) + } if (pageBlock) { await setDataToCache(cacheKey, pageBlock) @@ -29,7 +33,7 @@ export async function getPostBlocks(id, from, slice) { */ async function getPageWithRetry(id, from, retryAttempts = 3) { if (retryAttempts && retryAttempts > 0) { - console.log('[发起请求]', `from:${from}`, `id:${id}`, `剩余重试次数:${retryAttempts}`) + console.log('[发起请求]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '') try { const authToken = BLOG.NOTION_ACCESS_TOKEN || null const api = new NotionAPI({ authToken })