-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
160 lines (149 loc) · 4.06 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const path = require("path");
const slugify = require("./src/util/utilFunc");
const authors = require("./src/util/authors");
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === "MarkdownRemark") {
const slugTitle = slugify(node.frontmatter.title);
createNodeField({
node,
name: "slug",
value: slugTitle,
});
}
};
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions;
const template = {
single: path.resolve("src/templates/Singlepost.js"),
tags: path.resolve("src/templates/tags.js"),
tag: path.resolve("src/templates/SingleTag.js"),
pagination: path.resolve("src/templates/PostPage.js"),
tagPagination: path.resolve("src/templates/tagPagination.js"),
// team: path.resolve("src/templates/team.js"),
authorPost: path.resolve("src/templates/AuthorPage.js"),
};
const { data } = await graphql(`
{
allMarkdownRemark {
edges {
node {
frontmatter {
author
tags
}
fields {
slug
}
}
}
}
}
`);
if (data) {
const posts = data.allMarkdownRemark.edges;
// single post page
posts.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: template.single,
context: {
slug: node.fields.slug,
imageurl: authors.find(
(a) =>
a.name.toLowerCase() === node.frontmatter.author.toLowerCase()
).imageurl,
},
});
});
// author page with corresponding posts
authors.forEach((author) => {
createPage({
path: `/author/${slugify(author.name)}`,
component: template.authorPost,
context: {
authorName: author.name,
imageurl: author.imageurl,
},
});
});
// all tags page
let tags = [];
posts.forEach(({ node }) => {
if (node && node.frontmatter.tags) {
tags = tags.concat(node.frontmatter.tags);
}
});
const uniqueTags = [...new Set(tags)];
let tagCount = {};
tags.forEach((tag) => {
tagCount[tag] = (tagCount[tag] || 0) + 1;
});
createPage({
path: "/tags",
component: template.tags,
context: {
tags: uniqueTags,
tagCount,
},
});
// single tags page
uniqueTags.forEach((tag) => {
createPage({
path: `/tag/${slugify(tag)}`,
component: template.tag,
context: { tag },
});
});
// pagination for posts
const postsPerPage = 2;
const totalPages = Math.ceil(posts.length / postsPerPage);
Array.from({ length: totalPages }).map((_, index) => {
const currentPage = index + 1;
if (index === 0) {
return;
} else {
createPage({
path: `/page/${currentPage}`,
component: template.pagination,
context: {
limit: postsPerPage,
skip: index * postsPerPage,
currentPage,
totalPages,
},
});
}
});
// pagination if a same tag appears more than twice
let allTags = {};
tags.forEach((tag) => {
allTags[tag] = (allTags[tag] || 0) + 1;
if (allTags[tag] > 2) {
const tagsPerPage = 2;
const totalTagsPages = Math.ceil(allTags[tag] / tagsPerPage);
Array(totalTagsPages)
.fill()
.map((_, index) => {
const currentPage = index + 1;
if (currentPage === 1) {
return;
} else {
createPage({
path: `/tags/${slugify(tag)}/${currentPage}`,
component: template.tagPagination,
context: {
skip: index * tagsPerPage,
limit: tagsPerPage,
tag,
totalPages: totalTagsPages,
tagCount: allTags[tag],
currentPage,
},
});
}
});
}
});
}
};