movie 404

This commit is contained in:
tangly1024.com
2024-03-15 12:44:34 +08:00
parent ac91695d75
commit 3ed5f16a20
11 changed files with 496 additions and 139 deletions

View File

@@ -25,10 +25,7 @@ export { getPost } from '../notion/getNotionPost'
* @returns
*
*/
export async function getGlobalData({
pageId = BLOG.NOTION_PAGE_ID,
from
}) {
export async function getGlobalData({ pageId = BLOG.NOTION_PAGE_ID, from }) {
// 从notion获取
const data = await getNotionPageData({ pageId, from })
const db = deepClone(data)
@@ -89,7 +86,9 @@ function cleanBlock(post) {
* @returns
*/
function getLatestPosts({ allPages, from, latestPostCount }) {
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = allPages?.filter(
page => page.type === 'Post' && page.status === 'Published'
)
const latestPosts = Object.create(allPosts).sort((a, b) => {
const dateA = new Date(a?.lastEditedDate || a?.publishDate)
@@ -140,7 +139,13 @@ function getCustomNav({ allPages }) {
p.to = '/' + p.slug
}
}
customNav.push({ icon: p.icon || null, name: p.title, to: p.slug, target: '_blank', show: true })
customNav.push({
icon: p.icon || null,
name: p.title,
to: p.slug,
target: '_blank',
show: true
})
})
}
return customNav
@@ -152,7 +157,12 @@ function getCustomNav({ allPages }) {
* @returns
*/
function getCustomMenu({ collectionData }) {
const menuPages = collectionData.filter(post => post.status === 'Published' && (post?.type === BLOG.NOTION_PROPERTY_NAME.type_menu || post?.type === BLOG.NOTION_PROPERTY_NAME.type_sub_menu))
const menuPages = collectionData.filter(
post =>
post.status === 'Published' &&
(post?.type === BLOG.NOTION_PROPERTY_NAME.type_menu ||
post?.type === BLOG.NOTION_PROPERTY_NAME.type_sub_menu)
)
const menus = []
if (menuPages && menuPages.length > 0) {
menuPages.forEach(e => {
@@ -190,7 +200,9 @@ function getCustomMenu({ collectionData }) {
*/
function getTagOptions(schema) {
if (!schema) return {}
const tagSchema = Object.values(schema).find(e => e.name === BLOG.NOTION_PROPERTY_NAME.tags)
const tagSchema = Object.values(schema).find(
e => e.name === BLOG.NOTION_PROPERTY_NAME.tags
)
return tagSchema?.options || []
}
@@ -201,7 +213,9 @@ function getTagOptions(schema) {
*/
function getCategoryOptions(schema) {
if (!schema) return {}
const categorySchema = Object.values(schema).find(e => e.name === BLOG.NOTION_PROPERTY_NAME.category)
const categorySchema = Object.values(schema).find(
e => e.name === BLOG.NOTION_PROPERTY_NAME.category
)
return categorySchema?.options || []
}
@@ -213,9 +227,15 @@ function getCategoryOptions(schema) {
*/
function getSiteInfo({ collection, block }) {
const title = collection?.name?.[0][0] || BLOG.TITLE
const description = collection?.description ? Object.assign(collection).description[0][0] : BLOG.DESCRIPTION
const pageCover = collection?.cover ? mapImgUrl(collection?.cover, block[idToUuid(BLOG.NOTION_PAGE_ID)]?.value) : BLOG.HOME_BANNER_IMAGE
let icon = collection?.icon ? mapImgUrl(collection?.icon, collection, 'collection') : BLOG.AVATAR
const description = collection?.description
? Object.assign(collection).description[0][0]
: BLOG.DESCRIPTION
const pageCover = collection?.cover
? mapImgUrl(collection?.cover, block[idToUuid(BLOG.NOTION_PAGE_ID)]?.value)
: BLOG.HOME_BANNER_IMAGE
let icon = collection?.icon
? mapImgUrl(collection?.icon, collection, 'collection')
: BLOG.AVATAR
// 用户头像压缩一下
icon = compressImage(icon)
@@ -236,7 +256,13 @@ function getSiteInfo({ collection, block }) {
*/
export function getNavPages({ allPages }) {
const allNavPages = allPages?.filter(post => {
return post && post?.slug && (!post?.slug?.startsWith('http')) && post?.type === 'Post' && post?.status === 'Published'
return (
post &&
post?.slug &&
!post?.slug?.startsWith('http') &&
post?.type === 'Post' &&
post?.status === 'Published'
)
})
return allNavPages.map(item => ({
@@ -248,7 +274,8 @@ export function getNavPages({ allPages }) {
summary: item.summary || null,
slug: item.slug,
pageIcon: item.pageIcon || '',
lastEditedDate: item.lastEditedDate
lastEditedDate: item.lastEditedDate,
publishDate: item.publishDate
}))
}
@@ -265,19 +292,26 @@ async function getNotice(post) {
}
// 没有数据时返回
const EmptyData = (pageId) => {
const EmptyData = pageId => {
const empty = {
notice: null,
siteInfo: getSiteInfo({}),
allPages: [{
id: 1,
title: `无法获取Notion数据请检查Notion_ID \n 当前 ${pageId}`,
summary: '访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next',
status: 'Published',
type: 'Post',
slug: '13a171332816461db29d50e9f575b00d',
date: { start_date: '2023-04-24', lastEditedDay: '2023-04-24', tagItems: [] }
}],
allPages: [
{
id: 1,
title: `无法获取Notion数据请检查Notion_ID \n 当前 ${pageId}`,
summary:
'访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next',
status: 'Published',
type: 'Post',
slug: '13a171332816461db29d50e9f575b00d',
date: {
start_date: '2023-04-24',
lastEditedDay: '2023-04-24',
tagItems: []
}
}
],
allNavPages: [],
collection: [],
collectionQuery: {},
@@ -313,7 +347,8 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
const rawMetadata = block[pageId]?.value
// Check Type Page-Database和Inline-Database
if (
rawMetadata?.type !== 'collection_view_page' && rawMetadata?.type !== 'collection_view'
rawMetadata?.type !== 'collection_view_page' &&
rawMetadata?.type !== 'collection_view'
) {
console.error(`pageId "${pageId}" is not a database`)
return EmptyData(pageId)
@@ -328,9 +363,21 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
const viewIds = rawMetadata?.view_ids
const collectionData = []
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
const pageIds = getAllPageIds(
collectionQuery,
collectionId,
collectionView,
viewIds
)
if (pageIds?.length === 0) {
console.error('获取到的文章列表为空请检查notion模板', collectionQuery, collection, collectionView, viewIds, pageRecordMap)
console.error(
'获取到的文章列表为空请检查notion模板',
collectionQuery,
collection,
collectionView,
viewIds,
pageRecordMap
)
} else {
// console.log('有效Page数量', pageIds?.length)
}
@@ -343,7 +390,14 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
// 如果找不到文章对应的block说明发生了溢出使用pageID再去请求
const pageBlock = await getSingleBlock(id, from)
if (pageBlock.block[id].value) {
const properties = (await getPageProperties(id, pageBlock.block[id].value, schema, null, getTagOptions(schema))) || null
const properties =
(await getPageProperties(
id,
pageBlock.block[id].value,
schema,
null,
getTagOptions(schema)
)) || null
if (properties) {
collectionData.push(properties)
}
@@ -351,7 +405,14 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
continue
}
const properties = (await getPageProperties(id, value, schema, null, getTagOptions(schema))) || null
const properties =
(await getPageProperties(
id,
value,
schema,
null,
getTagOptions(schema)
)) || null
if (properties) {
collectionData.push(properties)
}
@@ -365,13 +426,16 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
if (post?.type === 'Post' && post.status === 'Published') {
postCount++
}
return post && post?.slug &&
(!post?.slug?.startsWith('http')) &&
return (
post &&
post?.slug &&
!post?.slug?.startsWith('http') &&
(post?.status === 'Invisible' || post?.status === 'Published')
)
})
// 站点配置优先读取配置表格否则读取blog.config.js 文件
const NOTION_CONFIG = await getConfigMapFromConfigPage(collectionData) || {}
const NOTION_CONFIG = (await getConfigMapFromConfigPage(collectionData)) || {}
// Sort by date
if (BLOG.POSTS_SORT_BY === 'date') {
@@ -380,13 +444,27 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
})
}
const notice = await getNotice(collectionData.filter(post => {
return post && post?.type && post?.type === 'Notice' && post.status === 'Published'
})?.[0])
const categoryOptions = getAllCategories({ allPages, categoryOptions: getCategoryOptions(schema) })
const notice = await getNotice(
collectionData.filter(post => {
return (
post &&
post?.type &&
post?.type === 'Notice' &&
post.status === 'Published'
)
})?.[0]
)
const categoryOptions = getAllCategories({
allPages,
categoryOptions: getCategoryOptions(schema)
})
const tagOptions = getAllTags({ allPages, tagOptions: getTagOptions(schema) })
// 旧的菜单
const customNav = getCustomNav({ allPages: collectionData.filter(post => post?.type === 'Page' && post.status === 'Published') })
const customNav = getCustomNav({
allPages: collectionData.filter(
post => post?.type === 'Page' && post.status === 'Published'
)
})
// 新的菜单
const customMenu = await getCustomMenu({ collectionData })
const latestPosts = getLatestPosts({ allPages, from, latestPostCount: 6 })