forked from nature-of-code/noc-book-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-pages.js
48 lines (42 loc) · 1022 Bytes
/
create-pages.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
const path = require('path');
module.exports = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allBookSection {
edges {
previous {
id
}
node {
slug
id
}
next {
id
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query');
}
// Create a page for each sections
const sections = result.data.allBookSection.edges;
if (sections.length > 0) {
sections.forEach(({ previous, node, next }) => {
const previousId = previous === null ? null : previous.id;
const nextId = next === null ? null : next.id;
createPage({
path: `/${node.slug}/`,
component: path.resolve(`./src/layouts/ChapterLayout.js`),
context: {
id: node.id,
previousId,
nextId,
},
});
});
}
};