-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
283 lines (261 loc) · 6.97 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const path = require('path')
const _ = require('lodash')
const axios = require('axios')
const { createFilePath } = require('gatsby-source-filesystem')
const { urlResolve, createContentDigest } = require('gatsby-core-utils')
const mdxResolverPassthrough = fieldName => async (
source,
args,
context,
info,
) => {
const type = info.schema.getType(`Mdx`)
const mdxNode = context.nodeModel.getNodeById({
id: source.parent,
})
const resolver = type.getFields()[fieldName].resolve
const result = await resolver(mdxNode, args, context, {
fieldName,
})
return result
}
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions
createTypes(`interface BlogPost @nodeInterface {
id: ID!
title: String!
body: String!
slug: String!
date: Date! @dateformat
tags: [String]!
keywords: [String]!
excerpt: String!
card: File @fileByRelativePath
published: Boolean!
}`)
createTypes(
schema.buildObjectType({
name: `MdxBlogPost`,
fields: {
id: { type: `ID!` },
title: {
type: `String!`,
},
slug: {
type: `String!`,
},
card: {
type: `File`,
},
published: {
type: `Boolean!`,
},
date: { type: `Date!`, extensions: { dateformat: {} } },
tags: { type: `[String]!` },
keywords: { type: `[String]!` },
excerpt: {
type: `String!`,
args: {
pruneLength: {
type: `Int`,
defaultValue: 140,
},
},
resolve: mdxResolverPassthrough(`excerpt`),
},
body: {
type: `String!`,
resolve: mdxResolverPassthrough(`body`),
},
},
interfaces: [`Node`, `BlogPost`],
}),
)
}
// Create fields for post slugs and source
// This will change with schema customization with work
exports.onCreateNode = async (
{ node, actions, getNode, createNodeId },
themeOptions,
) => {
const { createNode, createParentChildLink } = actions
// Make sure it's an MDX node
if (node.internal.type !== `Mdx`) {
return
}
// Create source field (according to contentPath)
const fileNode = getNode(node.parent)
const source = fileNode.sourceInstanceName
if (node.internal.type === `Mdx` && source === 'content') {
let slug
if (node.frontmatter.slug) {
if (path.isAbsolute(node.frontmatter.slug)) {
// absolute paths take precedence
slug = node.frontmatter.slug
} else {
// otherwise a relative slug gets turned into a sub path
slug = urlResolve('/', node.frontmatter.slug)
}
} else {
// otherwise use the filepath function from gatsby-source-filesystem
const filePath = createFilePath({
node: fileNode,
getNode,
basePath: 'content',
})
slug = urlResolve('/', filePath)
}
// normalize use of trailing slash
slug = slug.replace(/\/*$/, `/`)
const fieldData = {
title: node.frontmatter.title,
tags: node.frontmatter.tags || [],
slug,
date: node.frontmatter.date,
keywords: node.frontmatter.keywords || [],
card: node.frontmatter.card,
published: node.frontmatter.published,
}
const mdxBlogPostId = createNodeId(`${node.id} >>> MdxBlogPost`)
await createNode({
...fieldData,
// Required fields.
id: mdxBlogPostId,
parent: node.id,
children: [],
internal: {
type: `MdxBlogPost`,
contentDigest: createContentDigest(fieldData),
content: JSON.stringify(fieldData),
description: `Mdx implementation of the BlogPost interface`,
},
})
createParentChildLink({ parent: node, child: getNode(mdxBlogPostId) })
}
}
exports.createPages = async ({ actions, graphql, reporter }) => {
const { data } = await graphql(`
{
allBlogPost(sort: { fields: [date, title], order: DESC }, limit: 1000) {
edges {
node {
id
slug
}
}
}
allPodcast {
edges {
node {
slug
}
}
}
allLesson {
edges {
node {
slug
}
}
}
allCourse {
edges {
node {
slug
id
}
}
}
}
`)
if (data.errors) {
reporter.panic(data.errors)
}
const { allBlogPost } = data
const posts = allBlogPost.edges
posts.forEach(({ node: post }, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1]
const next = index === 0 ? null : posts[index - 1]
const { slug } = post
actions.createPage({
path: slug,
component: path.resolve(`./src/templates/post/post-query.js`),
context: {
id: post.id,
previousId: previous ? previous.node.id : undefined,
nextId: next ? next.node.id : undefined,
},
})
})
// Create the Posts page
actions.createPage({
path: '/archive',
component: path.resolve(`./src/templates/posts/posts-query.js`),
context: {},
})
data.allLesson.edges.forEach(({ node: lesson }) => {
actions.createPage({
path: `/lessons/${lesson.slug}`,
component: path.resolve(`./src/templates/lesson/lesson-query.js`),
context: {
slug: lesson.slug,
},
})
})
data.allPodcast.edges.forEach(({ node: podcast }) => {
actions.createPage({
path: `/podcasts/${podcast.slug}`,
component: path.resolve(`./src/templates/podcast/podcast-query.js`),
context: {
slug: podcast.slug,
},
})
})
data.allCourse.edges.forEach(({ node: course }) => {
actions.createPage({
path: `/courses/${course.slug}`,
component: path.resolve(`./src/templates/course/course-query.js`),
context: {
slug: course.slug,
},
})
})
}
exports.onCreateWebpackConfig = ({ actions, loaders }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
alias: {
'react-dom': '@hot-loader/react-dom',
$components: path.resolve(__dirname, 'src/components'),
},
},
})
}
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
const { createNode } = actions
const todoApp = await axios(`https://egghead.io/api/v1/playlists/349783`)
const convertingServerlessApp = await axios(
`https://egghead.io/api/v1/playlists/350751`,
)
const collections = [todoApp.data, convertingServerlessApp.data]
collections.forEach(collection =>
createNode(
Object.assign({}, collection, {
id: createNodeId(`collection-${collection.id}`),
parent: null,
children: [],
internal: {
type: `collection`,
mediaType: `application/json`,
content: JSON.stringify(collection),
contentDigest: createContentDigest(collection),
},
}),
),
)
}