forked from code-423n4/code423n4.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.esm.js
228 lines (202 loc) · 5.54 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
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
import { graphql } from "@octokit/graphql";
import format from "date-fns/format";
import { createFilePath } from "gatsby-source-filesystem";
import fetch from "node-fetch";
import path from "path";
import webpack from "webpack";
import SchemaCustomization from "./schema";
const { token } = require("./netlify/_config");
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `Bearer ${token}`,
},
});
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 contestSlug(contestNode) {
const startDate = new Date(contestNode.start_time);
const title = slugify(contestNode.title);
const slug = `${format(startDate, "yyyy-MM")}-${title}`;
return slug;
}
function contestPermalink(contestNode) {
return `/contests/${contestSlug(contestNode)}`;
}
function contestSubmissionPermalink(contestNode) {
return `/contests/${contestSlug(contestNode)}/submit`;
}
function getRepoName(contestNode) {
const regex = "([^/]+$)";
const url = contestNode.repo;
const result = url.match(regex);
const repoName = result[0];
return repoName;
}
async function fetchReadmeMarkdown(contestNode) {
const response = await fetch(
`https://raw.githubusercontent.com/${
process.env.GITHUB_CONTEST_REPO_OWNER
}/${getRepoName(contestNode)}/main/README.md`
);
const data = await response.text();
return data;
}
async function fetchSocialImage(contestNode) {
// @todo: fetch without auth
const { repository } = await graphqlWithAuth(
`query socialImage($repo: String!) {
repository(owner: "${process.env.GITHUB_CONTEST_REPO_OWNER}", name: $repo) {
openGraphImageUrl
usesCustomOpenGraphImage
}
}`,
{
repo: getRepoName(contestNode),
}
);
if (repository.usesCustomOpenGraphImage) {
return repository.openGraphImageUrl;
}
return null;
}
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
contestPath
readmeContent
artPath
}
}
}
}
}
`,
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
try {
createTypes(SchemaCustomization);
} catch (error) {
console.log(error);
}
};
exports.onCreateNode = async ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
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: `contestPath`,
value: contestPermalink(node),
});
createNodeField({
node,
name: `submissionPath`,
value: contestSubmissionPermalink(node),
});
const readmeMarkdown = await fetchReadmeMarkdown(node);
createNodeField({
node,
name: `readmeContent`,
value: readmeMarkdown,
});
const socialImageUrl = await fetchSocialImage(node);
createNodeField({
node,
name: `artPath`,
value: socialImageUrl,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const contests = await graphql(queries.contests);
const formTemplate = path.resolve("./src/templates/ReportForm.tsx");
const contestTemplate = path.resolve("./src/templates/ContestLayout.tsx");
contests.data.contests.edges.forEach((contest) => {
if (contest.node.findingsRepo) {
createPage({
path: contest.node.fields.submissionPath,
component: formTemplate,
context: {
contestId: contest.node.contestid,
},
});
// XXX: inject old-style reporting form
createPage({
path: contest.node.fields.submissionPath + "-old",
component: path.resolve("./src/templates/OldReportForm.js"),
context: {
contestId: contest.node.contestid,
},
});
}
createPage({
path: contest.node.fields.contestPath,
component: contestTemplate,
context: {
contestId: contest.node.contestid,
},
});
});
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
plugins: [
new webpack.IgnorePlugin({
resourceRegExp: /canvas/,
contextRegExp: /jsdom$/,
}),
new webpack.ProvidePlugin({
Buffer: [require.resolve("buffer/"), "Buffer"],
}),
],
resolve: {
fallback: {
assert: require.resolve("assert"),
crypto: require.resolve("crypto-browserify"),
http: require.resolve("stream-http"),
https: require.resolve("https-browserify"),
os: require.resolve("os-browserify/browser"),
stream: require.resolve("stream-browserify"),
},
},
});
};