-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-topic-cloud.html
More file actions
308 lines (263 loc) · 11 KB
/
github-topic-cloud.html
File metadata and controls
308 lines (263 loc) · 11 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!doctype html>
<html lang="en" class="neo-landing-root">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GitHub Topic Cloud</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,400;0,500;0,600;0,700;1,400&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="./github-tools-common.css" />
<link rel="stylesheet" href="./github-tools-neo.css" />
</head>
<body class="neo-landing">
<div class="wrap">
<section class="tool-hero">
<div class="tool-kicker">GitHub Tools Suite</div>
<h1>GitHub Topic Cloud</h1>
<p>Enter a username to see their most frequently used repository topics. Click a topic to open the global GitHub topic page.</p>
</section>
<div class="tool-grid">
<section class="card">
<div class="card-header">
<h2>Input + filters</h2>
<p class="sub">Pick a user, optionally add a token, and tune how many topics to show.</p>
</div>
<div class="card-content section-stack">
<form id="form" class="section-stack">
<div class="field">
<label for="username">GitHub username</label>
<input id="username" type="text" placeholder="e.g. monapdx" autocomplete="off" />
</div>
<div class="actions">
<button id="go" type="submit" class="primary">Generate</button>
<button id="advancedBtn" class="toggle" type="button">Advanced (token)</button>
<button id="clearTokenBtn" class="toggle hidden" type="button">Clear saved token</button>
</div>
</form>
<div id="advanced" class="controls hidden">
<div class="control" style="grid-column: 1 / -1;">
<label for="token">GitHub token (optional, stored locally)</label>
<input id="token" type="password" placeholder="ghp_… (optional)" autocomplete="off" />
<div class="value">Using a token helps avoid API rate limits. Token is saved in <code>localStorage</code> on this device.</div>
</div>
</div>
<div class="controls">
<div class="control">
<label for="topN">Show top N topics</label>
<input id="topN" type="range" min="10" max="100" value="40" />
<div class="value"><span id="topNVal">40</span></div>
</div>
<div class="control">
<label for="minFreq">Minimum frequency</label>
<input id="minFreq" type="range" min="1" max="10" value="1" />
<div class="value"><span id="minFreqVal">1</span></div>
</div>
<div class="control">
<label for="includeForks">Include forks</label>
<input id="includeForks" type="checkbox" />
<div class="value">Off by default</div>
</div>
</div>
</div>
</section>
<section class="card">
<div class="card-header">
<h2>Results</h2>
<p class="sub">Topic frequency, status messages, and summary information.</p>
</div>
<div class="card-content section-stack">
<div id="status" class="status"></div>
<div id="cloud" class="cloud"></div>
<div class="footer">
<span id="summary"></span>
</div>
</div>
</section>
</div>
</div>
<script>
const $ = (id) => document.getElementById(id);
const form = $("form");
const usernameInput = $("username");
const statusEl = $("status");
const cloudEl = $("cloud");
const summaryEl = $("summary");
const advancedBtn = $("advancedBtn");
const advancedPanel = $("advanced");
const tokenInput = $("token");
const clearTokenBtn = $("clearTokenBtn");
const topN = $("topN");
const topNVal = $("topNVal");
const minFreq = $("minFreq");
const minFreqVal = $("minFreqVal");
const includeForks = $("includeForks");
const goBtn = $("go");
topN.addEventListener("input", () => topNVal.textContent = topN.value);
minFreq.addEventListener("input", () => minFreqVal.textContent = minFreq.value);
// localStorage token handling
const TOKEN_KEY = "gh_topic_cloud_token";
const savedToken = localStorage.getItem(TOKEN_KEY);
if (savedToken) {
tokenInput.value = savedToken;
clearTokenBtn.classList.remove("hidden");
}
advancedBtn.addEventListener("click", () => {
advancedPanel.classList.toggle("hidden");
clearTokenBtn.classList.toggle("hidden", !localStorage.getItem(TOKEN_KEY));
});
tokenInput.addEventListener("change", () => {
const t = tokenInput.value.trim();
if (t) {
localStorage.setItem(TOKEN_KEY, t);
clearTokenBtn.classList.remove("hidden");
} else {
localStorage.removeItem(TOKEN_KEY);
clearTokenBtn.classList.add("hidden");
}
});
clearTokenBtn.addEventListener("click", () => {
localStorage.removeItem(TOKEN_KEY);
tokenInput.value = "";
clearTokenBtn.classList.add("hidden");
setStatus("Saved token cleared.", "ok");
});
function setStatus(msg, kind = "") {
statusEl.textContent = msg || "";
statusEl.className = "status" + (kind ? " " + kind : "");
}
function topicUrl(topic) {
// GitHub topic URLs are kebab-case; topics from API are already like "data-sovereignty"
const slug = String(topic).trim().toLowerCase();
return `https://github.com/topics/${encodeURIComponent(slug)}`;
}
function clamp(n, a, b) { return Math.max(a, Math.min(b, n)); }
function fontSizeFor(count, minC, maxC) {
// log scale for nicer spread
const min = Math.log(minC + 1);
const max = Math.log(maxC + 1);
const v = Math.log(count + 1);
const t = (v - min) / (max - min || 1);
const px = 12 + t * 18; // 12px..30px
return `${clamp(px, 12, 30).toFixed(1)}px`;
}
async function fetchAllRepos(username, token, includeForksFlag) {
const perPage = 100;
let page = 1;
let all = [];
while (true) {
const url = `https://api.github.com/users/${encodeURIComponent(username)}/repos?per_page=${perPage}&page=${page}&sort=updated`;
const headers = {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"
};
if (token) headers["Authorization"] = `Bearer ${token}`;
const res = await fetch(url, { headers });
if (!res.ok) {
let extra = "";
try { extra = await res.text(); } catch {}
throw new Error(`GitHub API error ${res.status} ${res.statusText}${extra ? " — " + extra : ""}`);
}
const batch = await res.json();
if (!Array.isArray(batch)) break;
const filtered = includeForksFlag ? batch : batch.filter(r => !r.fork);
all = all.concat(filtered);
if (batch.length < perPage) break;
page += 1;
// safety stop
if (page > 20) break; // 2000 repos cap for sanity
}
return all;
}
function countTopics(repos) {
const map = new Map();
for (const r of repos) {
const topics = Array.isArray(r.topics) ? r.topics : [];
for (const t of topics) {
const key = String(t).trim().toLowerCase();
if (!key) continue;
map.set(key, (map.get(key) || 0) + 1);
}
}
return map;
}
function renderCloud(topicCounts, options) {
cloudEl.innerHTML = "";
summaryEl.textContent = "";
const entries = [...topicCounts.entries()]
.map(([topic, count]) => ({ topic, count }))
.filter(x => x.count >= options.minFreq)
.sort((a, b) => b.count - a.count)
.slice(0, options.topN);
if (!entries.length) {
setStatus("No topics found (or filtered out).", "error");
return;
}
const counts = entries.map(e => e.count);
const minC = Math.min(...counts);
const maxC = Math.max(...counts);
for (const { topic, count } of entries) {
const a = document.createElement("a");
a.className = "chip";
a.href = topicUrl(topic);
a.target = "_blank";
a.rel = "noopener noreferrer";
a.style.fontSize = fontSizeFor(count, minC, maxC);
const label = document.createElement("span");
label.textContent = topic;
const badge = document.createElement("span");
badge.className = "count";
badge.textContent = count;
a.appendChild(label);
a.appendChild(badge);
cloudEl.appendChild(a);
}
const totalTopics = topicCounts.size;
const totalTaggedRepos = [...topicCounts.values()].reduce((a,b)=>a+b,0);
summaryEl.textContent = `Showing ${entries.length} topics • ${totalTopics} unique topics found • ${totalTaggedRepos} topic-usages counted (repos may contribute multiple topics).`;
setStatus("Done.", "ok");
}
async function run() {
const username = usernameInput.value.trim();
if (!username) {
setStatus("Please enter a GitHub username.", "error");
return;
}
const token = (tokenInput.value || localStorage.getItem(TOKEN_KEY) || "").trim();
const opts = {
topN: Number(topN.value),
minFreq: Number(minFreq.value),
includeForks: includeForks.checked
};
setStatus(`Fetching repos for ${username}…`, "");
cloudEl.innerHTML = "";
summaryEl.textContent = "";
goBtn.disabled = true;
try {
const repos = await fetchAllRepos(username, token, opts.includeForks);
setStatus(`Counting topics across ${repos.length} repos…`, "");
const topicCounts = countTopics(repos);
if (topicCounts.size === 0) {
setStatus("No topics found on this user's repos.", "error");
return;
}
renderCloud(topicCounts, opts);
} catch (err) {
const msg = (err && err.message) ? err.message : String(err);
// Common rate-limit hint
const hint = msg.includes("403") ? " If you hit rate limits, add a token in Advanced." : "";
setStatus(msg + hint, "error");
} finally {
goBtn.disabled = false;
}
}
form.addEventListener("submit", (e) => {
e.preventDefault();
run();
});
// nice default if you want:
// usernameInput.value = "monapdx";
// run();
</script>
</body>
</html>