-
Notifications
You must be signed in to change notification settings - Fork 4
/
examples.html
100 lines (87 loc) · 3.04 KB
/
examples.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="https://whsieh.github.io/examples" />
<style>
body {
font-family: -apple-system;
font-size: 1em;
margin: 1em;
}
.container {
margin-top: 1em;
}
</style>
</head>
<body>
<h1>Recent examples</h1>
<div id="recent"></div>
<h1>All examples</h1>
<div id="all"></div>
<script>
let apiRoot = "https://api.github.com";
let username = "whsieh";
let repository = "whsieh.github.io";
let urlRoot = "https://whsieh.github.io";
let examplesDirectoryName = "examples";
let fetchedExamplePages = new Set();
let validExamplePageNames = new Set();
function fetchJSONFromURL(url, completion) {
fetch(url).then(data => data.text()).then(text => completion(JSON.parse(text)));
}
function getLatestCommit(commitInfos)
{
let latestCommitInfo = null;
let timestampOfLatestCommit = 0;
for (let commitInfo of commitInfos) {
let timestamp = new Date(commitInfo.commit.author.date).getTime();
if (timestamp <= timestampOfLatestCommit)
continue;
timestampOfLatestCommit = timestamp;
latestCommitInfo = commitInfo;
}
return latestCommitInfo;
}
function makeLinkContainer(href, text = undefined)
{
if (!text)
text = href;
let link = document.createElement("a");
link.href = href;
link.textContent = text || href;
let container = document.createElement("div");
container.appendChild(link);
container.className = "container";
return container;
}
function recursivelyUpdateRecentPagesForCommits(commitInfos, limit, delay)
{
if (!limit)
return;
let latestCommitInfo = getLatestCommit(commitInfos);
commitInfos.splice(commitInfos.indexOf(latestCommitInfo), 1);
fetchJSONFromURL(`${apiRoot}/repos/${username}/${repository}/commits/${latestCommitInfo.sha}`, (commitDetail) => {
for (let changedFile of commitDetail.files.map(fileInfo => fileInfo.filename)) {
if (fetchedExamplePages.has(changedFile) || !changedFile.match(/^examples\/.*.html$/) || !validExamplePageNames.has(changedFile))
continue;
fetchedExamplePages.add(changedFile);
recent.appendChild(makeLinkContainer(`${urlRoot}/${changedFile}`, changedFile));
}
setTimeout(() => {
recursivelyUpdateRecentPagesForCommits(commitInfos, limit - 1);
}, delay);
});
}
(() => {
fetchJSONFromURL(`${apiRoot}/repos/${username}/${repository}/contents/${examplesDirectoryName}?ref=main`, examples => {
let allExamplePages = examples.map(exampleInfo => exampleInfo.path).filter(name => name.endsWith(".html"));
for (let examplePage of allExamplePages)
all.appendChild(makeLinkContainer(`${urlRoot}/${examplePage}`, examplePage));
validExamplePageNames = new Set(allExamplePages);
fetchJSONFromURL(`${apiRoot}/repos/${username}/${repository}/commits`, commitInfos => recursivelyUpdateRecentPagesForCommits(commitInfos, 10, 500));
});
})();
</script>
</body>
</html>