-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgatsby-node.js
116 lines (111 loc) · 2.81 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
const path = require(`path`)
// Create a slug for each recipe and set it as a field on the node.
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
const slug = 'articles/' + node.nid;
createNodeField({
node,
name: `slug`,
value: slug,
})
}
// Implement the Gatsby API “createPages”. This is called once the
// data layer is bootstrapped to let plugins create pages from data.
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
const articleTemplate = path.resolve(`src/templates/node/article/index.js`)
const pageTemplate = path.resolve(`src/templates/node/page/index.js`)
const categoryTemplate = path.resolve(`src/templates/taxonomy/tag/index.js`)
// page building queries
resolve(
graphql(
`
{
allTaxonomyTermTags {
edges {
node {
name
tid
path {
alias
}
}
}
}
allNodePage {
edges {
node {
title
nid
path {
alias
}
body {
value
processed
}
fields {
slug
}
}
}
}
allNodeArticle {
edges {
node {
title
nid
path {
alias
}
body {
value
processed
}
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
reject(result.errors)
}
// pages for each article.
result.data.allNodePage.edges.forEach(({ node }) => {
createPage({
path: node.path.alias,
component: pageTemplate,
context: {
nid: node.nid
},
})
})
result.data.allNodeArticle.edges.forEach(({ node }) => {
createPage({
path: node.path.alias,
component: articleTemplate,
context: {
nid: node.nid
},
})
})
//pages for each term
result.data.allTaxonomyTermTags.edges.forEach(({ node }) => {
createPage({
path: node.path.alias,
component: categoryTemplate,
context: {
tid: node.tid,
title: node.name
},
})
});
})
)
})
}