-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
152 lines (135 loc) · 4.4 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const { createFilePath } = require('gatsby-source-filesystem');
const { fmImagesToRelative } = require('gatsby-remark-relative-images');
// Modify nodes that are created by plugins.
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
// Ensure frontmatter images are usable.
fmImagesToRelative(node);
if (node.internal.type === `MarkdownRemark`) {
// Add the collection to our node so it can be used in querying.
const collection = getNode(node.parent).sourceInstanceName;
createNodeField({
node,
name: 'collection',
value: collection,
});
// Add slug information based on the collection.
const slugPrefix = {
sessions: '/sessions',
speakers: '/speakers',
pages: '',
};
if (slugPrefix[collection] !== undefined) {
createNodeField({
name: `slug`,
node,
value: `${slugPrefix[collection]}${createFilePath({ node, getNode })}`,
});
}
}
};
// Transform the session speakers frontmatter field into a relation.
// We use sourceNodes instead of onCreateNode because at this time plugins
// will have created all nodes already and we can link both speakers and
// sessions.
exports.sourceNodes = ({ actions, getNodes, getNode }) => {
const { createNodeField } = actions;
const sessionsOfSpeakers = {};
const speakersOfSessions = {}; // reverse index.
// Create a helper method to find the speaker node.
const getSpeakerByName = name =>
getNodes().find(
node2 =>
node2.internal.type === `MarkdownRemark` &&
node2.fields.collection === 'speakers' &&
node2.frontmatter.name === name
);
// Iterate through all session markdown nodes to find the links between
// sessions and speakers and vice versa.
getNodes()
.filter(
node =>
node.internal.type === `MarkdownRemark` &&
node.fields.collection === 'sessions'
)
.forEach(node => {
if (node.frontmatter.speakers) {
// Normalise the speakers to an array.
const speakerNodes =
node.frontmatter.speakers instanceof Array
? node.frontmatter.speakers.map(getSpeakerByName)
: [getSpeakerByName(node.frontmatter.speakers)];
speakerNodes
// Filter all undefined values.
.filter(speakerNode => speakerNode)
// Iterate through the speakerNodes to register the relationship.
.forEach(speakerNode => {
if (!(speakerNode.id in sessionsOfSpeakers)) {
sessionsOfSpeakers[speakerNode.id] = [];
}
sessionsOfSpeakers[speakerNode.id].push(node.id);
if (!(node.id in speakersOfSessions)) {
speakersOfSessions[node.id] = [];
}
speakersOfSessions[node.id].push(speakerNode.id);
});
}
});
// With all relationships defined, create the fields.
// createNodeField will create the node relationship based on the id we found.
// First for the sessions on the speaker content.
Object.entries(sessionsOfSpeakers).forEach(
([speakerNodeId, sessionNodeIds]) => {
createNodeField({
node: getNode(speakerNodeId),
name: `sessions`,
value: sessionNodeIds,
});
}
);
// Then for the speakers of the session content.
Object.entries(speakersOfSessions).forEach(
([sessionNodeId, speakerNodeIds]) => {
createNodeField({
node: getNode(sessionNodeId),
name: `speakers`,
value: speakerNodeIds,
});
}
);
};
// Create pages that come directly form Markdown files. These are different
// than the dynamic overview pages in the src/pages/ folder.
exports.createPages = async function({ actions, graphql }) {
const { data } = await graphql(`
query {
allPagesConnection: allMarkdownRemark(
filter: { fields: { collection: { eq: "pages" } } }
) {
edges {
node {
id
fields {
slug
}
}
}
}
}
`);
// Create all blog post pages.
data.allPagesConnection.edges.forEach(edge => {
const id = edge.node.id;
const slug = edge.node.fields.slug;
actions.createPage({
path: slug,
component: require.resolve('./src/components/templates/Page.js'),
context: { id },
});
});
};