-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMdxPost.tsx
194 lines (182 loc) · 5.48 KB
/
MdxPost.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
import { ProseContainer } from '@components/ProseContainer';
import { ShareContent } from '@components/ShareContent';
import { Tag, Tags } from '@components/Tags';
import { useLocalization } from '@hooks/useLocalization';
import { getTagUrl } from '@utils/url';
import { BlogJsonLd, NextSeo, NextSeoProps } from 'next-seo';
import Head from 'next/head';
import Link from 'next/link';
import { useRouter } from 'next/router';
import React, { useMemo } from 'react';
import siteData from 'site-data';
import 'twin.macro';
import { FeaturedImage, FeaturedImageProps } from './components/FeaturedImage';
import { Header } from './components/Header';
import { PreviewBanner } from './components/PreviewBanner/PreviewBanner';
import { PrismStyles } from './components/PrismStyles';
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote';
import { mdxComponents } from '@components/MdxComponents';
export const MdxPostTemplate: React.FC<MdxPostTemplateProps> = ({
content,
postContent,
featuredImage,
title,
subtitle,
description,
publishedAt,
tags,
series,
nextSeo,
preview,
}) => {
const { formatDate } = useLocalization();
const { asPath } = useRouter();
const seoInfo = useMemo(() => {
const date = new Date(publishedAt).toISOString();
return {
title: title,
/**
* This enforce parsing double quotes correctly.
* - no parsing: "the description will be "like this""
* - parsing: "the description will be \"like this\""
*/
description: `${description}`,
url: `${siteData.site.url}${asPath}`,
published: date,
modified: date,
image: {
url: featuredImage?.src ?? siteData.site.seo_image.url,
width: featuredImage?.width ?? siteData.site.seo_image.width,
height: featuredImage?.height ?? siteData.site.seo_image.height,
},
};
}, [
title,
description,
featuredImage?.src,
publishedAt,
asPath,
siteData.site.url,
]);
return (
<>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;600&display=swap"
rel="stylesheet"
/>
</Head>
<BlogJsonLd
url={seoInfo.url}
title={seoInfo.title}
description={seoInfo.description}
images={[seoInfo.image.url]}
datePublished={seoInfo.published}
dateModified={seoInfo.modified}
authorName={[siteData.personalInformation.full_name]}
/>
<NextSeo
title={seoInfo.title}
description={seoInfo.description}
openGraph={{
title: seoInfo.title,
description: seoInfo.description,
type: 'article',
url: seoInfo.url,
article: {
publishedTime: seoInfo.published,
modifiedTime: seoInfo.modified,
tags: tags?.map((tag) => tag.name),
},
images: [
{
url: seoInfo.image.url,
width: seoInfo.image.width,
height: seoInfo.image.height,
alt: 'Hero Image',
},
],
}}
{...nextSeo}
/>
<PrismStyles />
{preview ? <PreviewBanner /> : null}
{featuredImage ? (
<FeaturedImage
src={featuredImage.src}
unsplash={featuredImage.unsplash}
/>
) : null}
<section tw="w-full col-span-full lg:col-start-2 lg:col-end-12">
<Header
title={title}
subtitle={subtitle}
publishedDate={formatDate(new Date(publishedAt), {
year: 'numeric',
month: 'short',
day: '2-digit',
})}
readingTime={
postContent ? getEstimatedReadingTime(postContent) : undefined
}
/>
{series?.top}
<ProseContainer tw="mt-8">
<MDXRemote {...content} components={mdxComponents} />
</ProseContainer>
{series?.bottom}
<hr tw="mt-10 mb-6" />
<footer tw="flex justify-between flex-wrap">
{tags ? (
<div tw="mb-4 mr-4">
<span tw="font-extrabold text-md md:text-lg lg:text-xl block mb-4 md:mb-6">
Tags
</span>
<Tags>
{tags.map((tag) => (
<Tag key={tag.id} tw="text-base lg:text-lg">
<Link href={getTagUrl(tag.slug)} passHref>
<a tw="underline text-secondary">#{tag.name}</a>
</Link>
</Tag>
))}
</Tags>
</div>
) : null}
<ShareContent
twitter={{ text: description }}
linkedIn={{ title, summary: description }}
/>
</footer>
</section>
</>
);
};
interface MdxPostTemplateProps {
/**
* parsed content via MDX remote
*/
content: MDXRemoteSerializeResult;
/**
* markdown content used to generate estimated reading time.
*/
postContent?: string;
title: string;
description: string;
publishedAt: string;
preview?: boolean;
tags?: { id: string; slug: string; name: string }[];
featuredImage?: FeaturedImageProps & { width: number; height: number };
subtitle?: string;
series?: {
top: JSX.Element | null;
bottom: JSX.Element | null;
};
nextSeo?: NextSeoProps;
}
function getEstimatedReadingTime(text: string): number {
const AVERAGE_WORD_READING = 200;
const numberOfWords = text.split(' ').length;
const wpm = numberOfWords / AVERAGE_WORD_READING;
return Math.round(wpm);
}