diff --git a/lib/notion/getNotion.js b/lib/notion/getNotion.js new file mode 100644 index 00000000..7ce14180 --- /dev/null +++ b/lib/notion/getNotion.js @@ -0,0 +1,37 @@ +import BLOG from '@/blog.config' +import { idToUuid } from 'notion-utils' +import formatDate from '../formatDate' +import { getPostBlocks } from './getPostBlocks' +import { defaultMapImageUrl } from 'react-notion-x' + +export async function getNotion(pageId) { + const blockMap = await getPostBlocks(pageId, 'slug') + if (!blockMap) { + return null + } + + const postInfo = blockMap?.block?.[idToUuid(pageId)].value + + return { + id: pageId, + type: postInfo, + category: '', + tags: [], + title: postInfo?.properties?.title?.[0], + status: 'Published', + createdTime: formatDate(new Date(postInfo.created_time).toString(), BLOG.LANG), + lastEditedTime: formatDate(new Date(postInfo?.last_edited_time).toString(), BLOG.LANG), + fullWidth: false, + page_cover: getPageCover(postInfo), + date: { start_date: formatDate(new Date(postInfo?.last_edited_time).toString(), BLOG.LANG) }, + blockMap + } +} + +function getPageCover(postInfo) { + const pageCover = postInfo.format?.page_cover + if (pageCover) { + if (pageCover.startsWith('/')) return 'https://www.notion.so' + pageCover + if (pageCover.startsWith('http')) return defaultMapImageUrl(pageCover, postInfo) + } +} diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index 47cb9dae..920ab1a4 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -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, siteInfo) { +export default 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 @@ -96,5 +96,3 @@ async function getPageProperties(id, block, schema, authToken, tagOptions, siteI } } } - -export { getPageProperties as default } diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index f5712b8c..685e8d46 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -28,7 +28,7 @@ export async function getPostBlocks(id, from, slice) { * @param {*} id * @param {*} retryAttempts */ -async function getPageWithRetry(id, from, retryAttempts = 3) { +export async function getPageWithRetry(id, from, retryAttempts = 3) { if (retryAttempts && retryAttempts > 0) { console.log('[请求API]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '') try { diff --git a/pages/[...slug].js b/pages/[...slug].js index 97327526..0dfe01b5 100644 --- a/pages/[...slug].js +++ b/pages/[...slug].js @@ -7,6 +7,7 @@ import React from 'react' import { idToUuid } from 'notion-utils' import Router from 'next/router' import { isBrowser } from '@/lib/utils' +import { getNotion } from '@/lib/notion/getNotion' /** * 根据notion的slug访问页面 @@ -26,11 +27,11 @@ const Slug = props => { const article = document.getElementById('container') if (!article) { router.push('/404').then(() => { - // console.warn('找不到页面', router.asPath) + console.warn('找不到页面', router.asPath) }) } } - }, 10000) + }, 20 * 1000) const meta = { title: `${props?.siteInfo?.title || BLOG.TITLE} | loading`, image: siteInfo?.pageCover } return } @@ -62,7 +63,7 @@ const Slug = props => { const meta = { title: `${post?.title} | ${siteInfo?.title}`, description: post?.summary, - type: post.type, + type: post?.type, slug: post?.slug, image: post?.page_cover, category: post?.category?.[0], @@ -102,11 +103,17 @@ export async function getStaticProps({ params: { slug } }) { props.post = props.allPages.find((p) => { return p.slug === fullSlug || p.id === idToUuid(fullSlug) }) + if (!props.post) { - console.warn('无效地址', fullSlug) - return { props, revalidate: 1 } + const post = await getNotion(slug.slice(-1)[0]) + if (post) { + props.post = post + } else { + return { props, revalidate: 1 } + } + } else { + props.post.blockMap = await getPostBlocks(props.post.id, 'slug') } - props.post.blockMap = await getPostBlocks(props.post.id, 'slug') const allPosts = props.allPages.filter(page => page.type === 'Post') const index = allPosts.indexOf(props.post) @@ -134,7 +141,7 @@ export async function getStaticProps({ params: { slug } }) { function getRecommendPost(post, allPosts, count = 6) { let recommendPosts = [] const postIds = [] - const currentTags = post.tags || [] + const currentTags = post?.tags || [] for (let i = 0; i < allPosts.length; i++) { const p = allPosts[i] if (p.id === post.id || p.type.indexOf('Post') < 0) { diff --git a/themes/example/components/ArticleInfo.js b/themes/example/components/ArticleInfo.js index f7dc5a80..7317d533 100644 --- a/themes/example/components/ArticleInfo.js +++ b/themes/example/components/ArticleInfo.js @@ -10,7 +10,7 @@ export const ArticleInfo = (props) => { return - {post?.type[0] !== 'Page' && <> + {post?.type !== 'Page' && <> @@ -20,7 +20,7 @@ export const ArticleInfo = (props) => { | >} - {post?.type[0] !== 'Page' && (<> + {post?.type !== 'Page' && (<> | >)} - {post?.type[0] !== 'Page' && (<> + {post?.type !== 'Page' && (<> } - {post?.type[0] !== 'Page' && ( + {post?.type !== 'Page' && ( <> {showArticleInfo && - {post?.type && !post?.type.includes('Page') && post?.page_cover && ( + {post?.type && !post?.type !== 'Page' && post?.page_cover && ( {/* eslint-disable-next-line @next/next/no-img-element */} @@ -51,7 +51,7 @@ export default function ArticleDetail(props) { | >} - {post?.type[0] !== 'Page' && (<> + {post?.type !== 'Page' && (<>