-
Notifications
You must be signed in to change notification settings - Fork 1
/
[slug].tsx
174 lines (162 loc) · 4.82 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
import { PortableText, PortableTextReactComponents } from '@portabletext/react'
import { FunctionComponent } from 'react'
import { PageLayout } from '../../components/layout/PageLayout'
import client from '../../sanity/client'
import { Article } from '../../types/sanity'
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
import Link from 'next/link'
import { AnchoredHeading } from '../../components/shared/AnchoredHeading'
import { ArticleBodyImage } from './ArticleBodyImage'
interface BlogPostProps {
article: Article & {
imageUrl?: string
authorName?: string
}
}
const blogPostComponents = {
types: {
code: (props) => {
const { language, code } = props.value
return (
<SyntaxHighlighter theme="" language={language}>{code}</SyntaxHighlighter>
)
},
image: ({value}) => {
return (
<ArticleBodyImage
imgIndex={value.asset.index}
imgUrl={value.asset.url}
blurImg={value.asset.blurImg}
/>
)
},
},
marks: {
highlight: ({ children }) => <span className="highlighted-text">{children}</span>,
internalLink: ({ value, children }) => {
const linkText = children ? children[0] : `[internal_link]`
const {slug = {}} = value
return <Link href={`/blog/${slug.current}`}>{linkText}</Link>
},
link: ({ value, children }) => {
return (
<a href={value.url} target="_blank" rel="noreferrer">
{children}
</a>
)
},
},
block: {
h2: ({ value, children })=> {
return <AnchoredHeading
component={children}
label={value.children[0].text}
variant='h2'
/>
},
h3: ({ value, children })=> {
return <AnchoredHeading
component={children}
label={value.children[0].text}
variant='h3'
/>
},
h4: ({ value, children })=> {
return <AnchoredHeading
component={children}
label={value.children[0].text}
variant='h4'
/>
}
}
} as Partial<PortableTextReactComponents>
const BlogPost: FunctionComponent<BlogPostProps> = ({ article }) => {
return (
!!article && (
<PageLayout
pageTitle={article.title}
useTitleOverlay={false}
imgSrc={article.imageUrl}
metaDescription={article.summary && article.summary[0].children[0].text}
>
{!!article && (
<div>
<div className='article-metadata'>
<h1 data-cy="article-title">{article.title}</h1>
<div className='author-field'>By {article.authorName}</div>
<div className='publish-date' data-cy="article-publish-date">Published {article.publishDate}</div>
</div>
<br />
<div data-cy="article-body">
<PortableText
value={article.body}
components={blogPostComponents}
/>
</div>
</div>
)}
</PageLayout>
)
)
}
export async function getStaticPaths() {
const paths = await client.fetch(
`*[_type == "article" && defined(slug.current)][].slug.current`
)
return {
paths: paths.map((slug: string) => ({params: {slug}})),
fallback: true,
}
}
export async function getStaticProps(context: any) {
// It's important to default the slug so that it doesn't return "undefined"
const { slug = `` } = context.params
/**
* Because the generated slug is lower case, the query also needs to pass a slug that's all lower-case.
* Othwerise, no match will be found; in other words, this query syntax is case-sensitive
*/
const article = await client.fetch(`
*[_type == "article" && slug.current == $slug][0]{
title,
publishDate,
summary,
"imageUrl": image.asset->url,
"authorName": author->name,
body
}
`, { slug: slug.toLowerCase() })
let imgCount = 0
await Promise.all(
article.body.map(async (block, index) => {
if (block._type === `image`) {
imgCount++
const embeddedImage = await client.fetch(`*[_type == "sanity.imageAsset" && _id == $ref][0]{
metadata,
url
}`, { ref: block.asset._ref }) as {
// There are more fields available here, but we only need the lqip field
metadata: {
lqip: string // A 20x20, base64 encoding of the image, useful for placeholders
},
url: string // The URL to the original, full-resolution asset
}
article.body[index] = {
...block,
asset: {
index: imgCount,
url: embeddedImage.url,
blurImg: embeddedImage.metadata.lqip
}
}
} else {
article.body[index] = block
}
})
)
return {
props: {
article
}
}
}
export default BlogPost