This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
103 lines (89 loc) · 2.54 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const path = require("path")
const slash = require("slash")
const { paginate } = require("gatsby-awesome-pagination")
module.exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`./src/templates/blog.js`)
const postListTemplate = path.resolve("./src/templates/post-list.js")
const postListCatTemplate = path.resolve("./src/templates/post-list-cat.js")
const res = await graphql(`
query {
allContentfulBlogPost {
edges {
node {
id
slug
publishedDate(fromNow: true)
catetory {
catTitle
id
catSlug
}
excerpt
}
}
}
allContentfulCategory {
totalCount
edges {
node {
id
catTitle
catSlug
}
}
}
}
`)
// Check for errors
if (res.errors) {
throw new Error(res.errors)
}
const { allContentfulCategory, allContentfulBlogPost } = res.data
// create archive pages for each category
allContentfulCategory.edges.forEach(catEdge => {
// filter out the posts that belongs tot the current category
const filteredPosts = allContentfulBlogPost.edges.filter(
el => el.node.catetory.catSlug === catEdge.node.catSlug
)
if (filteredPosts.length > 0) {
paginate({
createPage,
items: filteredPosts,
itemsPerPage: 3,
component: postListCatTemplate,
pathPrefix: `/blog/${catEdge.node.catSlug}`,
context: {
catId: catEdge.node.id,
catName: catEdge.node.Title,
catSlug: catEdge.node.catSlug,
catCount: catEdge.node.count,
categories: allContentfulCategory.edges,
},
})
}
})
// end create archive pages for each category
allContentfulBlogPost.edges.forEach(edge => {
// Basic Blog Post Creation
createPage({
path: `/blog/${edge.node.slug}`,
component: blogPostTemplate,
context: {
slug: edge.node.slug,
},
})
//blog pagination start
const posts = allContentfulBlogPost.edges
const pathPrefix = ({ pageNumber }) =>
pageNumber === 0 ? "/blogs" : "/blog/page"
paginate({
createPage,
items: posts,
itemsPerPage: 5,
component: postListTemplate,
pathPrefix: pathPrefix,
})
//blog pagination end
})
}