This repository was archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatic.config.js
More file actions
243 lines (219 loc) · 6.39 KB
/
static.config.js
File metadata and controls
243 lines (219 loc) · 6.39 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import path from "path";
import cheerio from "cheerio";
import { Octokit } from "@octokit/rest";
import Interscript from "interscript";
import alalcSamples from "./src/samples/alalc.json";
import bgnpcgnSamples from "./src/samples/bgnpcgn.json";
import odniSamples from "./src/samples/odni.json";
import ogc11122Samples from "./src/samples/ogc11122.json";
import unSamples from "./src/samples/un.json";
const repoOwner = "interscript";
const repoName = "interscript";
const octokit = new Octokit(); // auth: process.env.GH_TOKEN }
export default {
entry: path.join(__dirname, "src", "index.tsx"),
getRoutes: async () => {
const readmeResp = await octokit.request(
"GET /repos/{owner}/{repo}/readme",
{
owner: repoOwner,
repo: repoName,
mediaType: {
format: "html",
},
}
);
await Interscript.load_map_list();
const maps = Interscript.map_list();
const mapsInfo = {};
mapsInfo.languages = {};
maps.forEach((systemCode) => {
const alpha3 = systemCode.split("-")[1];
if (mapsInfo.languages[alpha3]) {
mapsInfo.languages[alpha3]++;
} else {
mapsInfo.languages[alpha3] = 1;
}
});
mapsInfo["meta"] = {
total: Object.keys(maps).length,
};
mapsInfo["data"] = maps;
const readme = cheerio.load(readmeResp.data);
const sectionHeaders = readme("h2");
const readmeSections = [];
for (const idx of Object.keys(sectionHeaders)) {
const sectionEl = sectionHeaders[idx].parent;
if (sectionEl) {
const section /*: ReadmeSection */ = prepareReadmeSection(
cheerio.load(sectionEl)
);
if (section) {
readmeSections.push(section);
}
}
}
// SSR
await Interscript.load_map_list();
const translit = async (system, text) => {
await Interscript.load_map(system);
return Interscript.transliterate(system, text).split("\n");
};
const evaluate = (samples) =>
Promise.all(
samples.map(async (s) => {
const text = s.samples.join("\n");
const { systemName: system } = s;
if (!text || !system || !Interscript.map_exist(system)) {
return s;
}
try {
const result = await translit(system, text);
return { ...s, result };
} catch (e) {
console.log(e);
}
return s;
})
);
let alalc, bgnpcgn, odni, ogc11122, un;
if (process.env.NODE_ENV === "production") {
console.log("transliterates all samples on build process...");
alalc = await evaluate(alalcSamples);
bgnpcgn = await evaluate(bgnpcgnSamples);
odni = await evaluate(odniSamples);
ogc11122 = await evaluate(ogc11122Samples);
un = await evaluate(unSamples);
}
return [
{
path: "/",
template: "src/containers/Landing",
getData: () => ({
readmeSections,
repoInfo: {
owner: repoOwner,
name: repoName,
},
mapsInfo,
}),
},
{
path: "alalc",
template: "src/pages/alalc.tsx",
getData: () => ({
samples: alalc || alalcSamples,
}),
},
{
path: "bgnpcgn",
template: "src/pages/bgnpcgn.tsx",
getData: () => ({
samples: bgnpcgn || bgnpcgnSamples,
}),
},
{
path: "odni",
template: "src/pages/odni.tsx",
getData: () => ({
samples: odni || odniSamples,
}),
},
{
path: "ogc11122",
template: "src/pages/ogc11122.tsx",
getData: () => ({
samples: ogc11122 || ogc11122Samples,
}),
},
{
path: "un",
template: "src/pages/un.tsx",
getData: () => ({
samples: un || unSamples,
}),
},
];
},
plugins: [
"react-static-plugin-typescript",
[
require.resolve("react-static-plugin-source-filesystem"),
{
location: path.resolve("./src/pages"),
},
],
require.resolve("react-static-plugin-reach-router"),
require.resolve("react-static-plugin-styled-components"),
require.resolve("react-static-plugin-sitemap"),
],
};
function prepareReadmeSection(cheerioEl) /*: ReadmeSection | undefined */ {
const ghSectionID = cheerioEl("h2").attr("id");
if (!ghSectionID) {
// If this section’s top-level H2 doesn’t have an ID, don’t process it.
// NOTE: Throw?
console.warn("Missing section ID");
return;
}
if (cheerioEl.root().children().length !== 1) {
console.warn("Unexpected number of section element descendants");
return;
}
const sectionTitle = cheerioEl("h2").clone().removeAttr("id");
sectionTitle.find("a.anchor").remove();
// GitHub’s anchors have user-content- prefix in their IDs (but not in hrefs)
const sectionID = ghSectionID.replace("user-content-", "");
cheerioEl("*[id]")
.get()
.map((el) => {
if (el.attribs.id) {
el.attribs.id = el.attribs.id.replace("user-content-", "");
}
});
// Remove ID from headings and their anchors, add it to section wrappers instead.
// For top-level section wrapper, ID is expected to be added externally.
cheerioEl("a.anchor[id]")
.get()
.map((el) => {
delete el.attribs.id;
});
cheerioEl("h2[id], h3[id], h4[id], h5[id], h6[id]")
.get()
.map((el) => {
const id = el.attribs.id;
if (el.parent.type === "tag" && el.parent.name === "div") {
el.parent.attribs.id = id;
}
delete el.attribs.id;
});
// Make relative in-repo links work
cheerioEl('a:not([href^="http"])')
.get()
.map((el) => {
if (
el.type === "tag" &&
el.name === "img" &&
el.attribs.href?.indexOf("http") !== 0
) {
el.attribs.href = `https://github.com/${repoOwner}/${repoName}/blob/master/${el.attribs.href}`;
}
});
// Make relative in-repo image sources work
cheerioEl('img:not([src^="http"])')
.get()
.map((el) => {
if (
el.type === "tag" &&
el.name === "img" &&
el.attribs.src?.indexOf("http") !== 0
) {
el.attribs.src = `https://github.com/${repoOwner}/${repoName}/raw/master/${el.attribs.src}`;
}
});
return {
id: sectionID,
titleHTML: sectionTitle.html(),
html: cheerio(cheerioEl.root().children()[0]).html(),
};
}