-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgatsby-node.mjs
308 lines (274 loc) · 8.33 KB
/
gatsby-node.mjs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { generateEventsMDXIndex } from './gatsby-node/events-mdx-index.mjs';
import {
createEditionPages,
updateEditionPagesContext
} from './gatsby-node/edition-pages.mjs';
import {
createCommonSchema,
createEditionSchema,
createEventPeopleRelSchema,
createEventSchema,
createLetterSchema,
createPeopleSchema,
createSponsorSchema,
createUpdatesSchema
} from './gatsby-node/schema.mjs';
import { capitalize, pageComponent } from './gatsby-node/utils.mjs';
import { createUpdatesPages } from './gatsby-node/updates-pages.mjs';
export const onCreatePage = async (helpers) => {
const {
page,
actions: { deletePage }
} = helpers;
await updateEditionPagesContext(helpers);
// Remove sandbox in production.
if (process.env.NODE_ENV === 'production') {
if (page.path.match(/^\/sandbox/)) {
await deletePage(page);
}
}
};
export const onCreateNode = async ({
node,
actions,
createNodeId,
createContentDigest,
getNode,
reporter
}) => {
const { createNode, createParentChildLink } = actions;
const fileNode = getNode(node.parent);
if (!fileNode?.name) return;
const slug = fileNode.name.toLowerCase();
if (node.internal.type === `EditionsYaml` && node.parent) {
const nodeProps = {
published: true,
...node,
weight: node.weight || 0,
cId: slug,
slug
};
const newNode = {
...nodeProps,
id: createNodeId(`${node.id} >>> Edition`),
parent: fileNode.id,
internal: {
type: 'Edition',
contentDigest: createContentDigest(nodeProps)
}
};
await createNode(newNode);
await createParentChildLink({ parent: fileNode, child: newNode });
}
// Create a new node type for all the content pieces based on the folder name.
// This allows us to easily get content by type, rather than all MDX together.
if (node.internal.type === `Mdx` && node.parent) {
const nodeType = capitalize(fileNode.sourceInstanceName);
let nodeProps = {
published: true,
...node.frontmatter,
weight: node.frontmatter.weight || 0,
cId: slug.replace(/(^\/|\/$)/g, ''),
slug
};
if (nodeType === 'Event' || nodeType === 'People') {
// The following applies to events and people content types.
// The relative directory is the path to the file from the event content
// directory. We are structuring the content in a way that the events are
// in a folder with the edition name.
// events /
// edition-id /
// event-id.mdx
// edition-id /
// event-id.mdx
const edition = fileNode.relativeDirectory;
nodeProps = {
...nodeProps,
cId: `${edition}-${nodeProps.cId}`,
edition
};
} else if (nodeType === 'Updates') {
const dir = fileNode.relativeDirectory;
const match = dir.match(/(\d{4}-\d{2}-\d{2})-(.*)/);
if (!match) {
reporter.panicOnBuild(
`Blog post directory name is not valid: ${dir}. It should be in the format of YYYY-MM-DD-title.`
);
return;
}
const [, date, name] = match;
const finalDate = new Date(node.frontmatter.date || date);
const formattedDate = finalDate.toISOString().split('T')[0];
const finalSlug = `${formattedDate}-${name}`;
nodeProps = {
...nodeProps,
cId: finalSlug,
slug: finalSlug,
date: finalDate
};
}
const newNode = {
...nodeProps,
id: createNodeId(`${node.id} >>> ${nodeType}`),
parent: node.id,
internal: {
type: nodeType,
contentDigest: createContentDigest(nodeProps),
contentFilePath: node.internal.contentFilePath
}
};
await createNode(newNode);
await createParentChildLink({ parent: node, child: newNode });
}
};
// Implement createPages api since we only want to create pages for published
// content and it is not possible to do with the File routing API.
export const createPages = async (helpers) => {
const { actions, graphql } = helpers;
// Create events index. See (./gatsby-node/events-mdx-index) for details.
await generateEventsMDXIndex(helpers);
// Edition specific pages.
await createEditionPages(helpers);
// Updates pages.
await createUpdatesPages(helpers);
// --------------------------------------------------------------
// Global pages that are not edition specific.
const { data } = await graphql(`
query {
# Global letter pages that are not edition specific.
allLetter(
filter: {
title: { ne: "" }
published: { eq: true }
editions: { elemMatch: { edition: { cId: { eq: null } } } }
}
) {
nodes {
id
slug
editions {
edition {
cId
}
}
internal {
contentFilePath
}
}
}
}
`);
// Create global letter pages
const letterNodes = data?.allLetter.nodes || [];
for (const node of letterNodes) {
const { slug, id } = node;
await actions.createPage({
path: `/${slug}`,
// Details at: https://www.gatsbyjs.com/docs/how-to/routing/mdx/#make-a-layout-template-for-your-posts
component: pageComponent(
'letter-page.tsx',
node.internal.contentFilePath
),
context: { slug, id }
});
}
};
export const createSchemaCustomization = (helpers) => {
createCommonSchema(helpers);
createEditionSchema(helpers);
createUpdatesSchema(helpers);
createLetterSchema(helpers);
createEventSchema(helpers);
createPeopleSchema(helpers);
createEventPeopleRelSchema(helpers);
createSponsorSchema(helpers);
};
export const createResolvers = ({ createResolvers }) => {
const resolvers = {
Query: {
updatesByTag: {
type: ['Updates!'],
args: {
tag: 'String',
limit: 'Int',
skip: 'Int',
filter: 'UpdatesFilterInput'
},
resolve: async (source, args, context) => {
const { tag, limit, skip, filter: userFilter = {} } = args;
const getEntries = async (filter = {}) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
published: { eq: true },
...filter,
...userFilter
},
limit,
skip,
sort: { date: 'DESC' }
},
type: 'Updates'
});
return Array.from(entries);
};
if (!tag || tag === 'all') {
return getEntries();
}
const entriesByTag = await getEntries({ tags: { eq: tag } });
const entriesByEdition = await getEntries({
editions: { elemMatch: { edition: { name: { eq: tag } } } }
});
return entriesByEdition.concat(entriesByTag);
}
},
featuredEditionUpdates: {
type: ['Updates!'],
args: {
edition: 'String!',
limit: 'Int',
skip: 'Int',
filter: 'UpdatesFilterInput'
},
resolve: async (source, args, context) => {
const {
edition,
limit = Infinity,
skip = 0,
filter: userFilter = {}
} = args;
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
published: { eq: true },
editions: {
elemMatch: {
edition: { cId: { eq: edition } },
featured: { ne: null }
}
},
...userFilter
}
},
type: 'Updates'
});
const nodes = Array.from(entries);
// Sort nodes by edition's featured order.
const sorted = [...nodes].sort((a, b) => {
const aOrder =
a.editions.find((e) => e.edition === edition).featured || 0;
const bOrder =
b.editions.find((e) => e.edition === edition).featured || 0;
if (aOrder === bOrder) {
return b.date - a.date;
}
return bOrder - aOrder;
});
// Return the sorted nodes with limit and skip.
return sorted.slice(skip, limit);
}
}
}
};
createResolvers(resolvers);
};