-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
101 lines (68 loc) · 2.29 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
*/
const fs = require("fs");
const parse = require("./src/utility/stdParser.js");
const clean = require('./src/utility/stdCleaner.js');
function slugify(name) {
return name.replace(/ /g, '-').replace(/(?![a-zA-Z\-])/g, '').toLowerCase();
}
// exports.onPostBuild = ({ page, actions }) => {
// let files = fs.readdirSync("public/standards");
// files = files.filter(file => !file.endsWith('.html'))
// console.log(files);
// }
/**
* @type {import('gatsby').GatsbyNode['createPages']}
*/
exports.createPages = ({ actions }) => {
const standards = fs.readdirSync('./standards');
const { createPage } = actions
let standardsFileData = [];
let standardCategories = {};
standards.forEach((standard, index) => {
const standardData = fs.readFileSync('./standards/' + standard, 'utf-8');
const stdData = parse(standardData);
const cleanedData = clean(stdData);
cleanedData.tags.forEach(tag => {
if (standardCategories[tag] == undefined) {
standardCategories[tag] = [];
}
standardCategories[tag].push(cleanedData);
})
standardsFileData.push(cleanedData);
createPage({
path: `/standards/${cleanedData.slug}`,
component: require.resolve("./src/templates/standard.js"),
context: { ...cleanedData, index },
defer: false,
})
createPage({
path: `/standards/${cleanedData.slug}/html`,
component: require.resolve("./src/templates/standardHtml.js"),
context: { ...cleanedData, index },
defer: false,
})
// Todo: Generate PDF Versions of Each Standard
})
Object.keys(standardCategories).forEach(category => {
const values = standardCategories[category];
const slugifiedCategory = slugify(category);
createPage({
path: `/categories/${slugifiedCategory}`,
component: require.resolve("./src/templates/category.js"),
context: { standards: values, slug: slugifiedCategory, category },
defer: false,
})
})
fs.writeFileSync('./data/standards.json', JSON.stringify(standardsFileData, null, 4));
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
node: {
fs: 'empty'
}
})
}