Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
175 lines (142 loc) · 5.06 KB
/
Copy pathapp.js
File metadata and controls
175 lines (142 loc) · 5.06 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
(function () {
'use strict';
var DEFAULT_ORG = 'MiForge';
var host = location.hostname.toLowerCase();
var org = /\.github\.io$/.test(host) ? host.split('.')[0] : DEFAULT_ORG;
var siteRepo = org.toLowerCase() + '.github.io';
var cacheKey = 'projects:v1:' + org.toLowerCase();
var list = document.getElementById('projects');
if (!list) return;
var discussions = document.getElementById('discussions-link');
if (discussions) discussions.href = 'https://github.com/orgs/' + encodeURIComponent(org) + '/discussions';
var numbers = new Intl.NumberFormat('en-US');
var shown = null;
function el(tag, className, text) {
var node = document.createElement(tag);
if (className) node.className = className;
if (text !== undefined) node.textContent = text;
return node;
}
function githubUrl(value) {
try {
var url = new URL(String(value));
return url.protocol === 'https:' && url.hostname === 'github.com' ? url.href : '';
} catch (_) {
return '';
}
}
function starsLabel(count) {
return count === 1 ? ' star' : ' stars';
}
function normalize(data) {
if (!Array.isArray(data)) return [];
return data.filter(function (repo) {
return repo && typeof repo.name === 'string' && typeof repo.description === 'string' &&
repo.description.trim() !== '' && repo.name.toLowerCase() !== siteRepo && githubUrl(repo.html_url);
}).map(function (repo) {
return {
name: repo.name,
stars: Number(repo.stargazers_count) || 0,
url: githubUrl(repo.html_url),
description: repo.description.trim()
};
}).sort(function (a, b) {
return b.stars - a.stars || a.name.localeCompare(b.name);
});
}
function readCache() {
try {
var saved = JSON.parse(localStorage.getItem(cacheKey) || 'null');
if (!saved || !Array.isArray(saved.repos)) return null;
return saved.repos.filter(function (repo) {
return repo && typeof repo.name === 'string' && typeof repo.description === 'string' &&
Number.isFinite(repo.stars) && githubUrl(repo.url);
});
} catch (_) {
return null;
}
}
function writeCache(repos) {
try {
localStorage.setItem(cacheKey, JSON.stringify({ saved: Date.now(), repos: repos }));
} catch (_) {}
}
function row(repo, step) {
var item = el('li', 'repo');
if (step !== null) {
item.classList.add('enter');
item.style.setProperty('--step', step);
item.style.setProperty('--base', Math.max(0.05, 1.2 - performance.now() / 1000).toFixed(2));
}
var link = el('a', 'repo-link');
link.href = repo.url;
link.target = '_blank';
link.rel = 'noopener noreferrer';
var name = el('span', 'repo-name', repo.name);
name.translate = false;
var stars = el('span', 'repo-stars');
stars.append(el('span', 'repo-count', numbers.format(repo.stars)), el('span', 'sr-only', starsLabel(repo.stars)));
var top = el('span', 'repo-top');
top.append(name, stars);
link.append(top, el('span', 'repo-desc', repo.description));
item.append(link);
return item;
}
function render(repos, animate) {
var fragment = document.createDocumentFragment();
if (!repos.length) fragment.append(el('li', 'note', 'No public projects yet.'));
repos.forEach(function (repo, index) {
fragment.append(row(repo, animate ? Math.min(index, 9) : null));
});
list.replaceChildren(fragment);
list.setAttribute('aria-busy', 'false');
}
function renderError() {
var link = el('a', '', 'View them on GitHub');
link.href = 'https://github.com/' + encodeURIComponent(org);
link.target = '_blank';
link.rel = 'noopener noreferrer';
var item = el('li', 'note');
item.append("Couldn't load projects. ", link, '.');
list.replaceChildren(item);
list.setAttribute('aria-busy', 'false');
}
function sameShape(a, b) {
return a.length === b.length && a.every(function (repo, index) {
return repo.name === b[index].name;
});
}
function patch(repos) {
var rows = list.querySelectorAll('.repo');
repos.forEach(function (repo, index) {
var item = rows[index];
item.querySelector('.repo-link').href = repo.url;
item.querySelector('.repo-count').textContent = numbers.format(repo.stars);
item.querySelector('.repo-stars .sr-only').textContent = starsLabel(repo.stars);
item.querySelector('.repo-desc').textContent = repo.description;
});
}
function apply(repos) {
if (shown && sameShape(shown, repos)) patch(repos);
else render(repos, !shown);
shown = repos;
}
var cached = readCache();
if (cached) {
shown = cached;
render(cached, true);
}
fetch('https://api.github.com/orgs/' + encodeURIComponent(org) + '/repos?per_page=100')
.then(function (response) {
if (!response.ok) throw new Error(String(response.status));
return response.json();
})
.then(function (data) {
var repos = normalize(data);
writeCache(repos);
apply(repos);
})
.catch(function () {
if (!shown) renderError();
});
}());