forked from code-423n4/code423n4.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.esm.js
107 lines (97 loc) · 2.65 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
}
function contestPermalink(contestNode) {
const startDate = new Date(contestNode.start_time);
const year = startDate.getFullYear();
const month = `${startDate.getMonth() + 1}`.padStart(2, "0");
const title = slugify(contestNode.title);
const submissionPath = `/${year}-${month}-${title}/submit`;
return submissionPath;
}
const queries = {
contests: `query {
contests: allContestsCsv(sort: { fields: end_time, order: ASC }) {
edges {
node {
id
contestid
title
start_time(formatString: "YYYY-MM")
findingsRepo
fields {
submissionPath
}
}
}
}
}
`,
};
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,
});
}
if (node.internal.type === `ContestsCsv`) {
createNodeField({
node,
name: `submissionPath`,
value: contestPermalink(node),
});
}
};
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) => {
if (contest.node.findingsRepo) {
createPage({
path: contest.node.fields.submissionPath,
component: formTemplate,
context: {
contestId: contest.node.contestid,
},
});
}
});
};