-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
211 lines (187 loc) · 4.94 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const _ = require('lodash')
const createBlogPosts = (createPage, posts) => {
// Create Previous & Next for posts
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: post.node.fields.postUrl,
component: path.resolve('src/templates/blog-post-template.js'),
context: {
id: post.node.id,
filePath: post.node.fields.filePath,
previous,
next,
},
})
})
}
const createBlogTags = (createPage, posts) => {
// Tag pages:
let tags = []
// Iterate through each post, putting all found tags into `tags`
_.each(posts, edge => {
if (_.get(edge, 'node.fields.tags')) {
tags = tags.concat(edge.node.fields.tags)
}
})
// Eliminate duplicate tags
tags = _.uniq(tags)
tags.forEach(tag => {
createPage({
path: `/tags/${_.kebabCase(tag)}/`,
component: path.resolve('src/templates/tag-template.js'),
context: {
tag,
},
})
})
}
const createBlogCategories = (createPage, posts) => {
// Tag pages:
let categories = []
// Iterate through each post, putting all found tags into `tags`
_.each(posts, edge => {
if (_.get(edge, 'node.fields.categories')) {
categories = categories.concat(edge.node.fields.categories)
}
})
// Eliminate duplicate tags
categories = _.uniq(categories)
categories.forEach(category => {
createPage({
path: `/categories/${_.kebabCase(category)}/`,
component: path.resolve('src/templates/category-template.js'),
context: {
category,
},
})
})
}
exports.createPages = ({ actions, graphql }) => {
// const { createPage } = actions
// const blogPostTemplate =
// const tagTemplate =
// Query for markdown nodes to use in creating pages.
// You can query for whatever data you want to create pages for e.g.
// products, portfolio items, landing pages, etc.
return new Promise((resolve, reject) => {
resolve(
graphql(
`
{
allMdx(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
node {
id
fields {
id
filePath
postUrl
published
date
title
categories
tags
keywords
description
}
frontmatter {
slug
}
code {
scope
}
}
}
}
}
`
).then(result => {
// Handle errors
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
// Handle no posts
if (_.isEmpty(result.allMdx)) {
reject('Sorry there are no posts!')
}
// Create blog posts pages, tags pages, category.
const posts = result.data.allMdx.edges
const { createPage } = actions
createBlogPosts(createPage, posts)
createBlogTags(createPage, posts)
createBlogCategories(createPage, posts)
})
)
})
}
// This generates a node field for each new post
// Could use for automaticlaly creating urls from a file path
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const pathToPost = createFilePath({ node, getNode, basePath: `blog-posts` })
createNodeField({
name: `id`,
node,
value: node.id,
})
createNodeField({
name: `filePath`,
node,
value: pathToPost,
})
createNodeField({
name: `postUrl`,
node,
value: `/blog${node.frontmatter.slug}`,
})
createNodeField({
name: `published`,
node,
value: node.frontmatter.published,
})
createNodeField({
name: `date`,
node,
value: node.frontmatter.date ? node.frontmatter.date.split(' ')[0] : '',
})
createNodeField({
name: `title`,
node,
value: node.frontmatter.title || '',
})
createNodeField({
name: `categories`,
node,
value: node.frontmatter.categories || [],
})
createNodeField({
name: `tags`,
node,
value: node.frontmatter.tags || [],
})
createNodeField({
name: `keywords`,
node,
value: node.frontmatter.keywords || [],
})
createNodeField({
name: `description`,
node,
value: node.frontmatter.description || '',
})
}
}