-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgatsby-node.js
93 lines (87 loc) · 2.73 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`);
const slugify = require('slugify');
exports.createPages = ({ graphql, actions }) => {
// createPage is a built in action,
// available to all gatsby-node exports
const { createPage, createRedirect } = 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
data {
Slug
Publish
Other_names
}
}
}
}
}
`);
// For each path, create page and choose a template.
// values in context Object are available in that page's query
result.data.allAirtable.edges.forEach(({ node }) => {
let template;
let pathPrefix;
switch (node.table) {
case 'Components':
template = `./src/templates/component-template.js`;
pathPrefix = 'components/';
break;
default:
template = '';
pathPrefix = '';
}
if (template && pathPrefix && node.data.Publish === true) {
createPage({
path: `${pathPrefix}${node.data.Slug}`,
component: path.resolve(template),
context: {
Slug: node.data.Slug,
},
});
// Create a redirect away from all the other names to the canonical
// version of the page
if (
node.table === 'Components' &&
node.data.Other_names !== null &&
node.data.Other_names.length > 0
) {
const otherNames = node.data.Other_names.split(',');
otherNames.forEach((otherName) => {
const slugifiedOtherName = slugify(otherName.trim(), {
replacement: '-',
lower: true,
});
// Ideally we'd also check whether there is an existing page at that
// URL, but Netlify seems to only trigger redirects on 404s so we
// can get away without it
if (slugifiedOtherName.length > 0) {
/* eslint-disable */
console.log(
`Creating redirect from /${pathPrefix}${slugifiedOtherName}/ to /${pathPrefix}${node.data.Slug}/`
);
/* eslint-enable */
createRedirect({
fromPath: `/${pathPrefix}${slugifiedOtherName}/`,
toPath: `/${pathPrefix}${node.data.Slug}/`,
isPermanent: false,
});
}
});
}
}
});
resolve();
});
};