1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-23 02:10:01 +01:00
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')
2020-03-04 22:21:12 +01:00
const postsTemplate = path.resolve('src/components/templates/Posts.tsx')
2019-11-06 20:44:33 +01:00
const { itemsPerPage } = require('../config')
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 }
}
exports.generatePostPages = (createPage, posts) => {
2020-03-04 22:21:12 +01:00
const postTemplate = path.resolve('src/components/templates/Post/index.tsx')
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
}
})
})
// Create paginated Blog index pages
2019-11-06 20:44:33 +01:00
const numPages = Math.ceil(posts.length / itemsPerPage)
const slug = `/`
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,
2018-10-14 02:25:24 +02:00
component: postsTemplate,
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)
const slug = `/tags/${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,
component: postsTemplate,
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
})
})
}