-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
294 lines (274 loc) · 7.48 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
284
285
286
287
288
289
290
291
292
293
294
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// You can delete this file if you're not using it
const path = require('path');
// https://github.com/danielmahon/gatsby-remark-relative-images/blob/master/README.md
// const {
// fmImagesToRelative,
// } = require('gatsby-remark-relative-images');
const {
createRemoteFileNode,
} = require('gatsby-source-filesystem');
// makes it so imports can be absolute relative to src along with node_modules
// https://www.gatsbycentral.com/enable-absolute-imports-for-gatsby-v2
exports.onCreateWebpackConfig = ({ stage, actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [
path.resolve(__dirname, 'src'),
'node_modules',
],
},
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type MarkdownRemark implements Node {
frontmatter: Frontmatter
authorAvatar: File @link(from: "authorAvatar___NODE")
}
type Frontmatter {
authorAvatar: String
}
`);
};
exports.onCreateNode = async ({
node,
actions,
store,
cache,
createNodeId,
}) => {
const { createNode, createNodeField } = actions;
const { internal, fileAbsolutePath: file_path } = node;
// add custom fields
if (internal.type === 'MarkdownRemark') {
const filename = path.basename(file_path, '.md');
const path_parts = path
.normalize(file_path) // normalize the path to platform
.split(path.sep); // split based on plaform
// if (courseIndex !== -1) {
if (path_parts.includes('courses')) {
const courseIndex = path_parts.indexOf('courses');
let course;
// markdown nested in a folder
if (filename === path_parts[path_parts.length - 2]) {
course = path_parts.slice(courseIndex + 1, -2);
} else {
course = path_parts.slice(courseIndex + 1, -1);
}
if (filename === 'index') {
createNodeField({
node,
name: 'slug',
value: `${course}`,
});
createNodeField({
node,
name: 'pageType',
value: `course`,
});
} else {
createNodeField({
node,
name: 'slug',
value: `${course}/${filename}`,
});
createNodeField({
node,
name: 'pageType',
value: `lesson`,
});
}
}
// const projectIndex = path_parts.indexOf('projects');
// if (projectIndex !== -1) {
if (path_parts.includes('projects')) {
createNodeField({
node,
name: 'slug',
value: `${filename}`,
});
createNodeField({
node,
name: 'pageType',
value: `project`,
});
}
// const instructorIndex = path_parts.indexOf(
// 'instructors',
// );
// if (instructorIndex !== -1) {
if (path_parts.includes('people')) {
createNodeField({
node,
name: 'slug',
value: `${filename}`,
});
createNodeField({
node,
name: 'pageType',
value: 'person',
});
}
}
// https://github.com/danielmahon/gatsby-remark-relative-images/blob/master/README.md
// fmImagesToRelative(node);
// https://www.gatsbyjs.org/docs/preprocessing-external-images/
// For all MarkdownRemark nodes that have a featured image url, call createRemoteFileNode
if (
node.internal.type === 'MarkdownRemark' &&
typeof node.frontmatter.authorAvatar === 'string' &&
node.frontmatter.authorAvatar.length > 0
) {
let fileNode = await createRemoteFileNode({
url: node.frontmatter.authorAvatar, // string that points to the URL of the image
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
createNode, // helper function in gatsby-node to generate the node
createNodeId, // helper function in gatsby-node to generate the node id
cache, // Gatsby's cache
store, // Gatsby's redux store
});
// if the file was created, attach the new node to the parent node
if (fileNode) {
node.authorAvatar___NODE = fileNode.id;
}
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const markdownLesson = path.resolve(
'src/createPages/Lesson/Lesson.js',
);
const markdownCourse = path.resolve(
'src/createPages/Course/Course.js',
);
// https://github.com/gatsbyjs/gatsby/issues/9882#issuecomment-462089503
const courses = await graphql(`
{
allMarkdownRemark(
filter: { fields: { pageType: { eq: "course" } } }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
title
published
thumbnail {
childImageSharp {
fixed(width: 239, height: 135) {
base64
width
height
src
srcSet
}
}
}
}
}
}
}
}
`);
if (courses.errors) throw courses.errors;
for (const edge of courses.data.allMarkdownRemark.edges) {
const courseSlug = edge.node.fields.slug;
const published = edge.node.frontmatter.published;
if (published !== false) {
const lessons = await graphql(`
{
allMarkdownRemark(
filter: {
fields: {
slug: { glob: "${courseSlug}/*" }
pageType: { eq: "lesson" }
}
frontmatter: {
published: {
ne: false
}
}
}
limit: 1000
sort: {
fields: frontmatter___index
order: ASC
}
) {
edges {
node {
frontmatter {
title
}
fields {
slug
}
}
}
}
}
`);
if (lessons.errors) throw lessons.errors;
// nav
const nav = {
course: {
title: edge.node.frontmatter.title,
courseSlug: courseSlug,
thumbnail: {
childImageSharp: {
fixed:
edge.node.frontmatter.thumbnail
.childImageSharp.fixed,
},
},
},
links: lessons.data.allMarkdownRemark.edges.map(
(edge) => ({
title: edge.node.frontmatter.title,
slug: edge.node.fields.slug,
}),
),
};
// create course page
createPage({
path: `/courses/${courseSlug}`,
component: markdownCourse,
context: {
layout: 'Application',
courseSlug: courseSlug,
nav,
},
});
// create lesson page
const edges = lessons.data.allMarkdownRemark.edges;
edges.forEach((edge, index) => {
createPage({
path: `/courses/${edge.node.fields.slug}`,
component: markdownLesson,
context: {
layout: 'Application',
lessonSlug: edge.node.fields.slug,
index: index,
previousSlug:
edges[index - 1] &&
edges[index - 1].node.fields.slug,
nextSlug:
edges[index + 1] &&
edges[index + 1].node.fields.slug,
courseSlug: courseSlug,
nav,
},
});
});
}
}
};