-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
114 lines (102 loc) · 3.07 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
*/
/**
* @type {import('gatsby').GatsbyNode['createPages']}
*/
const path = require('path');
const _ = require("lodash")
// exports.onCreateNode = ({ node, actions }) => {
// const { createNodeField } = actions;
// if (node.internal.type === 'MarkdownRemark') {
// const slug = path.basename(node.fileAbsolutePath, '.md');
// createNodeField({
// node,
// name: 'slug',
// value: slug,
// });
// }
// };
exports.createPages = async ({ graphql, actions }) => {
const templates = {
post: path.resolve('./src/templates/blog-post.js'),
postList: path.resolve('./src/templates/blog-list.js'),
tagList: path.resolve('./src/templates/blog-tags.js')
}
const { createPage } = actions;
const locales = ["en", "pl", "no"];
await Promise.all(
locales.map(async (locale) => {
const response = await graphql(`
query NodeQuery($locale: String){
posts: allMarkdownRemark (
sort: { frontmatter: { date: DESC }}
limit: 1000
filter: {frontmatter: {slug: {ne: null}, locale: {eq: $locale}}}
){
edges {
node {
frontmatter {
slug
locale
}
}
}
}
tags: allMarkdownRemark(limit: 1000) {
group(field: { frontmatter: { tags: SELECT } }){
fieldValue
}
}
}`, {locale: locale});
if (response.errors) return Promise.reject(response.errors);
const posts = response.data.posts.edges
const prefix = `/${locale}`;
["contact", "about", "repos"].forEach(page => {
createPage({
path: `${prefix}/${page}`,
component: path.resolve(`./src/templates/${page}.js`),
context: { locale: locale }
});
})
posts.forEach((edge) => {
createPage({
path: `${prefix}/blog/${edge.node.frontmatter.slug}`,
component: templates.post,
context: {
locale: locale,
slug: edge.node.frontmatter.slug,
},
});
});
const tags = response.data.tags.group
tags.forEach(tag => {
createPage({
path: `${prefix}/blog/tags/${tag.fieldValue}/`,
component: templates.tagList,
context: {
locale: locale,
tag: tag.fieldValue,
},
})
})
const postsPerPage = 10
const numberOfPages = Math.ceil(posts.length / postsPerPage)
Array.from({ length: numberOfPages }).forEach((_, index) => {
createPage({
path: index === 0 ? `${prefix}/blog` : `${prefix}/blog/${index + 1}`,
component: templates.postList,
context: {
locale: locale,
limit: postsPerPage,
skip: index * postsPerPage,
numberOfPages: numberOfPages,
currentPage: index + 1,
},
})
})
})
)
}