From c6b0926ff79702dbe9a02a5ce54b6fc832bc646e Mon Sep 17 00:00:00 2001 From: tangly Date: Sat, 12 Nov 2022 23:07:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E6=B8=B2=E6=9F=93category=E3=80=81tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/category/[category]/page/[page].js | 27 ++++++++++++++------ pages/tag/[tag]/index.js | 5 +--- pages/tag/[tag]/page/[page].js | 32 +++++++++++++----------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/pages/category/[category]/page/[page].js b/pages/category/[category]/page/[page].js index 04f11a20..122d48f5 100644 --- a/pages/category/[category]/page/[page].js +++ b/pages/category/[category]/page/[page].js @@ -33,10 +33,8 @@ export async function getStaticProps({ params: { category, page } }) { const from = 'category-page-props' let props = await getGlobalNotionData({ from }) - // 过滤状态 - 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)) + // 过滤状态类型 + 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 // 处理分页 @@ -55,11 +53,24 @@ export async function getStaticProps({ params: { category, page } }) { export async function getStaticPaths() { const from = 'category-paths' - const { categories } = await getGlobalNotionData({ from }) + const { categories, allPages } = await getGlobalNotionData({ from }) + const paths = [] + + categories?.forEach(category => { + // 过滤状态类型 + 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) + if (totalPages > 1) { + for (let i = 1; i <= totalPages; i++) { + paths.push({ params: { category: category.name, page: '' + i } }) + } + } + }) + return { - paths: Object.keys(categories).map(category => ({ - params: { category: categories[category]?.name, page: '1' } - })), + paths, fallback: true } } diff --git a/pages/tag/[tag]/index.js b/pages/tag/[tag]/index.js index ea9f9000..ca52f8f1 100644 --- a/pages/tag/[tag]/index.js +++ b/pages/tag/[tag]/index.js @@ -28,10 +28,7 @@ export async function getStaticProps({ params: { tag } }) { const props = await getGlobalNotionData({ from }) // 过滤状态 - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') - - // 处理标签 - props.posts = props.posts.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 d9dd99fe..6317c377 100644 --- a/pages/tag/[tag]/page/[page].js +++ b/pages/tag/[tag]/page/[page].js @@ -26,18 +26,16 @@ const Tag = props => { export async function getStaticProps({ params: { tag, page } }) { const from = 'tag-page-props' const props = await getGlobalNotionData({ from }) - // 过滤状态 - props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') - // 过滤标签 - props.posts = props.posts.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 // 处理分页 props.posts = props.posts.slice(BLOG.POSTS_PER_PAGE * (page - 1), BLOG.POSTS_PER_PAGE * page - 1) props.tag = tag props.page = page - + delete props.allPages return { props, revalidate: 1 @@ -46,16 +44,22 @@ export async function getStaticProps({ params: { tag, page } }) { export async function getStaticPaths() { const from = 'tag-page-static-path' - const { tags } = await getGlobalNotionData({ from }) - const tagNames = [] - tags.forEach(tag => { - tagNames.push(tag.name) + const { tags, allPages } = await getGlobalNotionData({ from }) + const paths = [] + tags?.forEach(tag => { + // 过滤状态类型 + const categoryPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.tags.includes(tag.name)) + // 处理文章页数 + const postCount = categoryPosts.length + const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE) + if (totalPages > 1) { + for (let i = 1; i <= totalPages; i++) { + paths.push({ params: { tag: tag.name, page: '' + i } }) + } + } }) - return { - paths: Object.keys(tagNames).map(index => ({ - params: { tag: tagNames[index], page: '1' } - })), + paths: paths, fallback: true } }