/
[slug].tsx
199 lines (173 loc) · 4.4 KB
/
[slug].tsx
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { MDXProvider } from '@mdx-js/react';
import type { MDXComponents } from 'mdx/types';
import type { MDXRemoteSerializeResult } from 'next-mdx-remote';
import { MDXRemote } from 'next-mdx-remote';
import { serialize } from 'next-mdx-remote/serialize';
import rehypePrism from 'rehype-prism-plus';
import styled from 'styled-components';
import { FlexCenter } from '@/components/Container';
import CustomComponents from '@/components/CustomComponents';
import Emoji from '@/components/Emoji';
import Figure from '@/components/Figure';
import Footer from '@/components/Footer';
import Header from '@/components/Header';
import PostLayout from '@/components/Post';
import SEO from '@/components/SEO';
import TableOfContents from '@/components/TableOfContents';
import Video from '@/components/Video';
import { color, font, fontSize } from '@/styles/helpers';
import { getBg } from '@/utils/helperts';
import {
getBasicPostInfo,
getPostBySlug,
postFilePaths,
} from '@/utils/mdxUtils';
import type { PostBasicInfo, PostMetaType } from '@/utils/types';
import FeaturedPosts from '@/components/FeaturedPosts';
interface PostPageProps {
source: MDXRemoteSerializeResult<Record<string, unknown>>;
meta: PostMetaType;
slug: string;
relatedPosts: PostBasicInfo[];
}
const PostPage: React.FC<PostPageProps> = ({
source,
meta,
slug,
relatedPosts,
}) => {
const pathname = `/blog/${slug}`;
const { toc } = meta;
const components = {
...CustomComponents,
Figure,
Emoji,
Video,
FlexCenter,
} as MDXComponents;
const sidebar = toc ? (
<Sidebar>
<TableOfContents />
</Sidebar>
) : null;
return (
<>
<SEO
title={meta.title}
pathname={pathname}
description={meta.description}
seoTitle={meta.seoTitle}
canonical={meta.canonical}
pageImage={meta.pageImage}
/>
<Header pathname={pathname} bg={getBg(pathname)} />
<Wrapper>
<PostHeader>
<Title>{meta.title}</Title>
<Date>
<time>{meta.createdAt}</time>
</Date>
{meta.excerpt ? <Excerpt>{meta.excerpt}</Excerpt> : null}
</PostHeader>
<PostContainer>
{sidebar}
<PostLayout>
<MDXProvider components={components}>
<MDXRemote {...source} />
</MDXProvider>
</PostLayout>
</PostContainer>
{relatedPosts.length > 0 && (
<FeaturedPosts posts={relatedPosts} title="Related Posts" />
)}
</Wrapper>
<Footer />
</>
);
};
export default PostPage;
const Wrapper = styled.main`
--maxW: 1100px;
--spacingY: 96px;
padding: 32px 32px var(--spacingY) 32px;
`;
const PostContainer = styled.div`
max-width: var(--maxW);
display: flex;
flex-direction: row-reverse;
margin: 0 auto;
`;
const PostHeader = styled.header`
text-align: center;
width: 100%;
max-width: var(--maxW);
margin: 0 auto 4rem auto;
& > * + * {
margin-top: var(--space-flow, 1.6em);
}
`;
const Title = styled.h1`
font-size: ${fontSize('xxxl')};
`;
const Date = styled.p`
font-size: ${fontSize('sm')};
color: ${color('text.light')};
`;
const Excerpt = styled.p`
font-size: ${fontSize('lg')};
font-family: ${font('mono')};
text-align: left;
max-width: 65ch;
margin-inline: auto;
`;
const Sidebar = styled.aside`
display: none;
flex: 0 100000 250px;
margin-left: auto;
position: sticky;
top: 148px;
height: calc(100vh - 148px);
overflow: auto;
@media (min-width: 1084px) {
display: block;
}
`;
// ===========
export const getStaticPaths = () => {
const paths = postFilePaths
.map((path) => path.replace(/\.mdx?$/, ''))
.map((slug) => ({ params: { slug } }));
return {
paths,
fallback: false,
};
};
export const getStaticProps = async ({
params,
}: {
params: { slug: string };
}) => {
const { content, data } = getPostBySlug(params.slug);
const mdxSource = await serialize(content, {
mdxOptions: {
remarkPlugins: [],
rehypePlugins: [rehypePrism],
},
scope: data as any,
});
const relatedPosts: PostBasicInfo[] = [];
if (data.related && data.related.length > 0) {
data.related.forEach((slug) => {
const post = getPostBySlug(slug);
relatedPosts.push({ ...getBasicPostInfo(post), slug });
});
}
return {
props: {
source: mdxSource,
meta: data,
slug: params.slug,
relatedPosts,
},
};
};