Files
NotionNext/pages/category/[category]/index.js
tangly1024.com da73eafbe4 fix build
2024-04-12 13:07:16 +08:00

67 lines
1.7 KiB
JavaScript

import BLOG from '@/blog.config'
import { siteConfig } from '@/lib/config'
import { getGlobalData } from '@/lib/db/getSiteData'
import { getLayoutByTheme } from '@/themes/theme'
import { useRouter } from 'next/router'
/**
* 分类页
* @param {*} props
* @returns
*/
export default function Category(props) {
// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme({
theme: siteConfig('THEME'),
router: useRouter()
})
return <Layout {...props} />
}
export async function getStaticProps({ params: { category }, locale }) {
const from = 'category-props'
let props = await getGlobalData({ from, locale })
// 过滤状态
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.postCount = props.posts.length
// 处理分页
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
// 滚动列表 给前端返回所有数据
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
}
delete props.allPages
props = { ...props, category }
return {
props,
revalidate: siteConfig(
'NEXT_REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
}
}
export async function getStaticPaths() {
const from = 'category-paths'
const { categoryOptions } = await getGlobalData({ from })
return {
paths: Object.keys(categoryOptions).map(category => ({
params: { category: categoryOptions[category]?.name }
})),
fallback: true
}
}