-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
107 lines (95 loc) · 2.29 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const got = require('got')
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return graphql(
`
{
site {
siteMetadata {
baseUrl
}
}
}
`
)
.then(result => {
const baseUrl = result.data.site.siteMetadata.baseUrl
console.log(`Using '${baseUrl}'`)
const client = got.extend({
baseUrl,
json: true,
})
return client
.get('/v1/buckets/weihnachtsmarkt/collections/booths/records')
.then(({ body }) => {
body.data.forEach((data, index) => {
let path = createPath(data, index)
console.log('Create path: ' + path)
createPage({
path: path,
component: process.cwd() + '/src/templates/Details.js',
context: data,
})
})
})
})
}
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
const config = getConfig()
let newConfig = {
...config,
module: {
...config.module,
noParse: /(mapbox-gl)\.js$/,
},
}
if (stage === 'build-html') {
newConfig = {
...newConfig,
module: {
...newConfig.module,
rules: [
...newConfig.module.rules,
{
test: /(mapbox-gl)\.js$/,
loader: 'null-loader',
},
],
},
}
}
actions.replaceWebpackConfig(newConfig)
}
function createPath(data, index) {
let path = '/details/' + index
if ('name' in data) {
let slugifiedName = slugify(data.name)
if (slugifiedName === null) {
if ('id' in data) {
slugifiedName = data.id
} else {
slugifiedName = index
}
}
path = '/details/' + slugifiedName
}
return path
}
function slugify(text) {
if (text === undefined || text === null) {
return null
}
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}