This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
77 lines (71 loc) · 2.2 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path');
const { allPostsDataQuery } = require('./queries/allPostsData');
const { chunkArray } = require('./helpers/chunkArray');
exports.createPages = async ({ actions, graphql }) => {
const allPostsData = await graphql(allPostsDataQuery);
const allPosts = allPostsData.data.allButterPost.edges.reverse();
const chunkedPosts = chunkArray(allPosts, 3);
const categoryGroups = allPostsData.data.allButterPost.group;
/**
* CATEGORY PAGES
*
* 3 posts per page (unlimited pages per category)
*/
categoryGroups.forEach(cat => {
const categorySlug = cat.fieldValue;
const categoryPosts = cat.nodes.sort((a, b) => a.published < b.published)
const chunkedCategoryPosts = chunkArray(categoryPosts, 3);
chunkedCategoryPosts.forEach((group, index) => {
actions.createPage({
path: `/articles/${categorySlug}/page-${index + 1}`,
component: path.resolve(`./src/components/PostsList/CategoriesPostsList.jsx`),
context: {
skip: index * 3,
maxPageNumber: chunkedCategoryPosts.length,
categorySlug: categorySlug,
},
})
})
})
/**
* LATEST POST PAGES
*
* 3 posts per page
*/
chunkedPosts.forEach((collection, index) => {
actions.createPage({
path: `/articles/page-${index + 1}`,
component: path.resolve(`./src/components/PostsList/PostsList.jsx`),
context: {
skip: index * 3,
maxPageNumber: chunkedPosts.length,
posts: collection
},
})
})
/**
* POST SINGLE PAGES
*
*/
allPosts.forEach(( node, index ) => {
actions.createPage({
path: `/articles/${node.node.slug}`,
component: path.resolve(`./src/components/Post/Post.jsx`),
context: {
post: node.node,
prevPost: index === 0 ? null : allPosts[index - 1].node,
nextPost: index === allPosts.length - 1 ? null : allPosts[index + 1].node,
breadcrumbs: [
{ title: 'Home', path: '/'},
{ title: `Blog`, path: `/articles/page-1`},
{ title: node.node.title, path: null},
],
},
})
})
}