-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
99 lines (96 loc) · 2.51 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
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const ProjectTemplate = require.resolve(`./src/templates/ProjectTemplate.js`);
const result = await graphql(`
{
projects: allFile(
filter: {
relativeDirectory: { regex: "/projects/" }
extension: { eq: "md" }
}
) {
edges {
node {
id
name
relativeDirectory
relativeDirectory
childMarkdownRemark {
rawMarkdownBody
html
frontmatter {
id
tags
title
slug
year
language
street
}
}
}
}
group(field: relativeDirectory) {
edges {
node {
childMarkdownRemark {
id
frontmatter {
slug
language
}
}
relativeDirectory
}
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const languageLinks = result.data.projects.group.reduce((prev, curr) => {
let parentKey = null;
const value = curr.edges.reduce((innerPrev, innerCurr) => {
const val = innerCurr.node.childMarkdownRemark.frontmatter.slug;
const key =
innerCurr.node.childMarkdownRemark.frontmatter.language || "default";
parentKey = innerCurr.node.relativeDirectory;
return {
...innerPrev,
[key]: val,
};
}, {});
const key = parentKey;
return {
...prev,
[key]: value,
};
}, {});
result.data.projects.edges.forEach(({ node }) => {
const locale = node.childMarkdownRemark.frontmatter.language
? node.childMarkdownRemark.frontmatter.language
: "pl";
createPage({
path: node.childMarkdownRemark.frontmatter.slug,
component: ProjectTemplate,
context: {
slug: node.childMarkdownRemark.frontmatter.slug,
locales: languageLinks[node.relativeDirectory],
locale,
},
});
});
};
exports.onCreatePage = ({ page, actions }) => {
const { deletePage } = actions;
if (page.path && page.path.length > 2) {
const routeLocale = page.path.slice(1, 3);
if (page.context.locale && routeLocale !== page.context.locale) {
deletePage(page);
}
}
};