-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
56 lines (53 loc) · 1.36 KB
/
gatsby-node.js
File metadata and controls
56 lines (53 loc) · 1.36 KB
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
const path = require(`path`);
const cleanName = function(value) {
return String(value)
.toLowerCase()
.replace(/[^\w]/g, '');
};
exports.createPages = ({ graphql, actions }) => {
// createPage is a built in action,
// available to all gatsby-node exports
const { createPage } = actions;
return new Promise(async resolve => {
// we need the table name (e.g. "Sections")
// as well as the unique path for each Page/Section.
const result = await graphql(`
{
allAirtable {
edges {
node {
table
id
data {
Name
Title
Cover_Image {
filename
}
}
}
}
}
}
`);
// For each path, create a page and decide which template to use.
// values inside the context Object are available in the page's query
result.data.allAirtable.edges.forEach(({ node }) => {
const isBook = node.table === 'books' && node.data.Title;
const isAuthor = node.table === 'authors' && node.data.Name;
if (isBook || isAuthor) {
createPage({
path: isAuthor ? `/author/${cleanName(node.data.Name)}` : `/book/${cleanName(node.data.Title)}`,
component: isAuthor
? path.resolve(`./src/templates/author-template.js`)
: path.resolve(`./src/templates/book-template.js`),
context: {
id: node.id,
data: node.data
}
});
}
});
resolve();
});
};