Files
NotionNext/pages/[slug].js
2022-03-30 15:34:25 +08:00

96 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import BLOG from '@/blog.config'
import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import * as ThemeMap from '@/themes'
import { useEffect, useState } from 'react'
/**
* 根据notion的slug访问页面针对类型为Page的页面
* @param {*} props
* @returns
*/
const Slug = props => {
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
const { post } = props
if (!post) {
return <ThemeComponents.Layout404 {...props}/>
}
// 文章锁🔐
const [lock, setLock] = useState(true)
useEffect(() => {
if (post && post.password && post.password !== '') {
setLock(true)
} else {
setLock(false)
}
}, [post])
/**
* 验证文章密码
* @param {*} result
*/
const validPassword = result => {
if (result) {
setLock(false)
}
}
const { siteInfo } = props
const meta = {
title: `${post.title} | ${siteInfo.title}`,
description: post.summary,
type: 'article',
tags: post.tags
}
props = { ...props, meta, lock, setLock, validPassword }
return <ThemeComponents.LayoutSlug {...props} showArticleInfo={false}/>
}
export async function getStaticPaths () {
if (!BLOG.isProd) {
return {
paths: [],
fallback: true
}
}
const from = 'slug-paths'
const { allPosts } = await getGlobalNotionData({ from, pageType: ['Page'] })
const filterPosts = allPosts?.filter(e => e?.slug?.indexOf('http') !== 0) || []
return {
paths: filterPosts.map(row => ({ params: { slug: row.slug } })),
fallback: true
}
}
export async function getStaticProps ({ params: { slug } }) {
const from = `slug-props-${slug}`
const props = await getGlobalNotionData({ from, pageType: ['Page'] })
const { allPosts } = props
const post = allPosts.find(p => p.slug === slug)
if (!post) {
return { props: {}, revalidate: 1 }
}
try {
post.blockMap = await getPostBlocks(post.id, 'slug')
} catch (error) {
console.error('获取文章详情失败', error)
}
props.post = post
return {
props,
revalidate: 1
}
}
export default Slug