forked from keycap-archivist/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
126 lines (121 loc) · 3.15 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
/* eslint-disable no-param-reassign */
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const { createFilePath } = require('gatsby-source-filesystem');
const slugify = require('slugify');
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const slug = (d) => slugify(d, { replacement: '-', remove: /[.:?()'"/]/g, lower: true }).toLowerCase();
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
const nodeSlug = createFilePath({
node,
getNode,
basePath: 'content\\blog',
});
createNodeField({
node,
name: 'slug',
value: `blog${nodeSlug}`,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const makerTpl = require.resolve('./src/layouts/maker.js');
const cwTpl = require.resolve('./src/layouts/cw.js');
const sculptTpl = require.resolve('./src/layouts/sculpt.js');
const blogTpl = path.resolve('./src/layouts/blog-post.js');
/**
* Blog
*/
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`);
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: blogTpl,
context: {
slug: node.fields.slug,
},
});
});
/**
* Artisan catalog
*/
let db = JSON.parse(fs.readFileSync('./src/db/catalog.json'));
// While developing only getting the 2 first makers
if (process.env.TARGET === 'DEV') {
db = [db[0], db[1]];
}
db.forEach((maker) => {
maker.sculpts.forEach((element) => {
element.link = `maker/${slug(maker.name)}/${slug(element.name)}`;
const rng = Math.floor(Math.random() * element.colorways.length);
element.previewImg = element.colorways[rng].img;
});
const makerLightObj = _.cloneDeep(maker);
makerLightObj.sculpts.forEach((s) => {
delete s.colorways;
});
const makerUrl = `maker/${slug(maker.name)}`;
createPage({
path: makerUrl,
component: makerTpl,
context: {
maker: makerLightObj,
type: 'maker',
slug: makerUrl,
},
});
maker.sculpts.forEach((sculpt) => {
// Light maker object
const outMaker = {
...maker,
};
delete outMaker.sculpts;
createPage({
path: sculpt.link,
component: sculptTpl,
context: {
makerUrl,
type: 'sculpt',
sculpt,
maker: outMaker,
slug: sculpt.link,
},
});
sculpt.colorways.forEach((cw) => {
createPage({
path: `${sculpt.link}/${cw.id}`,
component: cwTpl,
context: {
makerUrl,
makerName: maker.name,
sculptName: sculpt.name,
sculptUrl: sculpt.link,
type: 'colorway',
colorway: cw,
slug: `${sculpt.link}/${cw.id}`,
},
});
});
});
});
};