forked from OpenBeta/open-tacos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
249 lines (232 loc) · 6.42 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const path = require("path");
const slugify = require("slugify");
/**
* Converts the relativePath to a POSIX path.
* This is to unify all paths to one structure for consistent ids
* @param {String} relativePath
* @returns
*/
const convertPathToPOSIX = (relativePath) => {
return relativePath.split(path.sep).join(path.posix.sep);
};
/**
* Remove project's content base dir (__dirname +"/content") from full path and split the rest with String.split("/").
* The output is used for generating page slug and breadcrumbs.
*
* Example:
* ```
* pathTokenizer("/Users/bob/projects/foo/content/usa/washington/seattle/index.md")
* => ["usa", "washington", "seattle"].
*
* Note that the file name 'index.md' is not included.
* ```
* @param {String} absolutePath
*/
const pathTokenizer = (absolutePath) => {
return path
.dirname(absolutePath.replace(__dirname + "/content/", ""))
.split("/");
};
/**
* Slugify each element of `pathTokens` and join them together.
* ```
* slugify_path(["USA", "This has space"]) => 'usa/this-has-space'
* ```
* @param {string[]} pathTokens
*/
const slugify_path = (pathTokens) => {
return pathTokens
.map((s) => slugify(s, { lower: true, strict: true }))
.join("/");
};
exports.onCreateNode = ({ node, getNode, actions }) => {
if (node.internal.type === "Mdx") {
const { createNodeField } = actions;
const parent = getNode(node["parent"]);
const nodeType = parent["sourceInstanceName"];
const markdownFileName = path.posix.parse(parent.relativePath).name;
if (nodeType === "climbing-routes") {
// Computed on the fly based off relative path of the current file
// climbing routes's parent id is the current directory.
const parentId = convertPathToPOSIX(
path.join(path.dirname(parent.relativePath))
);
const pathTokens = pathTokenizer(node.fileAbsolutePath);
pathTokens.push(markdownFileName);
createNodeField({
node,
name: `slug`,
value: `/${slugify_path(pathTokens)}`,
});
createNodeField({
node,
name: `filename`,
value: markdownFileName,
});
createNodeField({
node,
name: `collection`,
value: nodeType,
});
createNodeField({
node,
name: `parentId`,
value: parentId,
});
createNodeField({
node,
name: `pathTokens`,
value: pathTokens,
});
} else if (nodeType === "area-indices") {
// Computed on the fly based off relative path of the current file
// If you looking at an index.md for an area the parent would be the
// index.md of 1 directory level up.
// i.g. Take current path, go up one directory.
const parentId = convertPathToPOSIX(
path.join(path.dirname(parent.relativePath), "..")
);
const pathTokens = pathTokenizer(node.fileAbsolutePath);
const pathId = convertPathToPOSIX(
path.join(path.dirname(parent.relativePath))
);
createNodeField({
node,
name: `slug`,
value: `/${slugify_path(pathTokens)}`,
});
createNodeField({
node,
name: `filename`,
value: markdownFileName,
});
createNodeField({
node,
name: `collection`,
value: nodeType,
});
createNodeField({
node,
name: `parentId`,
value: parentId,
});
createNodeField({
node,
name: `pathId`,
value: pathId,
});
createNodeField({
node,
name: `pathTokens`,
value: pathTokens,
});
}
}
};
exports.createPages = async ({ graphql, actions }) => {
// Query all leaf area index documents
var result = await graphql(`
query {
allMdx(filter: { fields: { collection: { eq: "area-indices" } } }) {
edges {
node {
fields {
slug
pathId
parentId
}
frontmatter {
metadata {
legacy_id
}
}
}
}
}
}
`);
// For every node in "area-indices" create a parentId to child Ids look up
// data structure
const childAreas = {};
result.data.allMdx.edges.map(({ node }) => {
const parentId = node.fields.parentId;
const pathId = node.fields.pathId;
if (childAreas[parentId]) {
childAreas[parentId].push(pathId);
childAreas[parentId] = [...new Set(childAreas[parentId])];
} else {
childAreas[parentId] = [pathId];
}
});
// Create each index page for each leaf area
const { createPage } = actions;
for (const { node } of result.data.allMdx.edges) {
// For a given area indices, list out all the possible path strings for
// the children areas
const childAreaPathIds = childAreas[node.fields.pathId]
? childAreas[node.fields.pathId]
: [];
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/leaf-area-page-md.js`),
context: {
legacy_id: node.frontmatter.metadata.legacy_id,
pathId: node.fields.pathId,
childAreaPathIds: childAreaPathIds,
},
});
}
// Query all route .md documents
result = await graphql(`
query {
allMdx(filter: { fields: { collection: { eq: "climbing-routes" } } }) {
edges {
node {
fields {
slug
}
frontmatter {
metadata {
legacy_id
}
}
}
}
}
}
`);
// Create a single page for each climb
result.data.allMdx.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/climb-page-md.js`),
context: {
legacy_id: node.frontmatter.metadata.legacy_id,
},
});
});
};
/**
* Webpack no longer includes path-browserify. Adding this
* function to make 'path' library available to client-side code.
*/
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
fallback: {
path: require.resolve("path-browserify"),
assert: false,
stream: false,
},
},
});
};
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions;
// Matching pages on the client side
if (page.path.match(/^\/edit/)) {
page.matchPath = "/edit/*";
// Update the page
createPage(page);
}
};