forked from ncitron/code-423n4.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.esm.js
83 lines (76 loc) · 2.07 KB
/
gatsby-node.esm.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
import path from "path";
import SchemaCustomization from "./schema";
import { createFilePath } from "gatsby-source-filesystem";
function slugify(text) {
return text
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\-\-+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
}
const queries = {
contests: `query {
contests: allContestsJson(sort: { fields: end_time, order: ASC }) {
edges {
node {
id
contestid
title
start_time(formatString: "YYYY-MM")
}
}
}
}
`,
};
exports.createSchemaCustomization = (helpers) => {
const { actions } = helpers;
const { createTypes } = actions;
try {
createTypes(SchemaCustomization);
} catch (error) {
console.log(error);
}
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode });
const parent = getNode(node.parent);
let slug;
if (node.frontmatter.slug) {
// if a slug is defined, use that.
slug = "/" + node.frontmatter.slug;
} else {
// otherwise use the file path
slug = createFilePath({ node, getNode });
}
createNodeField({
node,
name: `collection`,
value: parent.sourceInstanceName,
});
createNodeField({
node,
name: `slug`,
value: slug,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
let contests = await graphql(queries.contests);
const formTemplate = path.resolve("./src/layouts/ReportForm.js");
contests.data.contests.edges.forEach((contest) => {
createPage({
path: `/${contest.node.start_time}-${slugify(contest.node.title)}/submit`,
component: formTemplate,
context: {
contestId: contest.node.contestid,
},
});
});
};