-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
184 lines (157 loc) · 4.45 KB
/
Copy pathgatsby-node.js
File metadata and controls
184 lines (157 loc) · 4.45 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const Promise = require('bluebird')
const path = require('path')
// const fs = require('fs')
let urlMap = {}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
const detailPage = path.resolve('./src/templates/detailPage.js')
const secondaryPage = path.resolve('./src/templates/secondaryPage.js')
const downloadPage = path.resolve('./src/templates/downloads.js')
const searchPage = path.resolve('./src/templates/search.js')
resolve(
graphql(
`
query allQuery {
allContentfulPage {
edges {
node {
id
slug
title
metaDescription
parentPage {
slug
}
}
}
}
allContentfulSecondaryPage {
edges {
node {
id
title
slug
metaDescription
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
const posts = result.data.allContentfulPage.edges
const secondaryPosts = result.data.allContentfulSecondaryPage.edges
//////////////////////
let arr = []
let topLevel = posts.filter(post => post.node.parentPage === null).map(post => post.node.slug)
let secondLevel = []
let thirdLevel = []
posts.forEach((post, index) => {
if (post.node.parentPage === null ) {
arr.push(post)
return
}
if (post.node.parentPage !== null && post.node.parentPage.slug) {
let fullSlug = `${post.node.parentPage.slug}/${post.node.slug}`
if (topLevel.includes(post.node.parentPage.slug)) {
secondLevel.push({
id: post.node.id,
slug: post.node.slug,
parent: post.node.parentPage.slug,
fullSlug: fullSlug,
temp: fullSlug
})
return
}
thirdLevel.push({
node: {
id: post.node.id,
slug: post.node.slug,
parent: post.node.parentPage.slug,
fullSlug: fullSlug
}
})
}
})
let temp = thirdLevel.map((post, index) => {
let g = secondLevel.filter(v => (post.node.parent === v.slug)).map(v => {
let s = `${v.fullSlug}/${post.node.slug}`
v.id = post.node.id
v['temp'] = s
let f = Object.assign({}, v)
return f
})
return g[0]
})
let path = secondLevel
.concat(temp)
.map(v => {
v['path'] = v.temp
delete v.temp
delete v.fullSlug
return {
node: v
}
})
path = path.concat(arr)
path.map(v => {
return urlMap[v.node.id] = v.node.path || v.node.slug
})
//////////////////////
path.forEach((post, index) => {
let path = post.node.path || post.node.slug
createPage({
path: path,
component: detailPage,
context: {
id: post.node.id,
map: urlMap,
slug: post.node.slug
},
})
})
secondaryPosts.forEach((post, index) => {
let path = post.node.slug
let template
switch(post.node.slug) {
case 'search':
template = searchPage
break
case 'downloads':
template = downloadPage
break
default:
template = secondaryPage
}
urlMap[post.node.id] = path
createPage({
path: path,
component: template,
context: {
id: post.node.id,
map: urlMap,
slug: post.node.slug
},
})
})
})
)
})
}
exports.onCreatePage = ({ page, actions }) => {
if (page.path === '/') {
const { createPage, deletePage } = actions
deletePage(page)
createPage({
...page,
context: {
...page.context,
map: urlMap
},
})
}
}