-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
63 lines (57 loc) · 1.38 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
const path = require('path');
const sectionTemplate = path.resolve('src/templates/section.js');
const articleTemplate = path.resolve('src/templates/article.js');
exports.createPages = async ({ graphql, actions: { createPage } }) => {
const result = await graphql(`
{
categories: allContentfulKbAppCategory {
nodes {
id
slug
name
articles: kbapparticle {
id
}
}
}
articles: allContentfulKbAppArticle {
nodes {
id
slug
title
category: kbAppCategory {
slug
}
}
}
}
`);
if (result.errors) {
// do something
}
// Create category pages
const withArticles = (category) =>
Array.isArray(category.articles) && category.articles.length > 0;
const categories = result.data.categories.nodes.filter(withArticles);
categories.forEach((category) => {
createPage({
path: `/${category.slug}/`,
component: sectionTemplate,
context: {
id: category.id,
name: category.name,
},
});
});
// Create article pages
result.data.articles.nodes.forEach((article) => {
createPage({
path: `/${article.category.slug}/${article.slug}/`,
component: articleTemplate,
context: {
id: article.id,
name: article.title,
},
});
});
};