Merge branch 'main' into feat/algolia

This commit is contained in:
tangly1024.com
2023-07-24 15:14:34 +08:00
200 changed files with 5879 additions and 1004 deletions

122
pages/[prefix]/[slug].js Normal file
View File

@@ -0,0 +1,122 @@
import BLOG from '@/blog.config'
import { getPostBlocks } from '@/lib/notion'
import { getGlobalData } from '@/lib/notion/getNotionData'
import { idToUuid } from 'notion-utils'
import { getNotion } from '@/lib/notion/getNotion'
import Slug from '.'
/**
* 根据notion的slug访问页面
* @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 })
return {
paths: allPages?.filter(row => row.slug.indexOf('/') > 0).map(row => ({ params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1] } })),
fallback: true
}
}
export async function getStaticProps({ params: { prefix, slug } }) {
let fullSlug = prefix + '/' + slug
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
if (!fullSlug.endsWith('.html')) {
fullSlug += '.html'
}
}
const from = `slug-props-${fullSlug}`
const props = await getGlobalData({ from })
// 在列表内查找文章
props.post = props?.allPages?.find((p) => {
return p.slug === fullSlug || p.id === idToUuid(fullSlug)
})
// 处理非列表内文章的内信息
if (!props?.post) {
const pageId = slug.slice(-1)[0]
if (pageId.length >= 32) {
const post = await getNotion(pageId)
props.post = post
}
}
// 无法获取文章
if (!props?.post) {
props.post = null
return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) }
}
// 文章内容加载
if (!props?.posts?.blockMap) {
props.post.blockMap = await getPostBlocks(props.post.id, from)
}
// 推荐关联文章处理
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, BLOG.POST_RECOMMEND_COUNT)
} else {
props.prev = null
props.next = null
props.recommendPosts = []
}
delete props.allPages
return {
props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND)
}
}
/**
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
* @param post
* @param {*} allPosts
* @param {*} count
* @returns
*/
function getRecommendPost(post, allPosts, count = 6) {
let recommendPosts = []
const postIds = []
const currentTags = post?.tags || []
for (let i = 0; i < allPosts.length; i++) {
const p = allPosts[i]
if (p.id === post.id || p.type.indexOf('Post') < 0) {
continue
}
for (let j = 0; j < currentTags.length; j++) {
const t = currentTags[j]
if (postIds.indexOf(p.id) > -1) {
continue
}
if (p.tags && p.tags.indexOf(t) > -1) {
recommendPosts.push(p)
postIds.push(p.id)
}
}
}
if (recommendPosts.length > count) {
recommendPosts = recommendPosts.slice(0, count)
}
return recommendPosts
}
export default PrefixSlug

View File

@@ -90,13 +90,14 @@ export async function getStaticPaths() {
const from = 'slug-paths'
const { allPages } = await getGlobalData({ from })
return {
paths: allPages?.map(row => ({ params: { slug: [row.slug] } })),
paths: allPages?.filter(row => row.slug.indexOf('/') < 0).map(row => ({ params: { prefix: row.slug } })),
fallback: true
}
}
export async function getStaticProps({ params: { slug } }) {
let fullSlug = slug.join('/')
export async function getStaticProps({ params: { prefix } }) {
// let fullSlug = slug.join('/')
let fullSlug = prefix
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
if (!fullSlug.endsWith('.html')) {
fullSlug += '.html'
@@ -111,7 +112,7 @@ export async function getStaticProps({ params: { slug } }) {
// 处理非列表内文章的内信息
if (!props?.post) {
const pageId = slug.slice(-1)[0]
const pageId = prefix.slice(-1)[0]
if (pageId.length >= 32) {
const post = await getNotion(pageId)
props.post = post

View File

@@ -1,6 +1,6 @@
import { useEffect } from 'react'
// import 'animate.css'
import '@/styles/animate.css' // @see https://animate.style/
import '@/styles/globals.css'
import '@/styles/nprogress.css'
import '@/styles/utility-patterns.css'
@@ -28,9 +28,9 @@ const MyApp = ({ Component, pageProps }) => {
return (
<GlobalContextProvider>
<Component {...pageProps}/>
<ExternalPlugins {...pageProps} />
<ExternalScript />
<Component {...pageProps} />
<ExternalPlugins {...pageProps} />
</GlobalContextProvider>
)
}

View File

@@ -13,11 +13,24 @@ class MyDocument extends Document {
return (
<Html lang={BLOG.LANG}>
<Head>
<link rel='icon' href='/favicon.ico' />
<CommonScript />
<link rel='icon' href= {`${BLOG.BLOG_FAVICON}`} />
<CommonScript />
{/* 预加载字体 */}
{BLOG.FONT_AWESOME && <>
<link rel='preload' href={BLOG.FONT_AWESOME} as="style" crossOrigin="anonymous" />
<link rel="stylesheet" href={BLOG.FONT_AWESOME} crossOrigin="anonymous" referrerPolicy="no-referrer" />
</>}
{BLOG.FONT_URL?.map((fontUrl, index) => {
if (fontUrl.endsWith('.css')) {
return <link key={index} rel="stylesheet" href={fontUrl} />
} else {
return <link key={index} rel="preload" href={fontUrl} as="font" type="font/woff2" />
}
})}
</Head>
<body className={`${BLOG.FONT_STYLE} font-light bg-day dark:bg-night`}>
<body className={`${BLOG.FONT_STYLE} font-light scroll-smooth`}>
<Main />
<NextScript />
</body>

View File

@@ -39,8 +39,9 @@ export const getServerSideProps = async (ctx) => {
}
]
const postFields = allPages?.filter(p => p.status === BLOG.NOTION_PROPERTY_NAME.status_publish)?.map(post => {
const slugWithoutLeadingSlash = post?.slug.startsWith('/') ? post?.slug?.slice(1) : post.slug
return {
loc: `${BLOG.LINK}/${post.slug}`,
loc: `${BLOG.LINK}/${slugWithoutLeadingSlash}`,
lastmod: new Date(post?.publishTime).toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'