-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnext.config.js
176 lines (165 loc) · 5.97 KB
/
next.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
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
// next.config.js
const withTypescript = require("@zeit/next-typescript");
const withCSS = require("@zeit/next-css");
var fs = require("fs");
var path = require("path");
var glob = require("glob");
const mjpage = require("mathjax-node-page/lib/main.js").mjpage;
const runMath = true;
function normalizeEntry(entry) {
const category = entry[0];
const children = entry[1].map(normalizeIndex);
return {
category: category,
self: [],
children: children,
};
}
function normalizeIndex(id) {
try {
const first = id[0];
if (typeof first !== "string")
throw new Error(`Expected first entry to be a string, but got ${typeof first} in ${JSON.stringify(id)}`);
const category = first;
const rest = id[1];
if (rest.length == 3 && rest[2] == null) {
const self = rest[0].map(x => x[1]);
const children = rest[1].map(x => normalizeIndex(x));
return {
category: category,
self: self,
children: children,
};
} else {
return {
category: category,
self: rest.map(r => r[1]),
children: [],
};
}
} catch (e) {
throw e;
}
}
function mathify(str) {
return new Promise((resolve, reject) => {
mjpage(str, { format: ["TeX"], fragment: true }, { svg: true }, output => resolve(output));
});
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
const imagesRE3 = new RegExp(escapeRegExp("../../../_images/"), "g");
const imagesRE2 = new RegExp(escapeRegExp("../../_images/"), "g");
const hrefRE = new RegExp(/href="/g);
/**
* This is run "server-side" (where the file system actually exists) and
* fetches the contexts of the local files. This will then be passed
* as "query strings" to the components so that they don't have to make any
* network requests or read the file system (because they are "client-side").
*/
async function getData(filename) {
const raw = fs.readFileSync(path.join(__dirname, "json", filename));
let str = raw.toString();
str = str.replace(imagesRE3, "/static/_images/");
str = str.replace(imagesRE2, "/static/_images/");
if (runMath) {
const obj = JSON.parse(str);
if (obj.body) {
obj.body = await mathify(obj.body);
}
str = JSON.stringify(obj);
}
return str;
}
async function getPages() {
return new Promise((resolve, reject) => {
glob(
"**/*.fjson",
{
cwd: path.join(__dirname, "json"),
},
(err, files) => {
if (err) reject(err);
else resolve(files);
},
);
});
}
module.exports = withCSS(
withTypescript({
useFileSystemPublicRoutes: false,
exportPathMap: async defaultPathMap => {
const globalData = await getData("globalcontext.json");
const sponsorData = await getData("_static/sponsors/sponsors.json");
const titles = {};
const files = await getPages();
const ret = {};
await Promise.all(
files.map(async file => {
const fjson = JSON.parse(await getData(file));
const route = `/${file.slice(0, file.length - 6)}/`;
console.log(`Title for ${route} is ${fjson.title}`);
titles[route] = fjson.title;
}),
);
const titleData = JSON.stringify(titles);
let toc = JSON.parse(await getData("index.fjson")).body;
toc = toc.replace(hrefRE, `href="/`);
const work = files.map(async file => {
const fjson = await getData(file);
console.log("file = ", file);
switch (file) {
case "search.fjson": {
break;
}
case "index.fjson": {
const query = {
page: fjson,
global: globalData,
titles: titleData,
toc: toc,
sponsors: sponsorData,
};
ret["/"] = { page: "/", query: query };
break;
}
case "genindex.fjson": {
const raw = JSON.parse(fjson);
const entries = raw["genindexentries"];
const normal = entries.map(normalizeEntry);
const text = JSON.stringify({
counts: raw["genindexcounts"],
entries: normal,
});
const query = {
index: text,
global: globalData,
titles: titleData,
toc: toc,
};
ret["/genindex"] = { page: "/indexview", query: query };
break;
}
default: {
const query = {
page: fjson,
global: globalData,
titles: titleData,
toc: toc,
};
const route = `/${file.slice(0, file.length - 6)}/`;
ret[route] = { page: "/pageview", query: query };
break;
}
}
});
await Promise.all(work);
Object.keys(ret).forEach(route => {
console.log(`${route} -> ${ret[route].page}`);
});
// console.log("ret = " + JSON.stringify(ret));
return ret;
},
}),
);