-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
179 lines (155 loc) · 3.79 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
const path = require('path')
const _ = require('lodash')
const createPosts = (createPage, createRedirect, edges) => {
edges.forEach(({ node }, i) => {
const next = i === 0 ? null : edges[i - 1].node
const prev = i === edges.length - 1 ? null : edges[i + 1].node
const pagePath = node.fields.slug
if (node.fields.redirects) {
node.fields.redirects.forEach((fromPath) => {
createRedirect({
fromPath,
toPath: pagePath,
redirectInBrowser: true,
isPermanent: true,
})
})
}
createPage({
path: pagePath,
component: path.resolve(`./src/templates/post.js`),
context: {
id: node.id,
previousId: prev ? prev.id : undefined,
nextId: next ? next.id : undefined,
},
})
})
}
exports.createPages = ({ actions, graphql }) =>
graphql(`
query {
allMdx(
filter: {
frontmatter: { published: { ne: false } }
fileAbsolutePath: { regex: "//content/writing//" }
}
sort: { order: DESC, fields: [frontmatter___date] }
) {
edges {
node {
id
parent {
... on File {
name
sourceInstanceName
}
}
excerpt(pruneLength: 250)
fields {
title
slug
date
}
}
}
}
}
`).then(({ data, errors }) => {
if (errors) {
return Promise.reject(errors)
}
if (_.isEmpty(data.allMdx)) {
return Promise.reject('There are no posts!')
}
const { edges } = data.allMdx
const { createRedirect, createPage } = actions
createPosts(createPage, createRedirect, edges)
// Create writing page
createPage({
path: '/writing',
component: path.resolve(`./src/templates/writing.js`),
context: {},
})
return null
})
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
})
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const parent = getNode(node.parent)
const titleSlugged = _.join(_.drop(parent.name.split('-'), 3), '-')
const slug =
parent.sourceInstanceName === 'legacy'
? `writing/${node.frontmatter.date
.split('T')[0]
.replace(/-/g, '/')}/${titleSlugged}`
: node.frontmatter.slug || titleSlugged
createNodeField({
name: 'id',
node,
value: node.id,
})
createNodeField({
name: 'published',
node,
value: node.frontmatter.published,
})
createNodeField({
name: 'pinned',
node,
value: node.frontmatter.pinned,
})
createNodeField({
name: 'title',
node,
value: node.frontmatter.title,
})
createNodeField({
name: 'description',
node,
value: node.frontmatter.description,
})
createNodeField({
name: 'slug',
node,
value: slug,
})
createNodeField({
name: 'date',
node,
value: node.frontmatter.date ? node.frontmatter.date.split(' ')[0] : '',
})
createNodeField({
name: 'banner',
node,
banner: node.frontmatter.banner,
})
createNodeField({
name: 'ogimage',
node,
ogimage: node.frontmatter.ogimage,
})
createNodeField({
name: 'categories',
node,
value: node.frontmatter.categories || [],
})
createNodeField({
name: 'tags',
node,
value: node.frontmatter.tags,
})
createNodeField({
name: 'redirects',
node,
value: node.frontmatter.redirects,
})
}
}