-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
157 lines (139 loc) · 4.08 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const { categories, siteStructure } = require('./src/config');
const dev = process.env.NODE_ENV === 'development';
const flattenSubcategories = ({ id, subcategories = [] }) => {
const subcategoryReducer = (acc, cat) => [
...acc,
...flattenSubcategories(cat),
];
return subcategories.reduce(subcategoryReducer, [id]);
};
const findParentCategories = categoryId => {
const find = (subStructure, idToFind) => {
const { subcategories = [], id } = subStructure;
let result;
if (id === idToFind) return [id];
const categoryContainsId = subcategories.some(c => {
result = find(c, idToFind);
return result.length > 0;
});
return categoryContainsId ? [...result, id] : [];
};
return find({ id: null, subcategories: siteStructure }, categoryId).filter(
c => c !== null
);
};
const findMainCategory = category => {
const path = findParentCategories(category) || [];
return path.length > 0 ? path[path.length - 1] : null;
};
// Netlify didn't know about Array.prototype.flat()
// (we used to be on Netlify)
const flatten = (array, depth = 1) => {
const f = (acc, cur) => {
if (Array.isArray(cur)) {
return [...acc, ...flatten(cur, depth - 1)];
} else {
return [...acc, cur];
}
};
return depth > 0 ? array.reduce(f, []) : array;
};
const allowedCategoryIds = flatten(siteStructure.map(flattenSubcategories))
.concat('operator-guides')
.concat('pilot-day-guides');
// Uncomment to warn about circular dependencies.
//
// const CircularDependencyPlugin = require('circular-dependency-plugin');
// exports.onCreateWebpackConfig = ({
// stage,
// rules,
// loaders,
// plugins,
// actions,
// }) => {
// actions.setWebpackConfig({
// plugins: [
// new CircularDependencyPlugin({
// exclude: /\.cache|node_modules/,
// cwd: process.cwd(),
// }),
// ],
// });
// };
exports.onCreateNode = ({ node, getNode }) => {
if (node.internal.type === `MarkdownRemark`) {
const fileNode = getNode(node.parent);
console.log(`\n`, fileNode.relativePath);
}
};
const createArticle = (createPage, edge) => {
const { fileAbsolutePath, frontmatter } = edge.node;
const { title, slug, updated, category, ingress, published } = frontmatter;
const parent = findMainCategory(category) || category;
if (!title) {
throw new Error(`title missing from file: ${fileAbsolutePath}`);
}
if (!slug) {
throw new Error(`slug missing from file: ${fileAbsolutePath}`);
}
if (!updated) {
throw new Error(`updated date missing from file: ${fileAbsolutePath}`);
}
if (!category) {
throw new Error(`category missing from file: ${fileAbsolutePath}`);
}
if (!ingress) {
throw new Error(`ingress missing from file: ${fileAbsolutePath}`);
}
if (!allowedCategoryIds.includes(category)) {
throw new Error(
`Unknown category: ${category} in file: ${fileAbsolutePath}. Category should be one of: ${allowedCategoryIds.join(
', '
)}`
);
}
if (!dev && !published) {
console.log(`Ignoring private article: ${fileAbsolutePath}`);
return Promise.resolve(null);
}
return createPage({
path: `${parent}/${slug}/`,
component: path.resolve(`./src/templates/ArticlePageTemplate.js`),
// Context will be exposed as variables in the GraphQL query, e.g. $category
context: { slug, category },
});
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return graphql(`
{
allMarkdownRemark {
edges {
node {
fileAbsolutePath
frontmatter {
title
slug
updated
category
ingress
published
# noindex
}
}
}
}
}
`).then(result => {
return Promise.all(
result.data.allMarkdownRemark.edges.map(e => createArticle(createPage, e))
);
});
};