-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
137 lines (129 loc) · 3.73 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require("path");
const chalk = require("chalk");
const generalSettings = require("./_site/settings/general.json");
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"]
}
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
const parent = getNode(node.parent);
if (node.internal.type === "StaticPagesJson" && node.template) {
createNodeField({
node,
name: "locales",
value: node.locales.map(locale => ({
...locale,
path:
locale.language === generalSettings.defaultLanguage
? path.join("/", typeof locale.path === "string" ? locale.path : "")
: path.join(
"/",
locale.language,
typeof locale.path === "string" ? locale.path : ""
)
}))
});
}
if (parent && parent.internal.mediaType === "application/json") {
const name = parent.name;
createNodeField({ node, name: "name", value: name });
}
};
exports.createPages = context => {
return Promise.all([createStaticPages(context)]);
};
function createStaticPages({ graphql, actions }) {
const { createPage } = actions;
return new Promise((resolve, reject) => {
graphql(`
{
settingsJson(fields: { name: { eq: "general" } }) {
fields {
name
}
defaultLanguage
}
allStaticPagesJson {
edges {
node {
template
fields {
name
locales {
language
path
}
}
}
}
}
}
`).then(result => {
const { allStaticPagesJson, settingsJson } = result.data;
const defaultLocale = settingsJson.defaultLanguage;
allStaticPagesJson.edges.forEach(({ node }) => {
const {
template,
fields: { locales }
} = node;
if (locales) {
locales.forEach(locale => {
createPage({
path: locale.path,
component: path.resolve(`./src/templates/${template}.tsx`),
context: {
name: node.fields.name,
locale: locale.language
}
});
});
} else {
console.warn(
chalk.yellow(
`Warning: locales are missing on StaticPage with id ${node.id}`
)
);
}
});
resolve();
});
});
}
exports.setFieldsOnGraphQLNodeType = ({ actions, getNodes, getNode }) => {
const { createNodeField } = actions;
const homeNode = getNodes().find(node => {
return (
node.internal.type === "StaticPagesJson" && node.fields.name === "home"
);
});
if (homeNode) {
homeNode.locales.forEach(homeLocale => {
if (Array.isArray(homeLocale.projects.featuredProjects)) {
const featuredProjectsTitles = homeLocale.projects.featuredProjects.map(
({ project }) => project
);
const featuredProjectsMarkdownNodes = getNodes().filter(
markdownNode =>
markdownNode.internal.type === "MarkdownRemark" &&
markdownNode.fields.collection === "projects" &&
featuredProjectsTitles.includes(markdownNode.frontmatter.title)
);
createNodeField({
node: homeNode,
name: "featuredProjects",
value: featuredProjectsMarkdownNodes
});
}
});
}
};