-
Notifications
You must be signed in to change notification settings - Fork 21
/
gatsby-node.js
81 lines (72 loc) · 1.96 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
const path = require(`path`);
const _ = require(`lodash`);
const { getKontentItemNodeTypeName } = require('@kontent-ai/gatsby-source');
const articleTypeIdentifier = `article`;
exports.onCreateNode = ({ node, actions: { createNodeField } }) => {
let withDetailView = false;
let templateName;
const type = _.get(node, `internal.type`, '');
if (type.startsWith(getKontentItemNodeTypeName(articleTypeIdentifier))) {
templateName = `article`;
withDetailView = true;
} else {
return;
}
if (withDetailView) {
createNodeField({
node,
name: `templateName`,
value: templateName
});
createNodeField({
node,
name: `slug`,
value: node.elements.url_pattern.value
});
}
createNodeField({
node,
name: `language`,
value: node.system.language
});
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve) => {
graphql(`
{
allKontentItemArticle (filter: {preferred_language: {eq: "en-US"}}) {
nodes {
fields {
templateName
slug
language
}
elements {
title {
name
value
}
}
}
}
}`).then(result => {
const union = result.data.allKontentItemArticle.nodes;
union.forEach((node) => {
if (_.has(node, `fields.templateName`) && !_.isNil(node.fields.templateName)) {
createPage({
path: `${node.fields.templateName}/${node.fields.slug}`,
component: path.resolve(`./src/templates/${node.fields.templateName}.js`),
context: {
// Data passed to context is available in page queries as GraphQL variables.
templateName: node.fields.templateName,
slug: node.fields.slug,
language: node.fields.language
},
});
}
});
resolve();
});
});
};