mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-17 23:16:50 +00:00
站点信息读取Notion数据
This commit is contained in:
@@ -20,7 +20,12 @@ import { getAllTags } from './getAllTags'
|
||||
categories, 所有分类
|
||||
postCount,
|
||||
customNav, 自定义导航菜单
|
||||
tags 所有标签
|
||||
tags, 所有标签
|
||||
siteInfo:{
|
||||
title,
|
||||
description,
|
||||
pageCover
|
||||
}
|
||||
}
|
||||
*
|
||||
*/
|
||||
@@ -33,15 +38,16 @@ export async function getGlobalNotionData ({
|
||||
pageType = ['Post']
|
||||
}) {
|
||||
const notionPageData = await getNotionPageData({ pageId, from })
|
||||
const siteInfo = await getBlogInfo({ notionPageData, from })
|
||||
const tagOptions = notionPageData.tagOptions
|
||||
const categoryOptions = notionPageData.categoryOptions
|
||||
const customNav = await getCustomNav({ notionPageData })
|
||||
const allPosts = await getAllPosts({ notionPageData, from, pageType })
|
||||
const postCount = await getAllPostCount({ notionPageData, from })
|
||||
const customNav = await getCustomNav({ notionPageData })
|
||||
const categories = await getAllCategories({ allPosts, categoryOptions, sliceCount: categoryCount })
|
||||
const tags = await getAllTags({ allPosts, tagOptions, sliceCount: tagsCount })
|
||||
const latestPosts = await getLatestPosts({ notionPageData, from, latestPostCount })
|
||||
return { allPosts, latestPosts, categories, postCount, customNav, tags }
|
||||
return { allPosts, latestPosts, categories, postCount, customNav, tags, siteInfo }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,12 +89,16 @@ export async function getNotionPageData ({ pageId, from }) {
|
||||
const pageRecordMap = await getPageRecordMapByNotionAPI({ pageId, from })
|
||||
// 存入缓存
|
||||
if (pageRecordMap) {
|
||||
console.log('[站点数据]', pageRecordMap)
|
||||
await setDataToCache(cacheKey, pageRecordMap)
|
||||
}
|
||||
return pageRecordMap
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户自定义单页菜单
|
||||
* @param notionPageData
|
||||
* @returns {Promise<[]|*[]>}
|
||||
*/
|
||||
async function getCustomNav ({ notionPageData }) {
|
||||
if (!notionPageData) {
|
||||
notionPageData = await getNotionPageData({ from: 'custom-nav' })
|
||||
@@ -121,12 +131,53 @@ function getTagOptions (schema) {
|
||||
return tagSchema?.options || []
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分类选项
|
||||
* @param schema
|
||||
* @returns {{}|*|*[]}
|
||||
*/
|
||||
function getCategoryOptions (schema) {
|
||||
if (!schema) return {}
|
||||
const categorySchema = Object.values(schema).find(e => e.name === 'category')
|
||||
return categorySchema?.options || []
|
||||
}
|
||||
|
||||
/**
|
||||
* 站点信息
|
||||
* @param notionPageData
|
||||
* @param from
|
||||
* @returns {Promise<{title,description,pageCover}>}
|
||||
*/
|
||||
async function getBlogInfo ({ notionPageData, from }) {
|
||||
if (!notionPageData) {
|
||||
notionPageData = await getNotionPageData({ from })
|
||||
}
|
||||
if (!notionPageData) {
|
||||
return null
|
||||
}
|
||||
const collection = notionPageData?.collection
|
||||
const title = collection?.name[0][0] || BLOG.TITLE
|
||||
const description = collection?.description[0][0] || BLOG.DESCRIPTION
|
||||
const pageCover = mapCoverUrl(collection?.cover)
|
||||
return { title, description, pageCover }
|
||||
}
|
||||
|
||||
/**
|
||||
* 网站封面背景图
|
||||
* @param pageCover
|
||||
* @returns {string}
|
||||
*/
|
||||
const mapCoverUrl = pageCover => {
|
||||
if (!pageCover || pageCover === '') {
|
||||
return BLOG.HOME_BANNER_IMAGE
|
||||
}
|
||||
if (pageCover.indexOf('/images') === 0) {
|
||||
return 'https://www.notion.so' + pageCover
|
||||
} else {
|
||||
return pageCover
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用NotionAPI获取Page数据
|
||||
* @returns {Promise<JSX.Element|null|*>}
|
||||
|
||||
Reference in New Issue
Block a user