-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
157 lines (143 loc) · 4.16 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`)
const _ = require(`lodash`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const { paginate } = require(`gatsby-awesome-pagination`)
/**
* Events
*/
// Define the "Event" node type with a "collection" field.
exports.sourceNodes = ({ actions }) => {
actions.createTypes(`
type Event implements Node @dontInfer {
id: ID!
name: String!
location: String!
startDate: Date! @dateformat @proxy(from: "start_date")
endDate: Date! @dateformat @proxy(from: "end_date")
url: String!
collection: String!
}
`)
}
// Add and populate a "collection" field based on the file directory name.
exports.createResolvers = ({ createResolvers, getNode }) => {
// Get the containing directory for the event (past or future)
const collection = source => getNode(source.parent).relativeDirectory
// Add a "collection" field to each node.
createResolvers({
Event: {
collection: {
resolve: source => collection(source),
},
},
})
}
/**
* Articles
*/
// Default subject taxonomy to "random" if no subject provided.
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions
const typeDefs = [
"type MarkdownRemark implements Node { frontmatter: Frontmatter }",
schema.buildObjectType({
name: "Frontmatter",
fields: {
subject: {
type: "[String!]",
resolve(source, args, context, info) {
const { subject } = source
if (
source.subject == null ||
(Array.isArray(subject) && !subject.length)
) {
return ["random"]
}
return subject
},
},
},
}),
]
createTypes(typeDefs)
}
// Markdown items: Create slug nodes based on folder
exports.onCreateNode = ({ node, getNode, actions }) => {
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `content` })
actions.createNodeField({
node,
name: `slug`,
value: `/articles${slug}`,
})
}
}
// Generate pages for each article.
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
// Query all the data
const queryResult = await graphql(`
{
postQuery: allMarkdownRemark(
sort: { order: ASC, fields: [frontmatter___date] }
) {
edges {
node {
fields {
slug
}
}
}
}
taxQuery: allMarkdownRemark {
group(field: frontmatter___subject) {
nodes {
id
}
fieldValue
}
}
}
`)
if (queryResult.errors) {
reporter.panic("error loading events", queryResult.errors)
return
}
// Generate single post pages
const posts = queryResult.data.postQuery.edges
posts.forEach(post => {
createPage({
path: post.node.fields.slug,
component: path.resolve(`./src/templates/article.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: post.node.fields.slug,
},
})
})
// Create your paginated pages
paginate({
createPage, // The Gatsby `createPage` function
items: posts, // An array of objects
itemsPerPage: 2, // How many items you want per page
pathPrefix: "/articles", // Creates pages like `/blog`, `/blog/2`, etc
component: path.resolve(`./src/templates/articles.js`), // Just like `createPage()`
})
const taxonomies = queryResult.data.taxQuery.group
taxonomies.map(({ nodes: nodes, fieldValue: fieldValue }) => {
paginate({
createPage, // The Gatsby `createPage` function
items: nodes, // An array of objects
itemsPerPage: 2, // How many items you want per page
pathPrefix: `/subjects/${_.kebabCase(fieldValue)}`, // Creates pages like `/blog`, `/blog/2`, etc
component: path.resolve(`./src/templates/subjects.js`), // Just like `createPage()`
context: { subject: fieldValue },
})
})
}