forked from LekoArts/gatsby-starter-minimal-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
365 lines (322 loc) · 9.33 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const fs = require(`fs`)
const kebabCase = require(`lodash.kebabcase`)
const mkdirp = require(`mkdirp`)
const path = require(`path`)
const withDefaults = require(`./src/utils/default-options`)
// Ensure that content directories exist at site-level
// If non-existent they'll be created here (as empty folders)
exports.onPreBootstrap = ({ reporter, store }, themeOptions) => {
const { program } = store.getState()
const { postsPath, pagesPath } = withDefaults(themeOptions)
const dirs = [path.join(program.directory, postsPath), path.join(program.directory, pagesPath)]
dirs.forEach(dir => {
if (!fs.existsSync(dir)) {
reporter.info(`Initializing "${dir}" directory`)
mkdirp.sync(dir)
}
})
}
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
}
// Create general interfaces that you could can use to leverage other data sources
// The core theme sets up MDX as a type for the general interface
exports.createSchemaCustomization = ({ actions, schema }, themeOptions) => {
const { createTypes, createFieldExtension } = actions
const { basePath } = withDefaults(themeOptions)
const slugify = source => {
const slug = source.slug ? source.slug : kebabCase(source.title)
return `/${basePath}/${slug}`.replace(/\/\/+/g, `/`)
}
createFieldExtension({
name: `slugify`,
extend() {
return {
resolve: slugify,
}
},
})
createFieldExtension({
name: `mdxpassthrough`,
args: {
fieldName: `String!`,
},
extend({ fieldName }) {
return {
resolve: mdxResolverPassthrough(fieldName),
}
},
})
createTypes(`
interface Post implements Node {
id: ID!
slug: String! @slugify
title: String!
date: Date! @dateformat
updated: Date! @dateformat
layout: String
excerpt(pruneLength: Int = 160): String!
body: String!
html: String
timeToRead: Int
tags: [PostTag]
banner: File @fileByRelativePath
description: String
canonicalUrl: String
}
type PostTag {
name: String
slug: String
}
interface Page implements Node {
id: ID!
slug: String!
title: String!
excerpt(pruneLength: Int = 160): String!
body: String!
}
type MdxPost implements Node & Post {
slug: String! @slugify
title: String!
date: Date! @dateformat
updated: Date! @dateformat
layout: String
excerpt(pruneLength: Int = 140): String! @mdxpassthrough(fieldName: "excerpt")
body: String! @mdxpassthrough(fieldName: "body")
html: String! @mdxpassthrough(fieldName: "html")
timeToRead: Int @mdxpassthrough(fieldName: "timeToRead")
tags: [PostTag]
banner: File @fileByRelativePath
description: String
canonicalUrl: String
}
type MdxPage implements Node & Page {
slug: String!
title: String!
excerpt(pruneLength: Int = 140): String! @mdxpassthrough(fieldName: "excerpt")
body: String! @mdxpassthrough(fieldName: "body")
}
type MinimalBlogConfig implements Node {
basePath: String
blogPath: String
postsPath: String
pagesPath: String
postsPrefix: String
tagsPath: String
externalLinks: [ExternalLink]
navigation: [NavigationEntry]
showLineNumbers: Boolean
showCopyButton: Boolean
}
type ExternalLink {
name: String!
url: String!
}
type NavigationEntry {
title: String!
slug: String!
}
`)
}
exports.sourceNodes = ({ actions, createContentDigest }, themeOptions) => {
const { createNode } = actions
const {
basePath,
blogPath,
postsPath,
pagesPath,
tagsPath,
navigation,
showLineNumbers,
} = withDefaults(themeOptions)
const minimalBlogConfig = {
basePath,
blogPath,
postsPath,
pagesPath,
tagsPath,
navigation,
showLineNumbers,
}
createNode({
...minimalBlogConfig,
id: `@lekoarts/gatsby-theme-minimal-blog-core-config`,
parent: null,
children: [],
internal: {
type: `MinimalBlogConfig`,
contentDigest: createContentDigest(minimalBlogConfig),
content: JSON.stringify(minimalBlogConfig),
description: `Options for @lekoarts/gatsby-theme-minimal-blog-core`,
},
})
}
exports.onCreateNode = ({ node, actions, getNode, createNodeId, createContentDigest }, themeOptions) => {
const { createNode, createParentChildLink } = actions
const { postsPath, pagesPath } = withDefaults(themeOptions)
// Make sure that it's an MDX node
if (node.internal.type !== `Mdx`) {
return
}
// Create a source field
// And grab the sourceInstanceName to differentiate the different sources
// In this case "postsPath" and "pagesPath"
const fileNode = getNode(node.parent)
const source = fileNode.sourceInstanceName
// Check for "posts" and create the "Post" type
if (node.internal.type === `Mdx` && source === postsPath) {
let modifiedTags
if (node.frontmatter.tags) {
modifiedTags = node.frontmatter.tags.map(tag => ({
name: tag,
slug: kebabCase(tag),
}))
} else {
modifiedTags = null
}
const fieldData = {
slug: node.frontmatter.slug ? node.frontmatter.slug : undefined,
title: node.frontmatter.title,
date: node.frontmatter.date,
tags: modifiedTags,
banner: node.frontmatter.banner,
description: node.frontmatter.description,
updated: node.frontmatter.updated,
layout: node.frontmatter.layout
}
const mdxPostId = createNodeId(`${node.id} >>> MdxPost`)
createNode({
...fieldData,
// Required fields
id: mdxPostId,
parent: node.id,
children: [],
internal: {
type: `MdxPost`,
contentDigest: createContentDigest(fieldData),
content: JSON.stringify(fieldData),
description: `Mdx implementation of the Post interface`,
},
})
createParentChildLink({ parent: node, child: getNode(mdxPostId) })
}
// Check for "pages" and create the "Page" type
if (node.internal.type === `Mdx` && source === pagesPath) {
const fieldData = {
title: node.frontmatter.title,
slug: node.frontmatter.slug,
}
const mdxPageId = createNodeId(`${node.id} >>> MdxPage`)
createNode({
...fieldData,
// Required fields
id: mdxPageId,
parent: node.id,
children: [],
internal: {
type: `MdxPage`,
contentDigest: createContentDigest(fieldData),
content: JSON.stringify(fieldData),
description: `Mdx implementation of the Page interface`,
},
})
createParentChildLink({ parent: node, child: getNode(mdxPageId) })
}
}
// These template are only data-fetching wrappers that import components
const homepageTemplate = require.resolve(`./src/templates/homepage-query.tsx`)
const blogTemplate = require.resolve(`./src/templates/blog-query.tsx`)
const postTemplate = require.resolve(`./src/templates/post-query.tsx`)
const pageTemplate = require.resolve(`./src/templates/page-query.tsx`)
const tagTemplate = require.resolve(`./src/components/tag.tsx`)
const tagsTemplate = require.resolve(`./src/templates/tags-query.tsx`)
exports.createPages = async ({ actions, graphql, reporter }, themeOptions) => {
const { createPage } = actions
const { basePath, blogPath, tagsPath, formatString } = withDefaults(themeOptions)
createPage({
path: basePath,
component: homepageTemplate,
context: {
formatString,
},
})
createPage({
path: `/${basePath}/${blogPath}`.replace(/\/\/+/g, `/`),
component: blogTemplate,
context: {
formatString,
},
})
createPage({
path: `/${basePath}/${tagsPath}`.replace(/\/\/+/g, `/`),
component: tagsTemplate,
})
const result = await graphql(`
query {
allPost(sort: { fields: date, order: DESC }) {
nodes {
slug
}
}
allPage {
nodes {
slug
}
}
tags: allPost(sort: { fields: tags___name, order: DESC }) {
group(field: tags___name) {
fieldValue
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild(`There was an error loading your posts or pages`, result.errors)
return
}
const posts = result.data.allPost.nodes
posts.forEach(post => {
createPage({
path: post.slug,
component: postTemplate,
context: {
slug: post.slug,
formatString,
},
})
})
const pages = result.data.allPage.nodes
if (pages.length > 0) {
pages.forEach(page => {
createPage({
path: `/${basePath}/${page.slug}`.replace(/\/\/+/g, `/`),
component: pageTemplate,
context: {
slug: page.slug,
},
})
})
}
const tags = result.data.tags.group
if (tags.length > 0) {
tags.forEach(tag => {
createPage({
path: `/${basePath}/${tagsPath}/${kebabCase(tag.fieldValue)}`.replace(/\/\/+/g, `/`),
component: tagTemplate,
context: {
slug: kebabCase(tag.fieldValue),
name: tag.fieldValue,
formatString,
},
})
})
}
}