-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
77 lines (69 loc) · 1.78 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
const path = require(`path`);
const indexEntry = (index, tag, entry) => {
if (!index[tag]) {
index[tag] = [];
}
index[tag].push(entry);
};
// Implement the Gatsby API “createPages”. This is
// called after the Gatsby bootstrap is finished so you have
// access to any information necessary to programmatically
// create pages.
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const tilTemplate = path.resolve('./src/templates/TIL.js');
const tagIndexTemplate = path.resolve('./src/templates/tagIndex.js');
return graphql(
`
{
allContentfulTil {
edges {
node {
slug
tags
title
subtitle
}
}
}
}
`
).then(result => {
if (result.errors) {
console.error(result.errors);
throw new Error(result.errors[0]);
}
const entries = result.data.allContentfulTil.edges;
const tagIndex = {};
entries.forEach((entry, index) => {
// Add this page to the tag index
entry.node.tags.forEach(tag => {
indexEntry(tagIndex, tag, entry.node.slug);
});
// Create the actual entry page
createPage({
path: `/til/${entry.node.slug}`,
component: tilTemplate,
context: {
slug: entry.node.slug,
title: entry.node.title,
description: entry.node.subtitle,
},
});
});
Object.keys(tagIndex).forEach(tag => {
createPage({
path: `/tag/${tag}`,
component: tagIndexTemplate,
context: {
title: `All '${tag} tags`,
tag: tag,
pages: tagIndex[tag],
},
});
});
});
};
exports.onCreateNode = ({ node }) => {
console.log(node.internal.type);
};