Repository navigation
Expand file tree
/
Copy pathgatsby-node.js
More file actions
154 lines (143 loc) · 4.17 KB
/
Copy pathgatsby-node.js
File metadata and controls
154 lines (143 loc) · 4.17 KB
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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path')
/**
* Enable absolute imports with `/src` as root.
*
* See: https://github.com/alampros/gatsby-plugin-resolve-src/issues/4
*/
exports.onCreateWebpackConfig = ({ actions, stage, loaders }) => {
const config = {
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
}
// when building HTML, window is not defined, so Leaflet causes the build to blow up
if (stage === 'build-html') {
config.module = {
rules: [
{
test: /leaflet/,
use: loaders.null(),
},
],
}
}
actions.setWebpackConfig(config)
}
exports.onCreateNode = ({ node, actions: { createNodeField } }) => {
// if the page originated from JSX, add a title based on the path
if (node.isCreatedByStatefulCreatePages) {
const pathParts = node.path.split('/')
let [title] = pathParts.slice(pathParts.length - 2, pathParts.length - 1)
if (title && title.length) {
title = `${title.slice(0, 1).toUpperCase()}${title.slice(1)}`
} else {
title = null
}
createNodeField({ node, name: 'title', value: title })
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
const contentTemplate = path.resolve(`src/templates/content.jsx`)
const mapTemplate = path.resolve(`src/templates/map.jsx`)
const speciesTemplate = path.resolve(`src/templates/species.jsx`)
const habitatTemplate = path.resolve(`src/templates/habitat.jsx`)
const ecosystemTemplate = path.resolve(`src/templates/ecosystem.jsx`)
let template = null
// Query for markdown nodes to use in creating pages.
resolve(
graphql(
`
{
allMarkdownRemark {
edges {
node {
frontmatter {
path
}
}
}
}
allJson(filter: { itemType: { ne: null } }) {
edges {
node {
id
path
itemType
habitatType
bounds
}
}
}
}
`
).then(result => {
if (result.errors) {
console.error(result.errors)
reject(result.errors)
}
// Create pages for each markdown file.
result.data.allMarkdownRemark.edges.forEach(
({
node: {
frontmatter: { path: pagePath },
},
}) => {
createPage({
path: pagePath,
component: contentTemplate,
})
}
)
// Create map pages for each species and habitat entry in maps/*.json
result.data.allJson.edges.forEach(
({ node: { id, path: pagePath, itemType, habitatType, bounds } }) => {
const imgSrc = `profiles/${id}.jpg`
const mapImgSrc = `maps/${id}.png`
if (itemType === 'species') {
template = speciesTemplate
} else if (habitatType) {
switch (habitatType) {
case 'ecosystem': {
template = ecosystemTemplate
break
}
case 'conservation asset': {
template = habitatTemplate
break
}
case 'habitat': {
template = habitatTemplate
break
}
default: {
break
}
}
}
// create profile pages
createPage({
path: pagePath,
context: { id, imgSrc, mapImgSrc },
component: template,
})
// Create map pages
if (bounds !== null && bounds !== undefined) {
createPage({
path: `${pagePath}/map`,
context: { id },
component: mapTemplate,
})
}
}
)
})
)
})
}