From 47b3fa28e9955d019bc3f29d30f4e7b736036671 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Tue, 11 Jul 2023 18:28:00 +0800 Subject: [PATCH] fix post?. --- lib/notion/getNotionData.js | 6 +++--- pages/search/[keyword]/index.js | 2 +- pages/search/[keyword]/page/[page].js | 2 +- pages/search/index.js | 2 +- pages/tag/[tag]/index.js | 2 +- pages/tag/[tag]/page/[page].js | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 91aa029e..89126d9c 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -102,7 +102,7 @@ function getCustomNav({ allPages }) { * @returns */ function getCustomMenu({ collectionData }) { - const menuPages = collectionData.filter(post => (post.type === BLOG.NOTION_PROPERTY_NAME.type_menu || post.type === BLOG.NOTION_PROPERTY_NAME.type_sub_menu) && post.status === 'Published') + const menuPages = collectionData.filter(post => (post?.type === BLOG.NOTION_PROPERTY_NAME.type_menu || post?.type === BLOG.NOTION_PROPERTY_NAME.type_sub_menu) && post.status === 'Published') const menus = [] if (menuPages && menuPages.length > 0) { menuPages.forEach(e => { @@ -287,7 +287,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) { let postCount = 0 // 查找所有的Post和Page const allPages = collectionData.filter(post => { - if (post.type === 'Post' && post.status === 'Published') { + if (post?.type === 'Post' && post.status === 'Published') { postCount++ } return post && post?.slug && @@ -306,7 +306,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) { 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: 5 }) diff --git a/pages/search/[keyword]/index.js b/pages/search/[keyword]/index.js index 59e26f96..c2e9817c 100644 --- a/pages/search/[keyword]/index.js +++ b/pages/search/[keyword]/index.js @@ -117,7 +117,7 @@ async function filterByMemCache(allPosts, keyword) { for (const post of allPosts) { const cacheKey = 'page_block_' + post.id const page = await getDataFromCache(cacheKey, true) - const tagContent = post.tags && Array.isArray(post.tags) ? post.tags.join(' ') : '' + const tagContent = post?.tags && Array.isArray(post?.tags) ? post?.tags.join(' ') : '' const categoryContent = post.category && Array.isArray(post.category) ? post.category.join(' ') : '' const articleInfo = post.title + post.summary + tagContent + categoryContent let hit = articleInfo.toLowerCase().indexOf(keyword) > -1 diff --git a/pages/search/[keyword]/page/[page].js b/pages/search/[keyword]/page/[page].js index e706e9b8..11a682f7 100644 --- a/pages/search/[keyword]/page/[page].js +++ b/pages/search/[keyword]/page/[page].js @@ -115,7 +115,7 @@ async function filterByMemCache(allPosts, keyword) { for (const post of allPosts) { const cacheKey = 'page_block_' + post.id const page = await getDataFromCache(cacheKey, true) - const tagContent = post.tags && Array.isArray(post.tags) ? post.tags.join(' ') : '' + const tagContent = post?.tags && Array.isArray(post?.tags) ? post?.tags.join(' ') : '' const categoryContent = post.category && Array.isArray(post.category) ? post.category.join(' ') : '' const articleInfo = post.title + post.summary + tagContent + categoryContent let hit = articleInfo.indexOf(keyword) > -1 diff --git a/pages/search/index.js b/pages/search/index.js index eb545434..ba09db8e 100644 --- a/pages/search/index.js +++ b/pages/search/index.js @@ -23,7 +23,7 @@ const Search = props => { // 静态过滤 if (keyword) { filteredPosts = posts.filter(post => { - const tagContent = post.tags ? post.tags.join(' ') : '' + const tagContent = post?.tags ? post?.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' const searchContent = post.title + post.summary + tagContent + categoryContent diff --git a/pages/tag/[tag]/index.js b/pages/tag/[tag]/index.js index a211fc36..db2aaed8 100644 --- a/pages/tag/[tag]/index.js +++ b/pages/tag/[tag]/index.js @@ -33,7 +33,7 @@ export async function getStaticProps({ params: { tag } }) { const props = await getGlobalData({ from }) // 过滤状态 - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.tags && post.tags.includes(tag)) + props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag)) // 处理文章页数 props.postCount = props.posts.length diff --git a/pages/tag/[tag]/page/[page].js b/pages/tag/[tag]/page/[page].js index 92d01820..bf383870 100644 --- a/pages/tag/[tag]/page/[page].js +++ b/pages/tag/[tag]/page/[page].js @@ -27,7 +27,7 @@ export async function getStaticProps({ params: { tag, page } }) { const from = 'tag-page-props' const props = await getGlobalData({ from }) // 过滤状态、标签 - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.tags && post.tags.includes(tag)) + props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag)) // 处理文章数 props.postCount = props.posts.length // 处理分页 @@ -48,7 +48,7 @@ export async function getStaticPaths() { const paths = [] tagOptions?.forEach(tag => { // 过滤状态类型 - const tagPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.tags && post.tags.includes(tag.name)) + const tagPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag.name)) // 处理文章页数 const postCount = tagPosts.length const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE)