mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-01 07:26:47 +00:00
148 lines
3.4 KiB
JavaScript
148 lines
3.4 KiB
JavaScript
import BLOG from '@/blog.config'
|
|
import { siteConfig } from '@/lib/config'
|
|
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
|
|
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
|
|
import { checkContainHttp } from '@/lib/utils'
|
|
import { idToUuid } from 'notion-utils'
|
|
import Slug, { getRecommendPost } from '..'
|
|
|
|
/**
|
|
* 根据notion的slug访问页面
|
|
* 解析三级以上目录 /article/2023/10/29/test
|
|
* @param {*} props
|
|
* @returns
|
|
*/
|
|
const PrefixSlug = props => {
|
|
return <Slug {...props} />
|
|
}
|
|
|
|
/**
|
|
* 编译渲染页面路径
|
|
* @returns
|
|
*/
|
|
export async function getStaticPaths() {
|
|
if (!BLOG.isProd) {
|
|
return {
|
|
paths: [],
|
|
fallback: true
|
|
}
|
|
}
|
|
|
|
const from = 'slug-paths'
|
|
const { allPages } = await getGlobalData({ from })
|
|
|
|
return {
|
|
paths: allPages
|
|
?.filter(row => checkSlug(row))
|
|
.map(row => ({
|
|
params: {
|
|
prefix: row.slug.split('/')[0],
|
|
slug: row.slug.split('/')[1],
|
|
suffix: row.slug.split('/').slice(1)
|
|
}
|
|
})),
|
|
fallback: true
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 抓取页面数据
|
|
* @param {*} param0
|
|
* @returns
|
|
*/
|
|
export async function getStaticProps({
|
|
params: { prefix, slug, suffix },
|
|
locale
|
|
}) {
|
|
let fullSlug = prefix + '/' + slug + '/' + suffix.join('/')
|
|
const from = `slug-props-${fullSlug}`
|
|
const props = await getGlobalData({ from, locale })
|
|
if (siteConfig('PSEUDO_STATIC', BLOG.PSEUDO_STATIC, props.NOTION_CONFIG)) {
|
|
if (!fullSlug.endsWith('.html')) {
|
|
fullSlug += '.html'
|
|
}
|
|
}
|
|
|
|
// 在列表内查找文章
|
|
props.post = props?.allPages?.find(p => {
|
|
return (
|
|
p.type.indexOf('Menu') < 0 &&
|
|
(p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
|
)
|
|
})
|
|
|
|
// 处理非列表内文章的内信息
|
|
if (!props?.post) {
|
|
const pageId = fullSlug.slice(-1)[0]
|
|
if (pageId.length >= 32) {
|
|
const post = await getPost(pageId)
|
|
props.post = post
|
|
}
|
|
}
|
|
|
|
// 无法获取文章
|
|
if (!props?.post) {
|
|
props.post = null
|
|
return {
|
|
props,
|
|
revalidate: siteConfig(
|
|
'REVALIDATE_SECOND',
|
|
BLOG.NEXT_REVALIDATE_SECOND,
|
|
props.NOTION_CONFIG
|
|
)
|
|
}
|
|
}
|
|
|
|
// 文章内容加载
|
|
if (!props?.posts?.blockMap) {
|
|
props.post.blockMap = await getPostBlocks(props.post.id, from)
|
|
}
|
|
// 生成全文索引 && 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: siteConfig(
|
|
'NEXT_REVALIDATE_SECOND',
|
|
BLOG.NEXT_REVALIDATE_SECOND,
|
|
props.NOTION_CONFIG
|
|
)
|
|
}
|
|
}
|
|
|
|
function checkSlug(row) {
|
|
let slug = row.slug
|
|
if (slug.startsWith('/')) {
|
|
slug = slug.substring(1)
|
|
}
|
|
return (
|
|
(slug.match(/\//g) || []).length >= 2 &&
|
|
row.type.indexOf('Menu') < 0 &&
|
|
!checkContainHttp(slug)
|
|
)
|
|
}
|
|
|
|
export default PrefixSlug
|