2022-07-17 21:40:41 +08:00
|
|
|
import fs from 'fs'
|
|
|
|
import PageTitle from '@/components/PageTitle'
|
|
|
|
import generateRss from '@/lib/generate-rss'
|
|
|
|
import { MDXLayoutRenderer } from '@/components/MDXComponents'
|
|
|
|
import { formatSlug, getAllFilesFrontMatter, getFileBySlug, getFiles } from '@/lib/mdx'
|
2022-10-07 13:55:39 +08:00
|
|
|
import { GetStaticProps, InferGetStaticPropsType } from 'next'
|
|
|
|
import { AuthorFrontMatter } from 'types/AuthorFrontMatter'
|
|
|
|
import { PostFrontMatter } from 'types/PostFrontMatter'
|
|
|
|
import { Toc } from 'types/Toc'
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
const DEFAULT_LAYOUT = 'PostLayout'
|
|
|
|
|
|
|
|
export async function getStaticPaths() {
|
|
|
|
const posts = getFiles('blog')
|
|
|
|
return {
|
|
|
|
paths: posts.map((p) => ({
|
|
|
|
params: {
|
|
|
|
slug: formatSlug(p).split('/'),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
fallback: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
// @ts-ignore
|
|
|
|
export const getStaticProps: GetStaticProps<{
|
|
|
|
post: { mdxSource: string; toc: Toc; frontMatter: PostFrontMatter }
|
|
|
|
authorDetails: AuthorFrontMatter[]
|
|
|
|
prev?: { slug: string; title: string }
|
|
|
|
next?: { slug: string; title: string }
|
|
|
|
}> = async ({ params }) => {
|
|
|
|
const slug = (params.slug as string[]).join('/')
|
2022-07-17 21:40:41 +08:00
|
|
|
const allPosts = await getAllFilesFrontMatter('blog')
|
2022-10-07 13:55:39 +08:00
|
|
|
const postIndex = allPosts.findIndex((post) => formatSlug(post.slug) === slug)
|
|
|
|
const prev: { slug: string; title: string } = allPosts[postIndex + 1] || null
|
|
|
|
const next: { slug: string; title: string } = allPosts[postIndex - 1] || null
|
|
|
|
const post = await getFileBySlug<PostFrontMatter>('blog', slug)
|
|
|
|
// @ts-ignore
|
2022-07-17 21:40:41 +08:00
|
|
|
const authorList = post.frontMatter.authors || ['default']
|
|
|
|
const authorPromise = authorList.map(async (author) => {
|
2022-10-07 13:55:39 +08:00
|
|
|
const authorResults = await getFileBySlug<AuthorFrontMatter>('authors', [author])
|
2022-07-17 21:40:41 +08:00
|
|
|
return authorResults.frontMatter
|
|
|
|
})
|
|
|
|
const authorDetails = await Promise.all(authorPromise)
|
|
|
|
|
|
|
|
// rss
|
|
|
|
if (allPosts.length > 0) {
|
|
|
|
const rss = generateRss(allPosts)
|
|
|
|
fs.writeFileSync('./public/feed.xml', rss)
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
post,
|
|
|
|
authorDetails,
|
|
|
|
prev,
|
|
|
|
next,
|
|
|
|
},
|
|
|
|
}
|
2022-07-17 21:40:41 +08:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
export default function Blog({
|
|
|
|
post,
|
|
|
|
authorDetails,
|
|
|
|
prev,
|
|
|
|
next,
|
|
|
|
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
2022-07-17 21:40:41 +08:00
|
|
|
const { mdxSource, toc, frontMatter } = post
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-10-07 13:55:39 +08:00
|
|
|
{'draft' in frontMatter && frontMatter.draft !== true ? (
|
2022-07-17 21:40:41 +08:00
|
|
|
<MDXLayoutRenderer
|
|
|
|
layout={frontMatter.layout || DEFAULT_LAYOUT}
|
|
|
|
toc={toc}
|
|
|
|
mdxSource={mdxSource}
|
|
|
|
frontMatter={frontMatter}
|
|
|
|
authorDetails={authorDetails}
|
|
|
|
prev={prev}
|
|
|
|
next={next}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="mt-24 text-center">
|
|
|
|
<PageTitle>
|
|
|
|
Under Construction{' '}
|
|
|
|
<span role="img" aria-label="roadwork sign">
|
|
|
|
🚧
|
|
|
|
</span>
|
|
|
|
</PageTitle>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|