-
Notifications
You must be signed in to change notification settings - Fork 7
/
gatsby-node.js
223 lines (201 loc) · 6.02 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
/*
* Package Import
*/
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
/*
* Local Import
*/
/*
* Code
*/
// Templates
const Article = path.resolve('./src/templates/Article/index.js');
const Lesson = path.resolve('./src/templates/Initiation/Lesson/index.js');
const Quiz = path.resolve('./src/templates/Initiation/Quiz/index.js');
const Exercise = path.resolve('./src/templates/Initiation/Exercise/index.js');
// Parse date information out of blog post fileName.
const POST_FILENAME_REGEX = /([0-9]+)-([0-9]+)-([0-9]+)_(.+)\/([a-z]+)\.md$/;
/**
* Create a Webpack config.
* @api : https://www.gatsbyjs.org/docs/node-apis/#onCreateWebpackConfig
*/
exports.onCreateWebpackConfig = ({ actions, plugins }) => {
actions.setWebpackConfig({
module: {
// https://github.com/gatsbyjs/gatsby/issues/8583
rules: [
{
type: 'javascript/auto',
// include: /node_modules/,
test: /\.mjs$/,
use: [],
},
],
},
resolve: {
// déclenche l'affichage de la stackframe :
// mainFields: ['main', 'module'],
alias: {
src: path.resolve(__dirname, 'src'),
},
},
// optimization: {
// // Useful to debug the production build locally.
// minimize: false
// },
plugins: [
plugins.define({
// Beware not to use "process.env" as a namespace, gatsby prevents
// editing it. Let's use "config" instead.
config: {
GITHUB_TOKEN: JSON.stringify(process.env.DEVIENSDEV_GITHUB_TOKEN),
},
}),
],
});
};
/**
* Create Babel config
* @api : https://www.gatsbyjs.org/docs/node-apis/#onCreateBabelConfig
*/
exports.onCreateBabelConfig = ({ actions }, pluginOptions) => {
actions.setBabelPlugin({
name: `babel-plugin-emotion`,
options: {
sourceMap: process.env.NODE_ENV !== `production`,
...(pluginOptions || {}),
},
});
};
/**
* On create Node
* @api : https://www.gatsbyjs.org/docs/node-apis/#onCreateNode
*/
exports.onCreateNode = ({ node, actions, getNode }) => {
if (node.internal.type === `MarkdownRemark`) {
let slug = createFilePath({ node, getNode });
const { relativePath } = getNode(node.parent);
if (relativePath.includes('blog')) {
/*
* Blog posts don't have embedded permalinks.
* Their slugs follow a pattern: /blog/<year>/<month>/<day>/<slug>
*/
const match = POST_FILENAME_REGEX.exec(relativePath);
const year = match[1];
const month = match[2];
const day = match[3];
const fileName = match[4];
/*
* If the doc has a `slug` field, override the default slug
* Otherwise, get the default slug, which is define in fileName
*/
const slugName = node.frontmatter.slug ? node.frontmatter.slug : fileName;
// New slug
slug = `/blog/${year}/${month}/${day}/${slugName}`;
const date = new Date(year, month - 1, day);
// Blog posts are sorted by date and display the date in their header.
actions.createNodeField({
node,
name: 'date',
value: date.toJSON(),
});
}
// Used to generate `slug` to view this content.
actions.createNodeField({
name: `slug`,
node,
value: slug,
});
// Used to generate a GitHub edit link.
actions.createNodeField({
name: 'path',
node,
value: relativePath,
});
}
};
/**
* Create Pages
* This is called after the Gatsby bootstrap is finished, so you have access
* to any information necessary to programatically create pages.
*
* @api : https://www.gatsbyjs.org/docs/node-apis/#createPages
*/
exports.createPages = ({ actions, graphql }) =>
new Promise((resolve, reject) => {
// Query for all Markdowns
resolve(
graphql(`
{
allMarkdownRemark(sort: { fields: [fields___date], order: DESC }) {
edges {
node {
frontmatter {
title
}
fields {
slug
}
}
}
}
}
`).then(result => {
// If we have errors
if (result.errors) {
reject(result.errors);
}
result.data.allMarkdownRemark.edges.forEach(edge => {
// Create Blog posts pages
if (edge.node.fields.slug.includes('blog')) {
actions.createPage({
path: edge.node.fields.slug,
component: Article,
context: {
// Add optional context data.
// Data can be used as arguments to the page GraphQL query.
slug: edge.node.fields.slug,
},
});
}
// Create lesson pages
if (edge.node.fields.slug.includes('lesson')) {
actions.createPage({
path: edge.node.fields.slug,
component: Lesson,
context: {
// Add optional context data.
// Data can be used as arguments to the page GraphQL query.
slug: edge.node.fields.slug,
},
});
}
// Create quiz pages
if (edge.node.fields.slug.includes('quiz')) {
actions.createPage({
path: edge.node.fields.slug,
component: Quiz,
context: {
// Add optional context data.
// Data can be used as arguments to the page GraphQL query.
slug: edge.node.fields.slug,
},
});
}
// Create practice pages
if (edge.node.fields.slug.includes('exercise')) {
actions.createPage({
path: edge.node.fields.slug,
component: Exercise,
context: {
// Add optional context data.
// Data can be used as arguments to the page GraphQL query.
slug: edge.node.fields.slug,
},
});
}
});
}),
);
});