mirror of
https://github.com/mastermindzh/rickvanlieshout.com
synced 2025-07-26 04:04:27 +02:00
Add pagination
This commit is contained in:
@@ -6,6 +6,7 @@ const siteConfig = require('../../config.js');
|
||||
|
||||
module.exports = async (graphql, actions) => {
|
||||
const { createPage } = actions;
|
||||
const { postsPerPage } = siteConfig;
|
||||
|
||||
const result = await graphql(`
|
||||
{
|
||||
@@ -21,17 +22,22 @@ module.exports = async (graphql, actions) => {
|
||||
`);
|
||||
|
||||
_.each(result.data.allMarkdownRemark.group, (category) => {
|
||||
const numPages = Math.ceil(category.totalCount / siteConfig.postsPerPage);
|
||||
const numPages = Math.ceil(category.totalCount / postsPerPage);
|
||||
const categorySlug = `/category/${_.kebabCase(category.fieldValue)}`;
|
||||
|
||||
for (let i = 0; i < numPages; i += 1) {
|
||||
createPage({
|
||||
path: i === 0 ? `/category/${_.kebabCase(category.fieldValue)}` : `/category/${_.kebabCase(category.fieldValue)}/page/${i}`,
|
||||
path: i === 0 ? categorySlug : `${categorySlug}/page/${i}`,
|
||||
component: path.resolve('./src/templates/category-template.js'),
|
||||
context: {
|
||||
page: i,
|
||||
category: category.fieldValue,
|
||||
limit: siteConfig.postsPerPage,
|
||||
skip: i * siteConfig.postsPerPage,
|
||||
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
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -14,16 +14,21 @@ module.exports = async (graphql, actions) => {
|
||||
}
|
||||
`);
|
||||
|
||||
const numPages = Math.ceil(result.data.allMarkdownRemark.totalCount / siteConfig.postsPerPage);
|
||||
const { postsPerPage } = siteConfig;
|
||||
const numPages = Math.ceil(result.data.allMarkdownRemark.totalCount / postsPerPage);
|
||||
|
||||
for (let i = 0; i < numPages; i += 1) {
|
||||
createPage({
|
||||
path: i === 0 ? '/' : `/page/${i}`,
|
||||
component: path.resolve('./src/templates/index-template.js'),
|
||||
context: {
|
||||
page: i,
|
||||
limit: siteConfig.postsPerPage,
|
||||
skip: i * siteConfig.postsPerPage,
|
||||
currentPage: i,
|
||||
postsLimit: postsPerPage,
|
||||
postsOffset: i * postsPerPage,
|
||||
prevPagePath: i <= 1 ? '/' : `/page/${i - 1}`,
|
||||
nextPagePath: `/page/${i + 1}`,
|
||||
hasPrevPage: i !== 0,
|
||||
hasNextPage: i !== numPages - 1
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ const siteConfig = require('../../config.js');
|
||||
|
||||
module.exports = async (graphql, actions) => {
|
||||
const { createPage } = actions;
|
||||
const { postsPerPage } = siteConfig;
|
||||
|
||||
const result = await graphql(`
|
||||
{
|
||||
@@ -21,17 +22,22 @@ module.exports = async (graphql, actions) => {
|
||||
`);
|
||||
|
||||
_.each(result.data.allMarkdownRemark.group, (tag) => {
|
||||
const numPages = Math.ceil(tag.totalCount / siteConfig.postsPerPage);
|
||||
const numPages = Math.ceil(tag.totalCount / postsPerPage);
|
||||
const tagSlug = `/tag/${_.kebabCase(tag.fieldValue)}`;
|
||||
|
||||
for (let i = 0; i < numPages; i += 1) {
|
||||
createPage({
|
||||
path: i === 0 ? `/tag/${_.kebabCase(tag.fieldValue)}` : `/tag/${_.kebabCase(tag.fieldValue)}/page/${i}`,
|
||||
path: i === 0 ? tagSlug : `${tagSlug}/page/${i}`,
|
||||
component: path.resolve('./src/templates/tag-template.js'),
|
||||
context: {
|
||||
page: i,
|
||||
tag: tag.fieldValue,
|
||||
limit: siteConfig.postsPerPage,
|
||||
skip: i * siteConfig.postsPerPage,
|
||||
currentPage: i,
|
||||
postsLimit: postsPerPage,
|
||||
postsOffset: i * postsPerPage,
|
||||
prevPagePath: i <= 1 ? tagSlug : `${tagSlug}/page/${i - 1}`,
|
||||
nextPagePath: `${tagSlug}/page/${i + 1}`,
|
||||
hasPrevPage: i !== 0,
|
||||
hasNextPage: i !== numPages - 1
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user