2018-11-09 18:08:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const path = require('path');
|
|
|
|
const siteConfig = require('../../config.js');
|
|
|
|
|
|
|
|
module.exports = async (graphql, actions) => {
|
|
|
|
const { createPage } = actions;
|
2018-11-11 12:19:06 +01:00
|
|
|
const { postsPerPage } = siteConfig;
|
2018-11-09 18:08:48 +01:00
|
|
|
|
|
|
|
const result = await graphql(`
|
|
|
|
{
|
|
|
|
allMarkdownRemark(
|
2018-11-11 17:32:51 +01:00
|
|
|
filter: { frontmatter: { template: { eq: "post" }, draft: { ne: true } } }
|
2018-11-09 18:08:48 +01:00
|
|
|
) {
|
|
|
|
group(field: frontmatter___category) {
|
|
|
|
fieldValue
|
|
|
|
totalCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
_.each(result.data.allMarkdownRemark.group, (category) => {
|
2018-11-11 12:19:06 +01:00
|
|
|
const numPages = Math.ceil(category.totalCount / postsPerPage);
|
|
|
|
const categorySlug = `/category/${_.kebabCase(category.fieldValue)}`;
|
2018-11-09 18:08:48 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < numPages; i += 1) {
|
|
|
|
createPage({
|
2018-11-11 12:19:06 +01:00
|
|
|
path: i === 0 ? categorySlug : `${categorySlug}/page/${i}`,
|
2018-11-09 18:08:48 +01:00
|
|
|
component: path.resolve('./src/templates/category-template.js'),
|
|
|
|
context: {
|
|
|
|
category: category.fieldValue,
|
2018-11-11 12:19:06 +01:00
|
|
|
currentPage: i,
|
|
|
|
postsLimit: postsPerPage,
|
|
|
|
postsOffset: i * postsPerPage,
|
|
|
|
prevPagePath: i <= 1 ? categorySlug : `${categorySlug}/page/${i - 1}`,
|
|
|
|
nextPagePath: `/${categorySlug}/page/${i + 1}`,
|
|
|
|
hasPrevPage: i !== 0,
|
|
|
|
hasNextPage: i !== numPages - 1
|
2018-11-09 18:08:48 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|