-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
69 lines (68 loc) · 1.9 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
const path = require('path')
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
graphql(`
{
allFile {
edges {
node {
relativeDirectory
name
}
}
}
allContentfulSitePage {
edges {
node {
slug
}
}
}
allContentfulBlogPost {
edges {
node {
slug
}
}
}
}
`)
.then(result => {
// Let's start by building an array with all current pages
// We've added '/' in contentful, so adding that in to start
let currentPages = ['/']
result.data.allFile.edges.filter(({ node }) => {
if (node.relativeDirectory.includes('pages')) {
return currentPages.push(node.name)
}
})
console.log(currentPages)
// Now let's create pages for anything that isn't our /pages
// directory already using indexOf() to check if it's in the array
result.data.allContentfulSitePage.edges.map(({ node }) => {
if (currentPages.indexOf(node.slug) === -1) {
console.log(`Creating new page ${node.slug}`)
createPage({
path: `${node.slug}`,
component: path.resolve('./src/templates/page.js'),
context: {
slug: node.slug,
},
})
}
})
result.data.allContentfulBlogPost.edges.map(({ node }) => {
createPage({
path: `/blog/${node.slug}`,
component: path.resolve('./src/templates/post.js'),
context: {
slug: node.slug,
},
})
})
})
.catch(error => console.error(error))
resolve()
})
}