forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateBlogPages.js
More file actions
43 lines (33 loc) · 1002 Bytes
/
createBlogPages.js
File metadata and controls
43 lines (33 loc) · 1002 Bytes
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
const { iterateEdges } = require('./createPageUtils');
function paginateBlogEdges(array, pageSize) {
return array.reduce((acc, val, i) => {
const idx = Math.floor(i / pageSize);
const page = acc[idx] || (acc[idx] = []);
page.push(val);
return acc;
}, []);
}
function createMarkdownPages(blogEdges) {
const blogPages = iterateEdges(blogEdges, node => {
const {
fields: { readingTime, date },
} = node;
return { readingTime, date };
});
const blogPosts = blogEdges.map(({ node }) => ({
node: { frontmatter: node.frontmatter, fields: node.fields },
}));
const getBlogPostsByCategory = cat =>
cat && cat.length
? blogPosts.filter(({ node }) => node.fields.categoryName === cat)
: blogPosts;
const getPaginatedPosts = category =>
paginateBlogEdges(getBlogPostsByCategory(category), 6);
return {
blogPages,
blogPosts,
getPaginatedPosts,
getBlogPostsByCategory,
};
}
module.exports = createMarkdownPages;