Merge pull request #256 from tangly1024/fix-article-cover

空白封面
This commit is contained in:
tangly1024
2022-05-19 16:30:03 +08:00
committed by GitHub
3 changed files with 62 additions and 6 deletions

54
lib/notion/getAllPosts.js Normal file
View File

@@ -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
}

View File

@@ -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,

View File

@@ -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' }