mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-08 07:26:47 +00:00
137 lines
3.8 KiB
JavaScript
137 lines
3.8 KiB
JavaScript
import BLOG from '@/blog.config'
|
||
import { siteConfig } from '@/lib/config'
|
||
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
|
||
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
||
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
|
||
import { checkSlugHasOneSlash, getRecommendPost } from '@/lib/utils/post'
|
||
import { idToUuid } from 'notion-utils'
|
||
import Slug from '..'
|
||
|
||
/**
|
||
* 根据notion的slug访问页面
|
||
* 解析二级目录 /article/about
|
||
* @param {*} props
|
||
* @returns
|
||
*/
|
||
const PrefixSlug = props => {
|
||
return <Slug {...props} />
|
||
}
|
||
|
||
export async function getStaticPaths() {
|
||
if (!BLOG.isProd) {
|
||
return {
|
||
paths: [],
|
||
fallback: true
|
||
}
|
||
}
|
||
|
||
const from = 'slug-paths'
|
||
const { allPages } = await getGlobalData({ from })
|
||
|
||
// 根据slug中的 / 分割成prefix和slug两个字段 ; 例如 article/test
|
||
// 最终用户可以通过 [domain]/[prefix]/[slug] 路径访问,即这里的 [domain]/article/test
|
||
const paths = allPages
|
||
?.filter(row => checkSlugHasOneSlash(row))
|
||
.map(row => ({
|
||
params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1] }
|
||
}))
|
||
|
||
// 增加一种访问路径 允许通过 [category]/[slug] 访问文章
|
||
// 例如文章slug 是 test ,然后文章的分类category是 production
|
||
// 则除了 [domain]/[slug] 以外,还支持分类名访问: [domain]/[category]/[slug]
|
||
|
||
return {
|
||
paths: paths,
|
||
fallback: true
|
||
}
|
||
}
|
||
|
||
export async function getStaticProps({ params: { prefix, slug }, locale }) {
|
||
const fullSlug = prefix + '/' + slug
|
||
const from = `slug-props-${fullSlug}`
|
||
const props = await getGlobalData({ from, locale })
|
||
|
||
// 在列表内查找文章
|
||
props.post = props?.allPages?.find(p => {
|
||
return (
|
||
p.type.indexOf('Menu') < 0 &&
|
||
(p.slug === slug || p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
||
)
|
||
})
|
||
|
||
// 处理非列表内文章的内信息
|
||
if (!props?.post) {
|
||
const pageId = slug.slice(-1)[0]
|
||
if (pageId.length >= 32) {
|
||
const post = await getPost(pageId)
|
||
props.post = post
|
||
}
|
||
}
|
||
|
||
// 无法获取文章
|
||
if (!props?.post) {
|
||
props.post = null
|
||
return {
|
||
props,
|
||
revalidate: process.env.EXPORT
|
||
? undefined
|
||
: siteConfig(
|
||
'NEXT_REVALIDATE_SECOND',
|
||
BLOG.NEXT_REVALIDATE_SECOND,
|
||
props.NOTION_CONFIG
|
||
)
|
||
}
|
||
}
|
||
|
||
// 文章内容加载
|
||
if (!props?.post?.blockMap) {
|
||
props.post.blockMap = await getPostBlocks(props.post.id, from)
|
||
}
|
||
|
||
// 目录默认加载
|
||
if (props.post?.blockMap?.block) {
|
||
props.post.content = Object.keys(props.post.blockMap.block).filter(
|
||
key => props.post.blockMap.block[key]?.value?.parent_id === props.post.id
|
||
)
|
||
props.post.toc = getPageTableOfContents(props.post, props.post.blockMap)
|
||
}
|
||
|
||
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)
|
||
if (BLOG.ALGOLIA_APP_ID) {
|
||
uploadDataToAlgolia(props?.post)
|
||
}
|
||
|
||
// 推荐关联文章处理
|
||
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]
|
||
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
|
||
props.recommendPosts = getRecommendPost(
|
||
props.post,
|
||
allPosts,
|
||
siteConfig('POST_RECOMMEND_COUNT')
|
||
)
|
||
} else {
|
||
props.prev = null
|
||
props.next = null
|
||
props.recommendPosts = []
|
||
}
|
||
|
||
delete props.allPages
|
||
return {
|
||
props,
|
||
revalidate: process.env.EXPORT
|
||
? undefined
|
||
: siteConfig(
|
||
'NEXT_REVALIDATE_SECOND',
|
||
BLOG.NEXT_REVALIDATE_SECOND,
|
||
props.NOTION_CONFIG
|
||
)
|
||
}
|
||
}
|
||
|
||
export default PrefixSlug
|