mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 15:09:22 +00:00
86 lines
2.1 KiB
JavaScript
86 lines
2.1 KiB
JavaScript
import BLOG from '@/blog.config'
|
|
import { siteConfig } from '@/lib/config'
|
|
import { getGlobalData, getPostBlocks } from '@/lib/db/getSiteData'
|
|
import { generateRobotsTxt } from '@/lib/robots.txt'
|
|
import { generateRss } from '@/lib/rss'
|
|
import { generateSitemapXml } from '@/lib/sitemap.xml'
|
|
import { getLayoutByTheme } from '@/themes/theme'
|
|
import { useRouter } from 'next/router'
|
|
|
|
/**
|
|
* 首页布局
|
|
* @param {*} props
|
|
* @returns
|
|
*/
|
|
const Index = props => {
|
|
// 根据页面路径加载不同Layout文件
|
|
const Layout = getLayoutByTheme({
|
|
theme: siteConfig('THEME'),
|
|
router: useRouter()
|
|
})
|
|
return <Layout {...props} />
|
|
}
|
|
|
|
/**
|
|
* SSG 获取数据
|
|
* @returns
|
|
*/
|
|
export async function getStaticProps(req) {
|
|
const { locale } = req
|
|
const from = 'index'
|
|
const props = await getGlobalData({ from, locale })
|
|
const POST_PREVIEW_LINES = siteConfig(
|
|
'POST_PREVIEW_LINES',
|
|
12,
|
|
props?.NOTION_CONFIG
|
|
)
|
|
props.posts = props.allPages?.filter(
|
|
page => page.type === 'Post' && page.status === 'Published'
|
|
)
|
|
|
|
// 处理分页
|
|
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
|
|
// 滚动列表默认给前端返回所有数据
|
|
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
|
|
props.posts = props.posts?.slice(
|
|
0,
|
|
siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
|
|
)
|
|
}
|
|
|
|
// 预览文章内容
|
|
if (siteConfig('POST_LIST_PREVIEW', false, props?.NOTION_CONFIG)) {
|
|
for (const i in props.posts) {
|
|
const post = props.posts[i]
|
|
if (post.password && post.password !== '') {
|
|
continue
|
|
}
|
|
post.blockMap = await getPostBlocks(post.id, 'slug', POST_PREVIEW_LINES)
|
|
}
|
|
}
|
|
|
|
// 生成robotTxt
|
|
generateRobotsTxt(props)
|
|
// 生成Feed订阅
|
|
generateRss(props)
|
|
// 生成
|
|
generateSitemapXml(props)
|
|
|
|
// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'
|
|
|
|
delete props.allPages
|
|
|
|
return {
|
|
props,
|
|
revalidate: process.env.EXPORT
|
|
? undefined
|
|
: siteConfig(
|
|
'NEXT_REVALIDATE_SECOND',
|
|
BLOG.NEXT_REVALIDATE_SECOND,
|
|
props.NOTION_CONFIG
|
|
)
|
|
}
|
|
}
|
|
|
|
export default Index
|