-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgatsby-node.js
144 lines (126 loc) · 3.46 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
const path = require("path")
const { createFilePath } = require("gatsby-source-filesystem")
const fs = require("fs")
const getTemplate = templateName => {
const template = path.resolve(
__dirname,
"src",
"templates",
templateName,
"index.js"
)
if (fs.existsSync(template)) {
return template
} else {
throw new Error(`Could not find the specified template file at ${template}`)
}
}
/**
* When Gatsby starts to create GraphQL nodes, we can extend some of these
* nodes to include our own information for our benefit.
*
* Here, we slap on:
* id | Based on the markdown filename
* slug | Based on where the markdown file is in the file system
* template | Based on which folder the markdown file is in the file system
*
* The ID will later be used for relational linking or whatever.
*/
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type == "Mdx") {
const parent = getNode(node.parent)
const directoryParts = parent.relativeDirectory.split(/[\\/]/)
const folderName = directoryParts[0]
const slug = createFilePath({ node, getNode })
// Grab the filename of the Markdown file.
const { name } = path.parse(
createFilePath({ node, getNode, trailingSlash: false })
)
createNodeField({
name: "id",
node,
value: name,
})
createNodeField({
name: "slug",
node,
value: slug,
})
createNodeField({
node,
name: "template",
value: folderName,
})
createNodeField({
node,
name: "relativePath",
value: parent.relativePath,
})
}
}
exports.createPages = ({ actions, graphql, reporter }) => {
const { createPage, createRedirect } = actions
return graphql(`
{
allMdx(sort: { order: DESC, fields: [frontmatter___event_start] }) {
edges {
node {
fields {
template
slug
}
frontmatter {
is_public
render
homepage
redirects
}
}
}
}
}
`).then(result => {
if (result.errors) {
reporter.panicOnBuild("Error while running GraphQL query")
return
}
result.data.allMdx.edges
.filter(
// Filter out edges where rendering is not enabled
({ node }) => node.frontmatter.render
)
.forEach(({ node }) => {
const template = node.frontmatter.template || node.fields.template
let slug = node.fields.slug
// If the page is meant to be found on the homepage,
// redirect users to "/" instead
if (node.frontmatter.homepage) {
createRedirect({
fromPath: slug,
toPath: "/",
isPermanent: false,
})
slug = "/"
}
// Create a page at the expected location
createPage({
path: slug,
component: getTemplate(template),
context: node.fields,
})
// Create redirects to the current page
// Ignore redirects that are the same as the page.
// Don't want any infinite loops!
node.frontmatter?.redirects
?.filter(redirectPath => redirectPath !== slug)
.forEach(redirectPath => {
createRedirect({
fromPath: redirectPath,
toPath: slug,
isPermanent: false,
})
})
})
})
}