1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-23 01:30:01 +01:00
blog/gatsby/createPages.ts

134 lines
3.4 KiB
TypeScript
Raw Normal View History

import path from 'path'
import config from '../config'
import { Actions } from 'gatsby'
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
]
function getPaginationData(i: number, numPages: number, slug: string) {
2019-11-06 20:44:33 +01:00
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 }
}
export const generatePostPages = (
createPage: Actions['createPage'],
posts: Queries.AllContentQuery['all']['edges'] | undefined
) => {
2018-10-14 02:25:24 +02:00
// Create Post pages
posts?.forEach((post) => {
2018-10-14 02:25:24 +02:00
createPage({
path: `${post.node.fields?.slug}`,
2018-10-14 02:25:24 +02:00
component: postTemplate,
context: {
slug: post.node.fields?.slug,
2019-11-11 01:00:26 +01:00
prev: post.previous && {
title: post.previous.frontmatter?.title,
slug: post.previous.fields?.slug
2019-11-11 01:00:26 +01:00
},
next: post.next && {
title: post.next.frontmatter?.title,
slug: post.next.fields?.slug
2019-11-11 01:00:26 +01:00
}
2018-10-14 02:25:24 +02:00
}
})
})
2020-07-19 13:31:27 +02:00
}
2018-10-14 02:25:24 +02:00
function generateIndexPages(
createPage: Actions['createPage'],
length: number,
slug: string,
template: string,
tag?: string
) {
const numPages = Math.ceil(length / config.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,
limit: config.itemsPerPage,
skip: i * config.itemsPerPage,
2021-03-01 00:03:39 +01:00
numPages: numPages,
2020-07-19 13:31:27 +02:00
currentPageNumber: i + 1,
prevPagePath,
2021-03-16 00:13:01 +01:00
nextPagePath,
...(tag && { tag })
2020-07-19 13:31:27 +02:00
}
})
})
}
2021-03-01 00:03:39 +01:00
// Create paginated archive pages
export const generateArchivePages = (
createPage: Actions['createPage'],
archiveLength: number | undefined
) => {
if (!archiveLength) return
2021-03-01 00:03:39 +01:00
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
export const generatePhotosPages = (
createPage: Actions['createPage'],
photosLength: number | undefined
) => {
if (!photosLength) return
2021-03-01 00:03:39 +01:00
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
export const generateTagPages = (
createPage: Actions['createPage'],
tags: Queries.AllContentQuery['tags']['group'] | undefined
) => {
if (!tags) return
2019-11-06 20:44:33 +01:00
tags.forEach(({ tag, totalCount }) => {
2021-03-01 00:03:39 +01:00
generateIndexPages(
createPage,
totalCount,
`/archive/${tag}/`,
2021-03-16 00:13:01 +01:00
archiveTemplate,
tag || ''
2021-03-01 00:03:39 +01:00
)
2018-10-14 02:25:24 +02:00
})
}
export const generateRedirectPages = (
createRedirect: Actions['createRedirect']
) => {
2018-10-14 02:25:24 +02:00
redirects.forEach(({ f, t }) => {
createRedirect({
fromPath: f,
redirectInBrowser: true,
toPath: t
})
})
}