forked from wesbos/wesbos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
187 lines (173 loc) · 4.43 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
require('dotenv').config();
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
// This surfaces backend env variables to the front end by prefixing them with GATSBY_
process.env.GATSBY_DEPLOY_PRIME_URL = process.env.DEPLOY_PRIME_URL;
process.env.GATSBY_URL = process.env.URL;
process.env.GATSBY_COMMIT_REF = process.env.COMMIT_REF;
process.env.GATSBY_REPOSITORY_URL = process.env.REPOSITORY_URL;
async function makePostsFromMdx({ graphql, actions }) {
const blogPost = path.resolve('./src/templates/post.js');
const { errors, data } = await graphql(
`
{
allMdx(
filter: { fields: { collection: { eq: "post" } } }
sort: { fields: [frontmatter___date], order: DESC }
) {
edges {
node {
body
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`
);
if (errors) {
throw new Error('There was an error');
}
const posts = data.allMdx.edges;
posts.forEach((post, i) => {
const prev = posts[i - 1];
const next = posts[i + 1];
actions.createPage({
path: post.node.fields.slug,
component: blogPost,
context: {
slug: post.node.fields.slug,
collection: 'post',
prev,
next,
pathPrefix: '',
},
});
});
}
async function makeTipsFromMdx({ graphql, actions }) {
const tipTemplate = path.resolve('./src/templates/tip.js');
const { errors, data } = await graphql(
`
{
allMdx(
filter: { fields: { collection: { eq: "tip" } } }
sort: { fields: [frontmatter___date], order: DESC }
) {
edges {
node {
body
fields {
slug
}
}
}
}
}
`
);
if (errors) {
throw new Error('There was an error');
}
const tips = data.allMdx.edges;
tips.forEach((tip, i) => {
// figure out if there is a prev and next tip
const prev = tips[i - 1];
const next = tips[i + 1];
actions.createPage({
path: `/tip${tip.node.fields.slug}`,
component: tipTemplate,
context: {
slug: tip.node.fields.slug,
prev,
collection: 'tip',
next,
pathPrefix: '/tip',
},
});
});
}
async function paginate({
graphql,
actions,
collection,
pathPrefix,
component,
}) {
const { errors, data } = await graphql(
`
{
allMdx(filter: { fields: { collection: { eq: "${collection}" } } }) {
totalCount
}
}
`
);
const { totalCount } = data.allMdx;
const pages = Math.ceil(totalCount / 10);
Array.from({ length: pages }).forEach((_, i) => {
// for each page, use the createPages api to dynamically create that page
actions.createPage({
path: `${pathPrefix}${i + 1}`,
component,
context: {
skip: i * 10,
currentPage: i + 1,
},
});
});
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
await Promise.all([
makePostsFromMdx({ graphql, actions }),
makeTipsFromMdx({ graphql, actions }),
paginate({
graphql,
actions,
collection: 'tip',
pathPrefix: '/tips/',
component: path.resolve('./src/pages/tips.js'),
}),
paginate({
graphql,
actions,
collection: 'post',
pathPrefix: '/blog/',
component: path.resolve('./src/pages/blog.js'),
}),
]);
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
// if my posts have a slug in the frontmatter, it means I've specified what I want it to be. Otherwise I want to create one automatically
// This is where we add our own custom fields to each node
const generatedSlug = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value: node.frontmatter.slug
? `/${node.frontmatter.slug}/`
: generatedSlug,
});
// Add it to a collection
createNodeField({
name: `collection`,
node,
value: getNode(node.parent).sourceInstanceName,
});
}
};
exports.onCreatePage = ({ page, actions }) => {
const { createPage } = actions;
if (page.path.match(/thumbnail/)) {
page.context.layout = 'thumbnail';
createPage(page);
}
};