-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (93 loc) · 2.98 KB
/
index.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
"use strict";
const FETCH_README = 'https://raw.githubusercontent.com/thechnet/adamatic/main/README.md';
const FETCH_CHANGELOG = 'https://raw.githubusercontent.com/thechnet/adamatic/main/CHANGELOG.md';
function parseReadme(text) {
let sections = {};
let lines = text.split('\n');
let section = undefined;
for (let i = 0; i < lines.length; ++i) {
if (lines[i] === '<!--@end-->') {
section = undefined;
continue;
}
let sectionDeclaration = lines[i].match(/^<!--@begin ([a-z]+)-->$/);
if (sectionDeclaration) {
section = sectionDeclaration[1];
if (!(section in sections))
sections[section] = [];
continue;
}
if (section) {
sections[section].push(lines[i]);
}
}
return sections;
}
function parseChangelog(text) {
let versions = {
latest: undefined,
unreleased: {
lines: []
}
};
let lines = text.split('\n');
let currentVersion = undefined;
for (let i = 0; i < lines.length; ++i) {
/* Skip reference declarations. */
if (lines[i].match(/^\[.+\]: .*$/)) {
continue;
}
let unreleasedDeclaration = lines[i].match(/^## \[Unreleased\]/i);
if (unreleasedDeclaration) {
currentVersion = 'unreleased';
continue;
}
let versionDeclaration = lines[i].match(/^## \[(\d+\.\d+\.\d+)\] - (\d{4}-\d{2}-\d{2})$/);
if (versionDeclaration) {
currentVersion = versionDeclaration[1];
versions[currentVersion] = {
date: versionDeclaration[2],
lines: []
};
if (!versions.latest) {
versions.latest = currentVersion;
}
continue;
}
if (currentVersion) {
versions[currentVersion].lines.push(lines[i]);
}
}
return versions;
}
function renderReadme(text) {
let sections = parseReadme(text);
document.getElementById('abstract').innerHTML = marked.parse(sections.abstract.join('\n'));
document.getElementById('highlights').innerHTML = marked.parse(sections.table.join('\n'));
}
function renderChanges(text, requestedVersion) {
let versions = parseChangelog(text);
if (requestedVersion === 'latest') {
requestedVersion = versions.latest;
}
if (!(requestedVersion in versions)) {
console.error('Requested version not in parsed versions.');
return;
}
if (versions[requestedVersion].lines.length) {
document.getElementById('changes-title').textContent = requestedVersion === 'unreleased' ? 'Upcoming Changes' : 'What\'s New in ' + requestedVersion;
document.getElementById('changelog').innerHTML = marked.parse(versions[requestedVersion].lines.join('\n'));
document.getElementById('changes').style.display = 'block';
}
}
let requestedVersion = new URLSearchParams(window.location.search).get('changes');
async function main() {
renderReadme(await (await fetch(FETCH_README)).text());
if (requestedVersion) {
renderChanges(await (await fetch(FETCH_CHANGELOG)).text(), requestedVersion);
}
Promise.all(Array.from(document.images).filter(img => !img.complete).map(img => new Promise(resolve => { img.onload = img.onerror = resolve; }))).then(() => {
document.getElementById('main').style.opacity = 1;
});
}
main();