-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
99 lines (87 loc) · 2.34 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
// kindly adapted from https://www.gatsbyjs.org/docs/mdx/programmatically-creating-pages/
const path = require('path')
const has = require('lodash/has')
const get = require('lodash/get')
const { paramCase } = require('param-case')
const BLOG_FOLDER = '/content/blogposts/'
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
// only for mdx files, which are blogposts in the BLOG_FOLDER and have a date
if (
node.internal.type === 'Mdx' &&
node.fileAbsolutePath.includes(BLOG_FOLDER) &&
has(node, 'frontmatter.date') &&
has(node, 'frontmatter.title')
) {
const title = paramCase(get(node, 'frontmatter.title', node.id))
createNodeField({
name: 'pathname',
node,
value: `/blog/${node.frontmatter.date}/${title}`,
})
createNodeField({
name: 'type',
node,
value: 'blogpost',
})
}
}
const createBlogposts = async (graphql, createPage, reporter) => {
// get all blogposts and use specific layout and data (title, date)
const allBlogposts = await graphql(`
fragment meta on Mdx {
fields {
pathname
}
frontmatter {
title
}
}
query {
allMdx(
sort: { fields: frontmatter___date, order: ASC }
filter: { fields: { type: { eq: "blogpost" } } }
) {
edges {
node {
id
fields {
pathname
}
}
next {
...meta
}
previous {
...meta
}
}
}
}
`)
if (allBlogposts.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query')
}
const posts = allBlogposts.data.allMdx.edges
posts.forEach(({ node, next, previous }) => {
createPage({
path: node.fields.pathname,
component: path.resolve(`./src/components/BlogpostLayout/index.js`),
context: {
id: node.id,
previous: {
pathname: get(previous, 'fields.pathname'),
title: get(previous, 'frontmatter.title'),
},
next: {
pathname: get(next, 'fields.pathname'),
title: get(next, 'frontmatter.title'),
},
},
})
})
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
await createBlogposts(graphql, createPage, reporter)
}