头像读取Notion

This commit is contained in:
tangly1024
2022-05-11 10:59:37 +08:00
parent ce046bff22
commit c5167d9fdd
10 changed files with 52 additions and 66 deletions

View File

@@ -25,12 +25,11 @@ export async function getGlobalNotionData({
from,
pageType = ['Post']
}) {
// 深拷贝数据
// 获取Notion数据
const notionPageData = deepClone(await getNotionPageData({ pageId, from }))
const allPosts = await getAllPosts({ notionPageData, from, pageType })
notionPageData.allPosts = allPosts
// 删除前端不需要的数据
// 获取文章列表
notionPageData.allPosts = await getAllPosts({ notionPageData, from, pageType })
delete notionPageData.block
delete notionPageData.collection
delete notionPageData.collectionQuery
@@ -49,14 +48,10 @@ 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)
}
@@ -171,22 +166,35 @@ function getAllCategories({ allPosts, categoryOptions, sliceCount = 0 }) {
function getBlogInfo({ collection, block }) {
const title = collection?.name?.[0][0] || BLOG.TITLE
const description = collection?.description?.[0][0] || BLOG.DESCRIPTION
const pageCover = mapCoverUrl(collection?.cover, block)
return { title, description, pageCover }
const pageCover = collection?.cover ? mapImgUrl(collection?.cover, block[idToUuid(BLOG.NOTION_PAGE_ID)]?.value) : BLOG.HOME_BANNER_IMAGE
const icon = collection?.icon ? mapCollectionImg(collection?.icon, collection) : BLOG.AVATAR
return { title, description, pageCover, icon }
}
/**
* 网站封面背景图
* Notion图片映射
* @param pageCover
* @returns {string}
*/
const mapCoverUrl = (pageCover, block) => {
if (!pageCover || pageCover === '') {
return BLOG.HOME_BANNER_IMAGE
const mapImgUrl = (img, value) => {
if (img) {
if (img.startsWith('/')) return 'https://www.notion.so' + img
if (img.startsWith('http')) return defaultMapImageUrl(img, value)
}
if (pageCover) {
if (pageCover.startsWith('/')) return 'https://www.notion.so' + pageCover
if (pageCover.startsWith('http')) return defaultMapImageUrl(pageCover, block[idToUuid(BLOG.NOTION_PAGE_ID)].value)
}
/**
* collection 图片映射
* @param {*} img
* @param {*} value
* @returns
*/
const mapCollectionImg = (img, value) => {
if (img) {
if (img.startsWith('/')) return 'https://www.notion.so' + img
if (img.startsWith('http')) {
return 'https://www.notion.so/image/' + encodeURIComponent(img) + '?table=collection&id=' + value.id
}
}
}
@@ -246,9 +254,10 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
const categories = getAllCategories({ allPosts, categoryOptions, sliceCount: BLOG.PREVIEW_CATEGORY_COUNT })
const tags = getAllTags({ allPosts, tagOptions, sliceCount: BLOG.PREVIEW_TAG_COUNT })
const latestPosts = getLatestPosts({ allPosts, from, latestPostCount: 5 })
const siteInfo = getBlogInfo({ collection, block })
const siteInfo = getBlogInfo({ collection: collection, block: block })
return {
siteInfo,
collection,
collectionQuery,
collectionId,
@@ -259,7 +268,6 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
tagOptions,
categoryOptions,
rawMetadata,
siteInfo,
customNav,
postCount,
pageIds,