-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
154 lines (145 loc) · 4.88 KB
/
gatsby-node.js
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
const path = require("path")
const createPaginatedPages = require("gatsby-paginate")
module.exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const blogIndexTemplate = path.resolve("./src/templates/blog-index.js")
const blogTemplate = path.resolve("./src/templates/blog.js")
const redirectTemplate = path.resolve("./src/templates/redirect.js")
const res = await graphql(`
query {
allContentfulBlogPost(
sort: { fields: publishedAt, order: DESC }
filter: { hideFromList: { ne: true } }
) {
edges {
node {
title
hideFromList
isFeatured
slug
excerpt {
childMarkdownRemark {
rawMarkdownBody
}
}
md {
childMarkdownRemark {
timeToRead
}
}
categories
publishedAt
coverImage {
title
fluid {
sizes
src
srcSet
}
}
}
}
}
allContentfulCodepenPost(
sort: { fields: publishedAt, order: DESC }
) {
edges {
node {
url
title
slug
categories
publishedAt
description {
description
}
coverImage {
title
fluid {
sizes
src
srcSet
}
fixed(width: 1200) {
src
}
}
}
}
}
}
`)
// Manipulate blog posts
res.data.allContentfulBlogPost.edges.forEach(edge => {
edge.node.postType = "blog"
})
// Manipulate codepen posts
res.data.allContentfulCodepenPost.edges.forEach(edge => {
edge.node.postType = "codepen"
edge.node.categories.unshift("codepen")
})
const published = [
...res.data.allContentfulBlogPost.edges,
...res.data.allContentfulCodepenPost.edges,
].sort((a, b) => {
return new Date(b.node.publishedAt) - new Date(a.node.publishedAt)
})
createPaginatedPages({
edges: published,
createPage: createPage,
pageTemplate: blogIndexTemplate,
pageLength: 6,
pathPrefix: "/blog",
context: {},
})
// Create pages for blog posts
res.data.allContentfulBlogPost.edges.forEach(({ node }) => {
createPage({
component: blogTemplate,
path: `/blog/${node.slug}`,
context: {
slug: node.slug,
},
})
})
// Create pages for blog posts
res.data.allContentfulCodepenPost.edges.forEach(({ node }) => {
createPage({
component: redirectTemplate,
path: `/codepen/${node.slug}`,
context: {
url: node.url,
text: "You are being redirected to Codepen...",
coverImageUrl: node.coverImage.fixed.src,
socialCardMeta: [
{ name: "twitter:card", content: "summary_large_image" },
{ name: "twitter:site", content: "@themarcba" },
{
name: "twitter:title",
content: node.title,
},
{
name: "og:title",
content: node.title,
},
{
name: "twitter:description",
content: node.description.description,
},
{
name: "og:description",
content: node.description.description,
},
{
name: "twitter:image",
content: node.coverImage.fixed.src,
},
{
name: "og:image",
content: node.coverImage.fixed.src,
},
],
},
})
})
}