From 49831089d9d1e3b33f6e3c944df18ce3ba7228ae Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sun, 30 Jul 2023 15:22:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=BC=96=E8=AF=91=EF=BC=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=94=AF=E6=8C=81=E5=A4=9A=E8=A7=86?= =?UTF-8?q?=E5=9B=BE=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getAllCategories.js | 2 +- lib/notion/getAllPageIds.js | 16 +++++++++------- lib/notion/getAllTags.js | 2 +- lib/notion/getNotionData.js | 2 +- pages/[prefix]/[slug].js | 2 +- pages/[prefix]/index.js | 2 +- pages/archive/index.js | 2 +- pages/category/[category]/index.js | 2 +- pages/category/[category]/page/[page].js | 4 ++-- pages/index.js | 2 +- pages/page/[page].js | 2 +- 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 ++-- 16 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/notion/getAllCategories.js b/lib/notion/getAllCategories.js index 7cfd4588..97c8ab3a 100644 --- a/lib/notion/getAllCategories.js +++ b/lib/notion/getAllCategories.js @@ -14,7 +14,7 @@ import { isIterable } from '../utils' * @returns {Promise<{}|*[]>} */ export function getAllCategories({ allPages, categoryOptions, sliceCount = 0 }) { - const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') + const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') if (!allPosts || !categoryOptions) { return [] } diff --git a/lib/notion/getAllPageIds.js b/lib/notion/getAllPageIds.js index e8f6dd8f..4bb12fe0 100644 --- a/lib/notion/getAllPageIds.js +++ b/lib/notion/getAllPageIds.js @@ -4,7 +4,15 @@ export default function getAllPageIds (collectionQuery, collectionId, collection return [] } let pageIds = [] - if (collectionQuery && Object.values(collectionQuery).length > 0) { + // 优先按照第一个视图排序 + if (viewIds && viewIds.length > 0) { + const ids = collectionView[viewIds[0]].value.page_sort + // console.log('PageIds: 从viewId获取', viewIds) + for (const id of ids) { + pageIds.push(id) + } + // 否则按照数据库原始排序 + } else if (collectionQuery && Object.values(collectionQuery).length > 0) { const pageSet = new Set() Object.values(collectionQuery[collectionId]).forEach(view => { view?.blockIds?.forEach(id => pageSet.add(id)) // group视图 @@ -12,12 +20,6 @@ export default function getAllPageIds (collectionQuery, collectionId, collection }) pageIds = [...pageSet] // console.log('PageIds: 从collectionQuery获取', collectionQuery, pageIds.length) - } else if (viewIds && viewIds.length > 0) { - const ids = collectionView[viewIds[0]].value.page_sort - // console.log('PageIds: 从viewId获取', viewIds) - for (const id of ids) { - pageIds.push(id) - } } return pageIds } diff --git a/lib/notion/getAllTags.js b/lib/notion/getAllTags.js index 968b8ca3..dfc92ac7 100644 --- a/lib/notion/getAllTags.js +++ b/lib/notion/getAllTags.js @@ -8,7 +8,7 @@ import { isIterable } from '../utils' * @returns {Promise<{}|*[]>} */ export function getAllTags({ allPages, sliceCount = 0, tagOptions }) { - const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') + const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') if (!allPosts || !tagOptions) { return [] diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 24b1fb8e..4e42d349 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -180,7 +180,7 @@ function getSiteInfo({ collection, block }) { * @param {*} param0 */ export function getNavPages({ allPages }) { - const allNavPages = allPages.filter(post => { + const allNavPages = allPages?.filter(post => { return post && post?.slug && (!post?.slug?.startsWith('http')) && post?.type === 'Post' && post?.status === 'Published' }) diff --git a/pages/[prefix]/[slug].js b/pages/[prefix]/[slug].js index 7b99ddd8..237b793a 100644 --- a/pages/[prefix]/[slug].js +++ b/pages/[prefix]/[slug].js @@ -70,7 +70,7 @@ export async function getStaticProps({ params: { prefix, slug } }) { } // 推荐关联文章处理 - const allPosts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') + const allPosts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published') if (allPosts && allPosts.length > 0) { const index = allPosts.indexOf(props.post) props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0] diff --git a/pages/[prefix]/index.js b/pages/[prefix]/index.js index e20bf690..885ea4aa 100644 --- a/pages/[prefix]/index.js +++ b/pages/[prefix]/index.js @@ -135,7 +135,7 @@ export async function getStaticProps({ params: { prefix } }) { } // 推荐关联文章处理 - const allPosts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') + const allPosts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published') if (allPosts && allPosts.length > 0) { const index = allPosts.indexOf(props.post) props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0] diff --git a/pages/archive/index.js b/pages/archive/index.js index 917fee76..1ef1f216 100644 --- a/pages/archive/index.js +++ b/pages/archive/index.js @@ -44,7 +44,7 @@ const ArchiveIndex = props => { export async function getStaticProps() { const props = await getGlobalData({ from: 'archive-index' }) // 处理分页 - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') + props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published') delete props.allPages const postsSortByDate = Object.create(props.posts) diff --git a/pages/category/[category]/index.js b/pages/category/[category]/index.js index 55300733..03cd4fbd 100644 --- a/pages/category/[category]/index.js +++ b/pages/category/[category]/index.js @@ -37,7 +37,7 @@ export async function getStaticProps({ params: { category } }) { let props = await getGlobalData({ from }) // 过滤状态 - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') + props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published') // 处理过滤 props.posts = props.posts.filter(post => post && post.category && post.category.includes(category)) // 处理文章页数 diff --git a/pages/category/[category]/page/[page].js b/pages/category/[category]/page/[page].js index 2f860c25..9174e117 100644 --- a/pages/category/[category]/page/[page].js +++ b/pages/category/[category]/page/[page].js @@ -37,7 +37,7 @@ export async function getStaticProps({ params: { category, page } }) { let props = await getGlobalData({ from }) // 过滤状态类型 - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category)) + props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category)) // 处理文章页数 props.postCount = props.posts.length // 处理分页 @@ -61,7 +61,7 @@ export async function getStaticPaths() { categoryOptions?.forEach(category => { // 过滤状态类型 - const categoryPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category.name)) + const categoryPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category.name)) // 处理文章页数 const postCount = categoryPosts.length const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE) diff --git a/pages/index.js b/pages/index.js index fbbe0eff..328ece1e 100644 --- a/pages/index.js +++ b/pages/index.js @@ -26,7 +26,7 @@ export async function getStaticProps() { const props = await getGlobalData({ from }) const { siteInfo } = props - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') + props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published') const meta = { title: `${siteInfo?.title} | ${siteInfo?.description}`, diff --git a/pages/page/[page].js b/pages/page/[page].js index 74b7f25b..02b847a8 100644 --- a/pages/page/[page].js +++ b/pages/page/[page].js @@ -45,7 +45,7 @@ export async function getStaticProps({ params: { page } }) { const from = `page-${page}` const props = await getGlobalData({ from }) const { allPages } = props - const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') + const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') // 处理分页 props.posts = allPosts.slice(BLOG.POSTS_PER_PAGE * (page - 1), BLOG.POSTS_PER_PAGE * page) props.page = page diff --git a/pages/search/[keyword]/index.js b/pages/search/[keyword]/index.js index 93fa6826..042bbf85 100644 --- a/pages/search/[keyword]/index.js +++ b/pages/search/[keyword]/index.js @@ -36,7 +36,7 @@ export async function getStaticProps({ params: { keyword } }) { pageType: ['Post'] }) const { allPages } = props - const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') + const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') props.posts = await filterByMemCache(allPosts, keyword) props.postCount = props.posts.length // 处理分页 diff --git a/pages/search/[keyword]/page/[page].js b/pages/search/[keyword]/page/[page].js index 11a682f7..20240f19 100644 --- a/pages/search/[keyword]/page/[page].js +++ b/pages/search/[keyword]/page/[page].js @@ -36,7 +36,7 @@ export async function getStaticProps({ params: { keyword, page } }) { pageType: ['Post'] }) const { allPages } = props - const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') + const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') props.posts = await filterByMemCache(allPosts, keyword) props.postCount = props.posts.length // 处理分页 diff --git a/pages/search/index.js b/pages/search/index.js index ba09db8e..22061cbd 100644 --- a/pages/search/index.js +++ b/pages/search/index.js @@ -55,7 +55,7 @@ export async function getStaticProps() { pageType: ['Post'] }) const { allPages } = props - props.posts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') + props.posts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) diff --git a/pages/tag/[tag]/index.js b/pages/tag/[tag]/index.js index db2aaed8..bd9f7529 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 bf383870..2eaef518 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)