-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
200 lines (189 loc) · 5.01 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const config = require('./src/utils/siteConfig')
const path = require(`path`)
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const loadPosts = new Promise((resolve, reject) => {
graphql(`
{
allContentfulPost(
sort: { fields: [publishDate], order: DESC }
limit: 10000
) {
edges {
node {
slug
publishDate
}
}
}
}
`).then(result => {
const posts = result.data.allContentfulPost.edges
const postsPerFirstPage = config.postsPerHomePage
const blogLimit = 6;
const postsPerPage = config.postsPerPage
const numPages = Math.ceil(
posts.slice(postsPerFirstPage).length / postsPerPage
)
// Create blog
createPage({
path: `/blog/`,
component: path.resolve(`./src/templates/blog.js`),
context: {
limit: blogLimit,
skip: 0,
numPages: numPages + 1,
currentPage: 1,
},
})
// Create additional pagination on blog page if needed
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: `/blog/${i + 2}/`,
component: path.resolve(`./src/templates/blog.js`),
context: {
limit: postsPerPage,
skip: i * postsPerPage + postsPerFirstPage,
numPages: numPages + 1,
currentPage: i + 2,
},
})
})
// Create each individual post
posts.forEach((edge, i) => {
const prev = i === 0 ? null : posts[i - 1].node
const next = i === posts.length - 1 ? null : posts[i + 1].node
createPage({
path: `/blog/${edge.node.slug}/`,
component: path.resolve(`./src/templates/post.js`),
context: {
slug: edge.node.slug,
prev,
next,
},
})
})
resolve()
})
})
const loadTags = new Promise((resolve, reject) => {
graphql(`
{
allContentfulTag {
edges {
node {
slug
post {
id
}
}
}
}
}
`).then(result => {
const tags = result.data.allContentfulTag.edges
const postsPerPage = config.postsPerPage
// Create tag pages with pagination if needed
tags.map(({ node }) => {
const totalPosts = node.post !== null ? node.post.length : 0
const numPages = Math.ceil(totalPosts / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path:
i === 0 ? `/blog/tag/${node.slug}/` : `blog/tag/${node.slug}/${i + 1}/`,
component: path.resolve(`./src/templates/tag.js`),
context: {
slug: node.slug,
limit: postsPerPage,
skip: i * postsPerPage,
numPages: numPages,
currentPage: i + 1,
},
})
})
})
resolve()
})
})
const loadListings = new Promise((resolve, reject) => {
graphql(`
{
allContentfulListing {
edges {
node {
slug
}
}
}
}
`).then(result => {
const pages = result.data.allContentfulListing.edges
const homePageLimit = 3;
// Create main home page
createPage({
path: `/`,
component: path.resolve(`./src/templates/index.js`),
context: {
limit: homePageLimit,
skip: 0,
},
})
pages.map(({ node }) => {
createPage({
path: `/listings/${node.slug}/`,
component: path.resolve(`./src/templates/listing.js`),
context: {
slug: node.slug,
},
})
})
resolve()
})
})
const loadPages = new Promise((resolve, reject) => {
graphql(`
{
allContentfulPage {
edges {
node {
slug
community
about
}
}
}
}
`).then(result => {
const pages = result.data.allContentfulPage.edges
pages.map(({ node }) => {
if (node.about) {
createPage({
path: `/about/${node.slug}/`,
component: path.resolve(`./src/templates/page.js`),
context: {
slug: node.slug,
},
})
} else if (node.community) {
createPage({
path: `/communities/${node.slug}/`,
component: path.resolve(`./src/templates/page.js`),
context: {
slug: node.slug,
},
})
} else {
createPage({
path: `/${node.slug}/`,
component: path.resolve(`./src/templates/page.js`),
context: {
slug: node.slug,
},
})
}
})
resolve()
})
})
return Promise.all([loadPosts, loadTags, loadPages, loadListings])
}