-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path[id].js
More file actions
134 lines (123 loc) · 3.72 KB
/
[id].js
File metadata and controls
134 lines (123 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
Avatar,
Box,
Button,
Container,
Divider,
Flex,
Heading,
Text,
VStack,
Wrap,
WrapItem,
} from '@chakra-ui/react'
import { ArrowBackIcon } from '@chakra-ui/icons'
import { MDXRemote } from 'next-mdx-remote'
import { serialize } from 'next-mdx-remote/serialize'
import rehypeSlug from 'rehype-slug'
import fs from 'fs'
import matter from 'gray-matter'
import path from 'path'
import { Giscus } from '@/components'
import { Layout } from '@/components/layout'
import { Link, mapping } from '@/components/mdx'
import { distanceToNow, formatDate } from '@/lib/date-formatting'
import { MDXElements } from '@/lib/mdx-elements'
import { getAllPostsIds, getPostData } from '@/lib/posts'
export default function Post({ source, frontmatter, postId }) {
const date = new Date(frontmatter.date)
return (
<Layout
title={`${frontmatter.title}`}
card={`/cards/${postId}.png`}
description={frontmatter.summary}
url={`/blog/${postId}`}
type='article'
imageWidth='2560'
imageHeight='1440'
publishedTime={date.toISOString()}
authors={frontmatter.authors}
>
<Box as={'section'}>
<Container maxW='container.lg' py={10}>
<Box spacing='3' alignItems='start'>
<VStack paddingTop='30px' spacing='2' alignItems='center'>
<Heading as={'h1'} textAlign={'center'} size='xl' my={4}>
{frontmatter.title}
</Heading>
<Text fontSize={'sm'} color={'gray.700'}>
{formatDate(date)} ({distanceToNow(date)})
</Text>
<Wrap spacing='20px'>
{frontmatter.authors.map((author) => {
return (
<WrapItem key={author.name}>
<Flex
as={Link}
href={`https://github.com/${author.github}`}
align={'center'}
my={2}
direction={'column'}
_hover={{
textDecoration: 'none',
}}
>
<Avatar
src={`https://github.com/${author.github}.png`}
name={author.name}
my={2}
size={'lg'}
/>
<Text fontWeight={600}>{author.name}</Text>
</Flex>
</WrapItem>
)
})}
</Wrap>
<Divider my={2} />
</VStack>
<br></br>
<MDXRemote
{...source}
components={{ ...mapping, ...MDXElements }}
/>
</Box>
<Button
my={8}
as={Link}
href={'/blog'}
variant={'outline'}
leftIcon={<ArrowBackIcon />}
colorScheme={'blue'}
>
Back to Blog
</Button>
<Divider my={8} />
<br />
<Giscus />
</Container>
</Box>
</Layout>
)
}
export async function getStaticPaths() {
const paths = getAllPostsIds()
return {
paths,
fallback: false,
}
}
export async function getStaticProps({ params }) {
const postData = getPostData(params.id)
const filePath = path.join(process.cwd(), 'src/posts', postData.file)
const source = fs.readFileSync(filePath, 'utf8')
const { content, data } = matter(source)
const mdxSource = await serialize(content, {
mdxOptions: {
remarkPlugins: [],
rehypePlugins: [rehypeSlug],
format: 'mdx',
},
})
return { props: { source: mdxSource, frontmatter: data, postId: params.id } }
}