forked from kernel-community/kernel-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblogPost_layout.js
More file actions
236 lines (214 loc) · 5.92 KB
/
blogPost_layout.js
File metadata and controls
236 lines (214 loc) · 5.92 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/** @jsx jsx */
import isArray from 'lodash/isArray'
import { BlogAuthor, BlogCard, BlogContributors } from '@modules/blog'
import { useTranslation } from '@modules/localization'
import { Button, Divider } from '@modules/ui'
import { Heading } from '@modules/ui/heading'
import { SEO } from '@modules/utility'
import { graphql, useStaticQuery } from 'gatsby'
import { Box, Flex, jsx } from 'theme-ui'
///MDX Layout for POSTs
export default ({ children, pageContext }) => {
const {
title,
description,
keywords, //<- Seo
authors,
date,
image,
recommend,
} = pageContext.frontmatter
const { t } = useTranslation()
const { blogPosts, siteContent } = useStaticQuery(graphql`
query blogPostRecommendations {
#Regex for all blog posts
blogPosts: allMdx(
filter: { fileAbsolutePath: { regex: "//blogPosts/" } }
) {
edges {
node {
fileAbsolutePath
excerpt(truncate: true, pruneLength: 200)
frontmatter {
title
date(formatString: "MM/DD/YYYY")
description
authors
image
}
id
}
}
}
siteContent: allMdx(
filter: {
fileAbsolutePath: {
regex: "/content/([\\w]{2})/(?!header.mdx|index.mdx|sidenav.mdx|example.mdx|social.mdx|footer.mdx|404.mdx|.js|.json)/"
}
}
) {
edges {
node {
excerpt(truncate: true, pruneLength: 200)
headings(depth: h1) {
value
}
fileAbsolutePath
frontmatter {
title
description
order
}
}
}
}
}
`)
const hasContributors = isArray(authors) && authors.length > 1
const contributors = hasContributors ? authors.slice(1, authors.length) : []
const pagePathSplit = pageContext.pagePath
.split('/')
.splice(1, pageContext.pagePath.split('/').length - 2)
const typeIndex = pagePathSplit.indexOf('blog') + 1
//Split absolute path up to blog, get directory AFTER blog.
let postType =
typeIndex !== pagePathSplit.length - 1
? pagePathSplit[typeIndex]
: 'general'
let postImage = null
if (image === null || image === undefined) {
postImage = `/images/blog_headers/${postType}_1.png` //will be general image 1
} else {
if (isNaN(parseInt(image))) {
//Not a number, but a string. Expect entire src url
postImage = image
} else {
postImage = `/images/blog_headers/${postType}_${image}.png`
}
}
const otherPosts = recommend
?.map((rec) => {
//Run through recommendation map for blog post recommendations
return blogPosts.edges.filter(({ node }) =>
node.fileAbsolutePath.includes(rec)
)[0]
})
.concat(
recommend?.map((rec) => {
//Concat recommendations for all other site content.
return siteContent.edges.filter(({ node }) =>
node.fileAbsolutePath.includes(rec)
)[0]
})
)
.filter((el) => el !== undefined) //Filter out empty array/undefined runs of recommend.
const recommendations = otherPosts && otherPosts.length > 0
const seo = {
title,
description,
keywords,
featuredImage: postImage,
}
return (
<Flex sx={{ flexDirection: 'column' }}>
<ContentBlock>
<SEO {...seo} />
<Button outline icon="chevron_left" to="/blog">
{t('Back_To_Blog')}
</Button>
<Heading level={1}>{title}</Heading>
{authors ? (
<BlogAuthor
sx={{ mb: '16px' }}
authors={authors}
date={date}
isDefaultLocale={true}
/>
) : null}
<img
src={postImage}
sx={{
width: '100%',
objectFit: 'cover',
maxHeight: '478px',
mb: '48px',
}}
/>
<div
sx={{
'& > *:first-of-type': {
mt: 0,
},
}}>
{children}
</div>
</ContentBlock>
{hasContributors ? (
<Box sx={{ pl: [4, 4, '64px'], pr: [4, 4, 0], mt: 2, mb: 2 }}>
<Divider />
</Box>
) : null}
{hasContributors ? (
<ContentBlock>
<h2 sx={{ fontWeight: '500', fontSize: '32px' }}> Contributors </h2>
<p sx={{ mb: '40px' }}>
This article is possible with a little help from friends.
</p>
<BlogContributors contributors={contributors} />
</ContentBlock>
) : null}
{recommendations ? (
<Box sx={{ pl: [4, 4, '64px'], pr: [4, 4, 0], mt: 4, mb: 2 }}>
<Divider />
</Box>
) : null}
{recommendations ? (
<Box
sx={{
width: ['100%', '100%', '90%'],
m: '0 auto',
mt: [2, 4, 4],
mb: [2, 4, 4],
pl: [4, 4, 0],
pr: [4, 4, 0],
position: 'relative',
}}>
<h2 sx={{ mb: '66px', fontWeight: '500', fontSize: '32px' }}>
{' '}
Read More{' '}
</h2>
<Flex
sx={{
justifyContent: 'start',
width: '100%',
mb: [0, '80px', '80px'],
flexDirection: ['column', 'row', 'row'],
'& > *:not(:last-child)': {
mr: '96px',
},
}}>
{otherPosts.map(({ node }, index) => (
<BlogCard {...node} key={`blog-recommendation-${index}`} />
))}
</Flex>
</Box>
) : null}
</Flex>
)
}
function ContentBlock({ children }) {
return (
<Box
sx={{
width: ['100%', '100%', '80%'],
m: '0 auto',
mt: [2, 4, 4],
mb: [2, 4, 4],
pl: [4, 4, '64px'],
pr: [4, 4, 0],
position: 'relative',
}}>
{children}
</Box>
)
}