2022-01-09 21:12:31 +01:00
|
|
|
import { GatsbyNode } from "gatsby";
|
|
|
|
|
|
|
|
import * as constants from "./constants";
|
|
|
|
import * as queries from "./queries";
|
|
|
|
import * as utils from "./utils";
|
|
|
|
|
|
|
|
type CreateWithPagination = (parameters: {
|
2022-04-16 16:25:55 +02:00
|
|
|
limit: number;
|
2022-01-09 21:12:31 +01:00
|
|
|
group?: string;
|
|
|
|
template: string;
|
|
|
|
total: number;
|
|
|
|
page: number;
|
|
|
|
path: string;
|
|
|
|
}) => void;
|
|
|
|
|
|
|
|
const getPaginationPath = (basePath: string, page: number): string =>
|
2022-04-16 16:25:55 +02:00
|
|
|
[basePath === "/" ? "" : basePath, "page", page].join("/");
|
2022-01-09 21:12:31 +01:00
|
|
|
|
|
|
|
const createPages: GatsbyNode["createPages"] = async ({ graphql, actions }) => {
|
|
|
|
const { createPage } = actions;
|
|
|
|
|
|
|
|
createPage({
|
|
|
|
path: constants.routes.notFoundRoute,
|
|
|
|
component: constants.templates.notFoundTemplate,
|
|
|
|
context: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
createPage({
|
|
|
|
path: constants.routes.tagsListRoute,
|
|
|
|
component: constants.templates.tagsTemplate,
|
|
|
|
context: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
createPage({
|
|
|
|
path: constants.routes.categoriesListRoute,
|
|
|
|
component: constants.templates.categoriesTemplate,
|
|
|
|
context: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const pages = await queries.pagesQuery(graphql);
|
|
|
|
|
2022-10-08 11:28:35 +02:00
|
|
|
const { pageTemplate, postTemplate } = constants.templates;
|
|
|
|
|
2022-04-24 18:21:11 +02:00
|
|
|
pages.forEach((edge) => {
|
2022-01-09 21:12:31 +01:00
|
|
|
const { node } = edge;
|
|
|
|
|
2022-10-08 11:28:35 +02:00
|
|
|
if (node?.fields?.slug) {
|
|
|
|
switch (node?.frontmatter?.template) {
|
|
|
|
case "page":
|
|
|
|
createPage({
|
|
|
|
path: node.fields.slug,
|
|
|
|
component: pageTemplate,
|
|
|
|
context: { slug: node.fields.slug },
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "post":
|
|
|
|
createPage({
|
|
|
|
path: node.fields.slug,
|
|
|
|
component: postTemplate,
|
|
|
|
context: { slug: node.fields.slug, readingTime: node?.fields?.readingTime },
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2022-01-09 21:12:31 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const createWithPagination: CreateWithPagination = ({
|
|
|
|
group,
|
|
|
|
template,
|
|
|
|
page,
|
|
|
|
path,
|
|
|
|
total,
|
2022-04-16 16:25:55 +02:00
|
|
|
limit,
|
2022-01-09 21:12:31 +01:00
|
|
|
}) => {
|
|
|
|
createPage({
|
|
|
|
component: template,
|
|
|
|
path: page === 0 ? path : getPaginationPath(path, page),
|
|
|
|
context: {
|
|
|
|
group,
|
2022-04-16 16:25:55 +02:00
|
|
|
limit,
|
|
|
|
offset: page * limit,
|
2022-01-09 21:12:31 +01:00
|
|
|
pagination: {
|
|
|
|
currentPage: page,
|
2022-09-17 23:16:13 +02:00
|
|
|
prevPagePath: page <= 1 ? path : getPaginationPath(path, utils.decrement(page)),
|
2022-01-09 21:12:31 +01:00
|
|
|
nextPagePath: getPaginationPath(path, utils.increment(page)),
|
|
|
|
hasNextPage: page !== utils.decrement(total),
|
|
|
|
hasPrevPage: page !== 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const categories = await queries.categoriesQuery(graphql);
|
|
|
|
const metadata = await queries.metadataQuery(graphql);
|
|
|
|
const postsLimit = metadata?.postsLimit ?? 1;
|
|
|
|
|
2022-04-24 18:21:11 +02:00
|
|
|
categories.forEach((category) => {
|
2022-01-09 21:12:31 +01:00
|
|
|
const total = Math.ceil(category.totalCount / postsLimit);
|
|
|
|
const path = utils.concat(
|
|
|
|
constants.routes.categoryRoute,
|
|
|
|
"/",
|
2022-09-17 23:16:13 +02:00
|
|
|
utils.toKebabCase(category.fieldValue)
|
2022-01-09 21:12:31 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
for (let page = 0; page < total; page += 1) {
|
|
|
|
createWithPagination({
|
2022-04-16 16:25:55 +02:00
|
|
|
limit: postsLimit,
|
2022-01-09 21:12:31 +01:00
|
|
|
group: category.fieldValue,
|
2022-04-16 16:25:55 +02:00
|
|
|
template: constants.templates.categoryTemplate,
|
2022-01-09 21:12:31 +01:00
|
|
|
total,
|
|
|
|
page,
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const tags = await queries.tagsQuery(graphql);
|
|
|
|
|
2022-04-24 18:21:11 +02:00
|
|
|
tags.forEach((tag) => {
|
2022-09-17 23:16:13 +02:00
|
|
|
const path = utils.concat(constants.routes.tagRoute, "/", utils.toKebabCase(tag.fieldValue));
|
2022-01-09 21:12:31 +01:00
|
|
|
|
|
|
|
const total = Math.ceil(tag.totalCount / postsLimit);
|
|
|
|
|
|
|
|
for (let page = 0; page < total; page += 1) {
|
|
|
|
createWithPagination({
|
2022-04-16 16:25:55 +02:00
|
|
|
limit: postsLimit,
|
2022-01-09 21:12:31 +01:00
|
|
|
group: tag.fieldValue,
|
2022-04-16 16:25:55 +02:00
|
|
|
template: constants.templates.tagTemplate,
|
2022-01-09 21:12:31 +01:00
|
|
|
total,
|
|
|
|
page,
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const path = constants.routes.indexRoute;
|
2022-04-16 16:25:55 +02:00
|
|
|
const template = constants.templates.indexTemplate;
|
2022-01-09 21:12:31 +01:00
|
|
|
const posts = await queries.postsQuery(graphql);
|
2022-04-16 16:25:55 +02:00
|
|
|
const total = Math.ceil((posts?.edges?.length ?? 0) / postsLimit);
|
2022-01-09 21:12:31 +01:00
|
|
|
|
|
|
|
for (let page = 0; page < total; page += 1) {
|
|
|
|
createWithPagination({
|
2022-04-16 16:25:55 +02:00
|
|
|
limit: postsLimit,
|
|
|
|
template,
|
2022-01-09 21:12:31 +01:00
|
|
|
total,
|
|
|
|
page,
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export { createPages };
|