diff --git a/lib/notion/getNotion.js b/lib/notion/getNotion.js index 7ce14180..32a6c9ca 100644 --- a/lib/notion/getNotion.js +++ b/lib/notion/getNotion.js @@ -4,6 +4,11 @@ import formatDate from '../formatDate' import { getPostBlocks } from './getPostBlocks' import { defaultMapImageUrl } from 'react-notion-x' +/** + * 根据页面ID获取内容 + * @param {*} pageId + * @returns + */ export async function getNotion(pageId) { const blockMap = await getPostBlocks(pageId, 'slug') if (!blockMap) { diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index 53715d36..5fe2af84 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -3,6 +3,13 @@ import { NotionAPI } from 'notion-client' import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager' import { deepClone, delay } from '../utils' +/** + * 获取文章内容 + * @param {*} id + * @param {*} from + * @param {*} slice + * @returns + */ export async function getPostBlocks(id, from, slice) { const cacheKey = 'page_block_' + id let pageBlock = await getDataFromCache(cacheKey) @@ -71,6 +78,13 @@ function filterPostBlocks(id, pageBlock, slice) { delete clonePageBlock?.block[i] continue } + // 当BlockId等于PageId时移除 + if (b?.value?.id === id) { + // 此block含有敏感信息 + delete b?.value?.properties + continue + } + count++ // 处理 c++、c#、汇编等语言名字映射 if (b?.value?.type === 'code') { diff --git a/pages/[...slug].js b/pages/[...slug].js index c88570a7..8ed9f270 100644 --- a/pages/[...slug].js +++ b/pages/[...slug].js @@ -114,25 +114,31 @@ export async function getStaticProps({ params: { slug } }) { } const from = `slug-props-${fullSlug}` const props = await getGlobalNotionData({ from }) + // 在列表内查找文章 props.post = props.allPages.find((p) => { return p.slug === fullSlug || p.id === idToUuid(fullSlug) }) - if (!props.post) { + // 处理非列表内文章的内信息 + if (!props?.post) { const pageId = slug.slice(-1)[0] - if (pageId.length < 32) { - return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) } - } - const post = await getNotion(pageId) - if (post) { + if (pageId.length >= 32) { + const post = await getNotion(pageId) props.post = post - } else { - return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) } } - } else { - props.post.blockMap = await getPostBlocks(props.post.id, 'slug') } + // 无法获取文章 + if (!props?.post) { + return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) } + } + + // 文章内容加载 + if (!props?.posts?.blockMap) { + props.post.blockMap = await getPostBlocks(props.post.id, from) + } + + // 推荐关联文章处理 const allPosts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') if (allPosts && allPosts.length > 0) { const index = allPosts.indexOf(props.post)