1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-22 18:00:06 +01:00
blog/gatsby/createPages.js

107 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')
2020-07-19 13:31:27 +02:00
const photosTemplate = path.resolve('src/components/templates/Photos.tsx')
2020-07-11 10:29:42 +02:00
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' },
2021-02-28 22:14:36 +01:00
{ f: '/goodies/', t: '/archive/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-19 13:31:27 +02:00
exports.generatePostPages = (createPage, posts) => {
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-19 13:31:27 +02:00
}
2018-10-14 02:25:24 +02:00
2021-03-01 00:03:39 +01:00
function generateIndexPages(createPage, length, slug, template) {
const numPages = Math.ceil(length / itemsPerPage)
2019-11-06 20:44:33 +01:00
2021-03-01 00:03:39 +01:00
Array.from({ length: numPages }).forEach((_, i) => {
2019-11-06 20:44:33 +01:00
const { prevPagePath, nextPagePath, path } = getPaginationData(
i,
2021-03-01 00:03:39 +01:00
numPages,
slug
2019-11-06 20:44:33 +01:00
)
2018-10-14 02:25:24 +02:00
createPage({
2019-11-06 20:44:33 +01:00
path,
2021-03-01 00:03:39 +01:00
component: template,
2018-10-14 02:25:24 +02:00
context: {
2021-03-01 00:03:39 +01:00
slug,
2019-11-06 20:44:33 +01:00
limit: itemsPerPage,
skip: i * itemsPerPage,
2021-03-01 00:03:39 +01:00
numPages: numPages,
2020-07-19 13:31:27 +02:00
currentPageNumber: i + 1,
prevPagePath,
nextPagePath
}
})
})
}
2021-03-01 00:03:39 +01:00
// Create paginated archive pages
exports.generateArchivePages = (createPage, archiveLength) => {
generateIndexPages(createPage, archiveLength, `/archive/`, archiveTemplate)
}
2020-07-19 13:31:27 +02:00
2021-03-01 00:03:39 +01:00
// Create paginated photos pages
exports.generatePhotosPages = (createPage, photosLength) => {
generateIndexPages(createPage, photosLength, `/photos/`, photosTemplate)
2018-10-14 02:25:24 +02:00
}
2021-03-01 00:03:39 +01:00
// Create paginated tag pages
2019-11-06 20:44:33 +01:00
exports.generateTagPages = (createPage, tags) => {
tags.forEach(({ tag, totalCount }) => {
2021-03-01 00:03:39 +01:00
generateIndexPages(
createPage,
totalCount,
`/archive/${tag}/`,
archiveTemplate
)
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
})
})
}