合并notionData 接口

This commit is contained in:
tangly1024
2022-01-07 14:27:21 +08:00
parent a8a8df0209
commit f98dc6a937
19 changed files with 330 additions and 276 deletions

View File

@@ -1,7 +1,52 @@
import BLOG from '@/blog.config'
import { idToUuid } from 'notion-utils'
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
import { getPostBlocks } from '@/lib/notion/getPostBlocks'
import { idToUuid } from 'notion-utils'
import { getAllCategories } from './getAllCategories'
import { getAllPosts } from './getAllPosts'
import { getAllTags } from './getAllTags'
/**
* 获取博客数据
* @param {*} pageId
* @param {*} from
* @returns
* allPosts 所有博客
* categories 所有分类
* tags 所有标签
*/
export async function getGlobalNotionData ({
pageId = BLOG.notionPageId,
from,
latestPostCount = 5,
tagsCount = 16,
includePage
}) {
const notionPageData = await getNotionPageData({ pageId, from })
const tagOptions = notionPageData.tagOptions
const allPosts = await getAllPosts({ notionPageData, from, includePage })
const postCount = allPosts?.length
const categories = await getAllCategories(allPosts)
const tags = await getAllTags({ allPosts, tagOptions, sliceCount: tagsCount })
// 深拷贝
let latestPosts = Object.create(allPosts)
// 时间排序
latestPosts.sort((a, b) => {
const dateA = new Date(a?.lastEditedTime || a.createdTime)
const dateB = new Date(b?.lastEditedTime || b.createdTime)
return dateB - dateA
})
// 只取前五
latestPosts = latestPosts.slice(0, latestPostCount)
return {
allPosts,
latestPosts,
categories,
postCount,
tags
}
}
/**
* 获取指定notion的collection数据
@@ -9,7 +54,7 @@ import { getPostBlocks } from '@/lib/notion/getPostBlocks'
* @param from 请求来源
* @returns {Promise<JSX.Element|*|*[]>}
*/
export async function getNotionPageData ({ pageId = BLOG.notionPageId, from }) {
export async function getNotionPageData ({ pageId, from }) {
// 尝试从缓存获取
const cacheKey = 'page_record_map_' + pageId
const data = await getDataFromCache(cacheKey)
@@ -54,7 +99,10 @@ async function getPageRecordMapByNotionAPI ({ pageId, from }) {
const tagOptions = getTagOptions(schema)
// Check Type Page-Database和Inline-Database
if (rawMetadata?.type !== 'collection_view_page' && rawMetadata?.type !== 'collection_view') {
if (
rawMetadata?.type !== 'collection_view_page' &&
rawMetadata?.type !== 'collection_view'
) {
console.warn(`pageId "${pageId}" is not a database`)
return null
}