-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
87 lines (74 loc) · 2.09 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
const dotenv = require('dotenv');
const slugify = require('voca/slugify');
const ImgixClient = require('imgix-core-js');
dotenv.config({ path: '.env' });
const imgix = new ImgixClient({
domain: 'cdn.sunny.app',
secureURLToken: process.env.IMGIX_SECURE_URL_TOKEN,
});
const getCdnUrl = (url) => imgix.buildURL(url, { w: 500 });
const normalizeAirtableData = (data) => ({
id: data.id,
name: data.Name,
description: data.Description,
url: data.URL,
categories: data.Category || [],
date: data.Date,
image: Array.isArray(data.Image) ? getCdnUrl(data.Image[0].url) : null,
});
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'Airtable') {
const data = normalizeAirtableData(node.data);
Object.entries(data).forEach(([name, value]) => {
createNodeField({ node, name, value });
});
}
};
exports.createPages = async ({ actions: { createPage }, graphql }) => {
const results = await graphql(`
{
allAirtable(
filter: { table: { eq: "Resources" } }
sort: { order: DESC, fields: data___Date }
) {
edges {
node {
data {
Category
Name
Description
URL
Image {
url
}
}
}
}
}
}
`);
const pagesByCategory = {};
results.data.allAirtable.edges.forEach((edge) => {
const { categories, name, description, url, image } = normalizeAirtableData(
edge.node.data
);
if (categories.length === 0) return;
categories.forEach((category) => {
if (!pagesByCategory[category]) pagesByCategory[category] = [];
pagesByCategory[category].push({ name, description, url, image });
});
});
Object.entries(pagesByCategory).forEach(([category, items]) => {
const slug = slugify(category.toLowerCase());
createPage({
path: `/${slug}`,
component: require.resolve('./src/components/ResourceLayout.js'),
context: {
items,
category,
slug,
},
});
});
};