This repository was archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatic.config.js
executable file
·64 lines (57 loc) · 1.53 KB
/
static.config.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
const glob = require("glob");
const fs = require("fs");
const issues = glob.sync("src/issues/*/index.html").map(parseIssue);
issues.sort(({ issue: a }, { issue: b }) => b - a);
const latestIssue = issues[0].issue;
const firstIssue = issues[issues.length - 1].issue;
export default {
siteRoot:
process.env.NODE_ENV === "development"
? "http://localhost:3000"
: "https://this-week-in-react.org",
getSiteData: () => ({
title: "This Week in React"
}),
getRoutes: async () => {
return [
{
path: "/",
redirect: "issues/" + latestIssue
},
...issues.map(({ issue, date, html }) => {
return {
path: "/issues/" + issue,
component: "src/containers/Issue",
async getData() {
return {
issue,
date,
html,
isLatest: issue === latestIssue,
isFirst: issue === firstIssue
};
},
lastModified: utcDate(date)
};
}),
{
path: "404",
component: "src/containers/404"
}
];
}
};
function parseIssue(path) {
try {
const html = fs.readFileSync(path).toString();
const match = /This Week in React – Issue (\d+) – (.*)/.exec(html);
const issue = parseInt(match[1]);
const date = new Date(match[2]);
return { issue, date, html };
} catch (error) {
throw new Error(`Error parsing issue ${path}\n\n${error.message}`);
}
}
function utcDate(date) {
return date.toISOString().split("T")[0];
}