2021-05-11 15:05:33 +01:00
|
|
|
import fs from 'fs'
|
2021-01-16 18:36:25 +08:00
|
|
|
import PageTitle from '@/components/PageTitle'
|
2021-01-09 17:50:45 +08:00
|
|
|
import generateRss from '@/lib/generate-rss'
|
2021-05-26 00:11:20 +08:00
|
|
|
import { MDXLayoutRenderer } from '@/components/MDXComponents'
|
2021-05-08 13:58:57 +08:00
|
|
|
import { formatSlug, getAllFilesFrontMatter, getFileBySlug, getFiles } from '@/lib/mdx'
|
2021-01-09 17:50:45 +08:00
|
|
|
|
2021-05-26 20:11:55 +08:00
|
|
|
const DEFAULT_LAYOUT = 'PostLayout'
|
|
|
|
|
2021-01-09 17:50:45 +08:00
|
|
|
export async function getStaticPaths() {
|
2021-05-08 13:58:57 +08:00
|
|
|
const posts = getFiles('blog')
|
2021-01-09 17:50:45 +08:00
|
|
|
return {
|
|
|
|
paths: posts.map((p) => ({
|
|
|
|
params: {
|
2021-05-08 13:58:57 +08:00
|
|
|
slug: formatSlug(p).split('/'),
|
2021-01-09 17:50:45 +08:00
|
|
|
},
|
|
|
|
})),
|
|
|
|
fallback: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getStaticProps({ params }) {
|
|
|
|
const allPosts = await getAllFilesFrontMatter('blog')
|
2021-05-08 13:58:57 +08:00
|
|
|
const postIndex = allPosts.findIndex((post) => formatSlug(post.slug) === params.slug.join('/'))
|
2021-01-09 17:50:45 +08:00
|
|
|
const prev = allPosts[postIndex + 1] || null
|
|
|
|
const next = allPosts[postIndex - 1] || null
|
2021-05-26 19:20:24 +08:00
|
|
|
const post = await getFileBySlug('blog', params.slug.join('/'))
|
2021-05-18 23:46:30 +08:00
|
|
|
const authorList = post.frontMatter.authors || ['default']
|
|
|
|
const authorPromise = authorList.map(async (author) => {
|
|
|
|
const authorResults = await getFileBySlug('authors', [author])
|
|
|
|
return authorResults.frontMatter
|
|
|
|
})
|
|
|
|
const authorDetails = await Promise.all(authorPromise)
|
2021-01-09 17:50:45 +08:00
|
|
|
|
|
|
|
// rss
|
|
|
|
const rss = generateRss(allPosts)
|
2021-07-11 23:03:27 +08:00
|
|
|
fs.writeFileSync('./public/feed.xml', rss)
|
2021-01-09 17:50:45 +08:00
|
|
|
|
2021-05-16 15:56:39 +08:00
|
|
|
return { props: { post, authorDetails, prev, next } }
|
2021-01-09 17:50:45 +08:00
|
|
|
}
|
|
|
|
|
2021-05-16 15:56:39 +08:00
|
|
|
export default function Blog({ post, authorDetails, prev, next }) {
|
2021-01-09 17:50:45 +08:00
|
|
|
const { mdxSource, frontMatter } = post
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-01-16 18:36:25 +08:00
|
|
|
{frontMatter.draft !== true ? (
|
2021-05-26 00:11:20 +08:00
|
|
|
<MDXLayoutRenderer
|
2021-05-26 20:11:55 +08:00
|
|
|
layout={frontMatter.layout || DEFAULT_LAYOUT}
|
2021-05-26 00:11:20 +08:00
|
|
|
mdxSource={mdxSource}
|
|
|
|
frontMatter={frontMatter}
|
|
|
|
authorDetails={authorDetails}
|
|
|
|
prev={prev}
|
|
|
|
next={next}
|
|
|
|
/>
|
2021-01-16 18:36:25 +08:00
|
|
|
) : (
|
|
|
|
<div className="mt-24 text-center">
|
|
|
|
<PageTitle>
|
|
|
|
Under Construction{' '}
|
|
|
|
<span role="img" aria-label="roadwork sign">
|
|
|
|
🚧
|
|
|
|
</span>
|
|
|
|
</PageTitle>
|
|
|
|
</div>
|
2021-01-09 17:50:45 +08:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|