2018-10-14 02:25:24 +02:00
|
|
|
const path = require('path')
|
2019-10-02 13:35:50 +02:00
|
|
|
const postsTemplate = path.resolve('src/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) => {
|
2019-10-28 23:00:55 +01:00
|
|
|
const postTemplate = path.resolve('src/templates/Post/index.tsx')
|
2018-10-14 02:25:24 +02:00
|
|
|
|
|
|
|
// Create Post pages
|
|
|
|
posts.forEach(post => {
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.generateRedirectPages = createRedirect => {
|
|
|
|
redirects.forEach(({ f, t }) => {
|
|
|
|
createRedirect({
|
|
|
|
fromPath: f,
|
|
|
|
redirectInBrowser: true,
|
|
|
|
toPath: t
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|