站点信息读取Notion数据

This commit is contained in:
tangly1024
2022-03-30 15:34:25 +08:00
parent 914e9a108f
commit 53c004843e
61 changed files with 322 additions and 463 deletions

View File

@@ -4,18 +4,34 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import * as ThemeMap from '@/themes'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
/**
* 根据notion的slug访问页面
* @param {*} props
* @returns
*/
const Slug = (props) => {
const Slug = props => {
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
const { post } = props
if (!post) {
return <ThemeComponents.Layout404 {...props}/>
const router = useRouter()
useEffect(() => {
setTimeout(() => {
if (window) {
const article = document.getElementById('container')
if (!article) {
router.push('/404').then(() => {
console.log('找不到页面', router.asPath)
})
}
}
}, 3000)
})
return <p>Redirecting...</p>
}
// 文章锁🔐
@@ -40,7 +56,15 @@ const Slug = (props) => {
props = { ...props, lock, setLock, validPassword }
return <ThemeComponents.LayoutSlug {...props} showArticleInfo={true}/>
const { siteInfo } = props
const meta = {
title: `${props.post.title} | ${siteInfo.title}`,
description: props.post.summary,
type: 'article',
tags: props.post.tags
}
return <ThemeComponents.LayoutSlug {...props} showArticleInfo={true} meta={meta}/>
}
export async function getStaticPaths () {
@@ -61,35 +85,20 @@ export async function getStaticPaths () {
export async function getStaticProps ({ params: { slug } }) {
const from = `slug-props-${slug}`
const { customNav, allPosts, categories, tags, postCount, latestPosts } =
await getGlobalNotionData({ from, pageType: ['Post'] })
const post = allPosts.find(p => p.slug === slug)
if (!post) {
return { props: {}, revalidate: 1 }
const props = await getGlobalNotionData({ from, pageType: ['Post'] })
const allPosts = props.allPosts
props.post = props.allPosts.find(p => p.slug === slug)
if (!props.post) {
return { props, revalidate: 1 }
}
props.post.blockMap = await getPostBlocks(props.post.id, 'slug')
post.blockMap = await getPostBlocks(post.id, 'slug')
const index = allPosts.indexOf(post)
const prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
const next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
const recommendPosts = getRecommendPost(post, allPosts, BLOG.POST_RECOMMEND_COUNT)
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, BLOG.POST_RECOMMEND_COUNT)
return {
props: {
post,
tags,
prev,
next,
recommendPosts,
categories,
postCount,
latestPosts,
customNav
},
props,
revalidate: 1
}
}