1
1
mirror of https://github.com/theoludwig/theoludwig.git synced 2024-12-08 00:44:30 +01:00
.profile/pages/blog/[slug].tsx

109 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-09-04 20:40:58 +02:00
import type { GetStaticProps, GetStaticPaths, NextPage } from 'next'
2021-11-08 15:10:26 +01:00
import { MDXRemote } from 'next-mdx-remote'
import date from 'date-and-time'
import Giscus from '@giscus/react'
import { useTheme } from 'next-themes'
2021-11-08 15:10:26 +01:00
import { Head } from 'components/Head'
import { Header } from 'components/Header'
2022-09-04 20:40:58 +02:00
import type { FooterProps } from 'components/Footer'
import { Footer } from 'components/Footer'
2021-11-08 15:10:26 +01:00
import type { Post } from 'utils/blog'
interface BlogPostPageProps extends FooterProps {
post: Post
}
2021-12-04 15:52:51 +01:00
const BlogPostPage: NextPage<BlogPostPageProps> = (props) => {
2021-11-08 15:10:26 +01:00
const { version, post } = props
const { theme = 'dark' } = useTheme()
2021-11-08 15:10:26 +01:00
return (
<>
<Head
title={`${post.frontmatter.title} | Divlo`}
description={post.frontmatter.description}
/>
<Header />
2021-12-04 15:52:51 +01:00
<main className='flex flex-1 flex-col flex-wrap items-center'>
<div className='my-10 flex flex-col items-center'>
2021-11-08 15:10:26 +01:00
<h1 className='text-3xl font-semibold'>{post.frontmatter.title}</h1>
<p className='mt-2' data-cy='blog-post-date'>
{date.format(new Date(post.frontmatter.publishedOn), 'DD/MM/YYYY')}
</p>
</div>
<div className='prose mb-10 px-8'>
<MDXRemote
{...post.source}
components={{
2022-08-27 02:30:55 +02:00
img: (properties) => {
const { src, alt, ...props } = properties
let source = src
2022-10-20 22:24:01 +02:00
source = src?.replace('../public/', '/')
2022-08-27 02:30:55 +02:00
return <img src={source} alt={alt} {...props} />
},
2022-10-20 22:24:01 +02:00
a: (props) => {
2021-11-09 15:14:31 +01:00
if (props.href?.startsWith('#') ?? false) {
return <a {...props} />
}
return (
<a target='_blank' rel='noopener noreferrer' {...props} />
)
}
2021-11-08 15:10:26 +01:00
}}
/>
<Giscus
id='comments'
repo='Divlo/Divlo'
repoId='MDEwOlJlcG9zaXRvcnkzNTg5NDg1NDQ='
category='General'
categoryId='DIC_kwDOFWUewM4CQ_WK'
mapping='pathname'
reactionsEnabled='1'
emitMetadata='0'
inputPosition='top'
theme={theme}
lang='en'
loading='lazy'
/>
2021-11-08 15:10:26 +01:00
</div>
</main>
<Footer version={version} />
</>
)
}
export const getStaticProps: GetStaticProps<BlogPostPageProps> = async (
context
) => {
const slug = context?.params?.slug
const { getPostBySlug } = await import('utils/blog')
const post = await getPostBySlug(slug)
if (post == null || (post != null && !post.frontmatter.isPublished)) {
return {
redirect: {
destination: '/404',
permanent: false
}
}
}
const { readPackage } = await import('read-pkg')
const { version } = await readPackage()
return { props: { version, post } }
}
export const getStaticPaths: GetStaticPaths = async () => {
const { getPosts } = await import('utils/blog')
const posts = await getPosts()
return {
paths: posts.map((post) => {
return { params: { slug: post.slug } }
}),
fallback: false
}
}
export default BlogPostPage