-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
51 lines (46 loc) · 1.55 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
const path = require(`path`)
exports.onCreateNode = ({ node, actions }) => {
if (node.internal.type === `PodcastShow`) {
console.log(`slug: ${node.slug}`)
actions.createPage({
path: `/shows/${node.slug}/`,
component: path.resolve(`./src/templates/podcast-show.js`),
context: node
})
console.log(`created page`)
}
let earliestDate = new Date()
earliestDate = earliestDate.setDate(earliestDate.getDate() - 90)
const formatDate = d => {
const year = d.getFullYear()
const month = (d.getMonth() + 1).toString().padStart(2, '0')
const day = d.getDate().toString().padStart(2, '0')
return `${year}${month}${day}`
}
exports.onPreExtractQueries = ({ actions }) => {
console.log(`Creating pages by date range`)
let today = newDate()
let endDate = newDate()
endDate.setDate(today.getDate() - today.getDay())
let startDate = newDate()
startDate.setDate(endDate.getDate() - 7)
while (startDate > earliestDate) {
const slug = `/episodes/for-week/${formatDate(startDate)}-${formatDate(endDate)}/`
console.log(`Created page for ${slug}`)
actions.createPage({
path: slug,
component: path.resolve(`./src/templates/episodes-for-week.js`),
context: {
startDate: new Date(startDate),
endDate: new Date(endDate)
}
})
startDate.setDate(startDate.getDate() - 7)
endDate.setDate(endDate.getDate() - 7)
}
}
}
const newDate = () => {
const d = new Date()
return new Date(d.getFullYear(), d.getMonth(), d.getDate())
}