1
0
Fork 0
blog/gatsby/createPages.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-10-14 02:25:24 +02:00
const path = require('path')
2019-11-06 20:44:33 +01:00
const { itemsPerPage } = require('../config')
2018-10-14 02:25:24 +02:00
2020-07-11 10:29:42 +02:00
const postTemplate = path.resolve('src/components/templates/Post/index.tsx')
const archiveTemplate = path.resolve('src/components/templates/Archive.tsx')
2018-10-14 02:25:24 +02:00
const redirects = [
{ f: '/feed', t: '/feed.xml' },
2019-11-06 20:44:33 +01:00
{ f: '/feed/', t: '/feed.xml' },
{ f: '/goodies/', t: '/tags/goodies/' }
2018-10-14 02:25:24 +02:00
]
2019-11-06 20:44:33 +01:00
function getPaginationData(i, numPages, slug) {
const currentPage = i + 1
const prevPageNumber = currentPage <= 1 ? null : currentPage - 1
const nextPageNumber = currentPage + 1 > numPages ? null : currentPage + 1
const prevPagePath = prevPageNumber
? prevPageNumber === 1
? slug
: `${slug}page/${prevPageNumber}/`
: null
const nextPagePath = nextPageNumber ? `${slug}page/${nextPageNumber}/` : null
const path = i === 0 ? slug : `${slug}page/${i + 1}`
return { prevPagePath, nextPagePath, path }
}
2020-07-11 11:42:37 +02:00
exports.generatePostPages = (createPage, posts, archiveLength) => {
2018-10-14 02:25:24 +02:00
// Create Post pages
2020-05-08 13:27:40 +02:00
posts.forEach((post) => {
2018-10-14 02:25:24 +02:00
createPage({
path: `${post.node.fields.slug}`,
component: postTemplate,
context: {
2019-11-11 01:00:26 +01:00
slug: post.node.fields.slug,
prev: post.previous && {
title: post.previous.frontmatter.title,
slug: post.previous.fields.slug
},
next: post.next && {
title: post.next.frontmatter.title,
slug: post.next.fields.slug
}
2018-10-14 02:25:24 +02:00
}
})
})
2020-07-11 11:42:37 +02:00
// Create paginated archive pages
const numPages = Math.ceil(archiveLength / itemsPerPage)
2020-07-08 01:31:03 +02:00
const slug = `/archive/`
2019-11-06 20:44:33 +01:00
2018-10-14 02:25:24 +02:00
Array.from({ length: numPages }).forEach((_, i) => {
2019-11-06 20:44:33 +01:00
const { prevPagePath, nextPagePath, path } = getPaginationData(
i,
numPages,
slug
)
2018-10-14 02:25:24 +02:00
createPage({
2019-11-06 20:44:33 +01:00
path,
2020-07-08 01:31:03 +02:00
component: archiveTemplate,
2018-10-14 02:25:24 +02:00
context: {
2019-11-06 20:44:33 +01:00
slug,
limit: itemsPerPage,
skip: i * itemsPerPage,
2018-10-14 02:25:24 +02:00
numPages,
currentPageNumber: i + 1,
2019-11-06 20:44:33 +01:00
prevPagePath,
nextPagePath
2018-10-14 02:25:24 +02:00
}
})
})
}
2019-11-06 20:44:33 +01:00
exports.generateTagPages = (createPage, tags) => {
tags.forEach(({ tag, totalCount }) => {
// Create paginated tag pages
const numPages = Math.ceil(totalCount / itemsPerPage)
2020-07-11 11:42:37 +02:00
const slug = `/archive/${tag}/`
2018-10-14 02:25:24 +02:00
2019-11-06 20:44:33 +01:00
Array.from({ length: numPages }).forEach((_, i) => {
const { prevPagePath, nextPagePath, path } = getPaginationData(
i,
numPages,
slug
)
2018-10-14 02:25:24 +02:00
2019-11-06 20:44:33 +01:00
createPage({
path,
2020-07-08 01:31:03 +02:00
component: archiveTemplate,
2019-11-06 20:44:33 +01:00
context: {
tag,
slug,
limit: itemsPerPage,
skip: i * itemsPerPage,
numPages,
currentPageNumber: i + 1,
prevPagePath,
nextPagePath
}
})
2018-10-14 02:25:24 +02:00
})
})
}
2020-05-08 13:27:40 +02:00
exports.generateRedirectPages = (createRedirect) => {
2018-10-14 02:25:24 +02:00
redirects.forEach(({ f, t }) => {
createRedirect({
fromPath: f,
redirectInBrowser: true,
toPath: t
})
})
}