-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-link-generator.html
More file actions
520 lines (463 loc) · 19.4 KB
/
github-link-generator.html
File metadata and controls
520 lines (463 loc) · 19.4 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<!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 Link Generator</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 Link Generator</h1>
<p>
Generate GitHub links from common URL patterns, then copy them as a plain URL,
markdown link, badge markdown, or HTML button.
</p>
</section>
<div class="tool-grid">
<section class="card">
<div class="card-header">
<h2>Build Link</h2>
<p class="sub">Choose a GitHub URL pattern and fill in the matching fields.</p>
</div>
<div class="card-content section-stack">
<div class="form-grid">
<div class="field full">
<label for="pattern">Link type</label>
<select id="pattern"></select>
</div>
<div class="field">
<label for="owner">Owner / Username</label>
<input id="owner" type="text" placeholder="monapdx" />
</div>
<div class="field">
<label for="repo">Repository</label>
<input id="repo" type="text" placeholder="Frontend-Widgets" />
</div>
<div class="field dynamic-field" data-field="branch">
<label for="branch">Branch</label>
<input id="branch" type="text" placeholder="main" value="main" />
</div>
<div class="field dynamic-field" data-field="path">
<label for="path">Path</label>
<input id="path" type="text" placeholder="src/index.js" />
</div>
<div class="field dynamic-field" data-field="template">
<label for="template">Issue template filename</label>
<input id="template" type="text" placeholder="add-template.yml" />
</div>
<div class="field dynamic-field" data-field="labels">
<label for="labels">Labels</label>
<input id="labels" type="text" placeholder="bug,help wanted" />
</div>
<div class="field dynamic-field" data-field="title">
<label for="title">Issue title</label>
<input id="title" type="text" placeholder="Suggestion: add new widget" />
</div>
<div class="field dynamic-field full" data-field="body">
<label for="body">Issue body</label>
<textarea id="body" placeholder="Write some default issue body text here..."></textarea>
</div>
<div class="field dynamic-field" data-field="discussionCategory">
<label for="discussionCategory">Discussion category</label>
<input id="discussionCategory" type="text" placeholder="ideas" />
</div>
<div class="field dynamic-field" data-field="wikiPage">
<label for="wikiPage">Wiki page</label>
<input id="wikiPage" type="text" placeholder="Home" />
</div>
<div class="field dynamic-field" data-field="base">
<label for="base">Compare base</label>
<input id="base" type="text" placeholder="main" value="main" />
</div>
<div class="field dynamic-field" data-field="head">
<label for="head">Compare head</label>
<input id="head" type="text" placeholder="feature-branch" />
</div>
<div class="field dynamic-field" data-field="commit">
<label for="commit">Commit SHA</label>
<input id="commit" type="text" placeholder="abc123def456" />
</div>
<div class="field dynamic-field" data-field="releaseTag">
<label for="releaseTag">Release tag</label>
<input id="releaseTag" type="text" placeholder="v1.0.0" />
</div>
<div class="field full">
<label for="linkText">Link text / button text</label>
<input id="linkText" type="text" placeholder="Open on GitHub" value="Open on GitHub" />
</div>
<div class="field">
<label for="badgeLabel">Badge label</label>
<input id="badgeLabel" type="text" placeholder="Live Demo" value="Open on GitHub" />
</div>
<div class="field">
<label for="badgeColor">Badge color</label>
<input id="badgeColor" type="text" placeholder="pink" value="pink" />
</div>
</div>
<div class="actions">
<button class="primary" id="generateBtn">Generate</button>
<button class="secondary" id="resetBtn" type="button">Reset</button>
</div>
<p class="inline-note" id="patternNote"></p>
</section>
<section class="card">
<div class="card-header">
<h2>Output</h2>
<p class="sub">Copy the generated URL in whichever format you need.</p>
</div>
<div class="card-content section-stack">
<div class="output-block">
<label for="plainUrl">Plain URL</label>
<div class="output-row">
<input id="plainUrl" type="text" readonly />
<button class="copy-btn" data-copy-target="plainUrl">Copy</button>
</div>
</div>
<div class="output-block">
<label for="markdownLink">Markdown Link</label>
<div class="output-row">
<textarea id="markdownLink" readonly></textarea>
<button class="copy-btn" data-copy-target="markdownLink">Copy</button>
</div>
</div>
<div class="output-block">
<label for="badgeMarkdown">Badge Markdown</label>
<div class="output-row">
<textarea id="badgeMarkdown" readonly></textarea>
<button class="copy-btn" data-copy-target="badgeMarkdown">Copy</button>
</div>
</div>
<div class="output-block">
<label for="htmlLink">HTML Link/Button</label>
<div class="output-row">
<textarea id="htmlLink" readonly></textarea>
<button class="copy-btn" data-copy-target="htmlLink">Copy</button>
</div>
</div>
<div class="preview">
<strong>Preview</strong>
<div class="pattern-note" style="margin-top:8px;">Markdown badges do not render live in this preview, but the generated code is ready to paste into GitHub.</div>
<div style="margin-top:12px;">
<a id="liveLinkPreview" href="#" target="_blank" rel="noopener noreferrer">Open on GitHub</a>
</div>
<div class="badge-preview" style="margin-top:12px;">
<a id="liveBadgePreviewLink" href="#" target="_blank" rel="noopener noreferrer">
<img id="liveBadgePreviewImg" alt="Badge preview" />
</a>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<script>
const patterns = {
repo: {
label: "Repository",
fields: ["owner", "repo"],
note: "Main repository URL.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}`
},
issues: {
label: "Issues",
fields: ["owner", "repo"],
note: "Repository issues list.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}/issues`
},
newIssue: {
label: "New Issue",
fields: ["owner", "repo", "title", "body", "labels"],
note: "Prefill a new issue with optional title, body, and labels.",
build: ({ owner, repo, title, body, labels }) => {
const params = new URLSearchParams();
if (title) params.set("title", title);
if (body) params.set("body", body);
if (labels) params.set("labels", labels);
const query = params.toString();
return `https://github.com/${owner}/${repo}/issues/new${query ? `?${query}` : ""}`;
}
},
issueTemplate: {
label: "Issue Template",
fields: ["owner", "repo", "template", "title", "body", "labels"],
note: "Open a specific issue template, with optional prefilled values.",
build: ({ owner, repo, template, title, body, labels }) => {
const params = new URLSearchParams();
if (template) params.set("template", template);
if (title) params.set("title", title);
if (body) params.set("body", body);
if (labels) params.set("labels", labels);
return `https://github.com/${owner}/${repo}/issues/new?${params.toString()}`;
}
},
pulls: {
label: "Pull Requests",
fields: ["owner", "repo"],
note: "Repository pull requests tab.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}/pulls`
},
actions: {
label: "Actions",
fields: ["owner", "repo"],
note: "Repository Actions tab.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}/actions`
},
discussions: {
label: "Discussions",
fields: ["owner", "repo"],
note: "Repository discussions home.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}/discussions`
},
discussionCategory: {
label: "Discussion Category",
fields: ["owner", "repo", "discussionCategory"],
note: "Discussion category link.",
build: ({ owner, repo, discussionCategory }) =>
`https://github.com/${owner}/${repo}/discussions/categories/${encodeURIComponent(discussionCategory)}`
},
releases: {
label: "Releases",
fields: ["owner", "repo"],
note: "Repository releases page.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}/releases`
},
latestRelease: {
label: "Latest Release",
fields: ["owner", "repo"],
note: "Redirects to the latest release.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}/releases/latest`
},
releaseTag: {
label: "Specific Release Tag",
fields: ["owner", "repo", "releaseTag"],
note: "Direct link to a particular release tag.",
build: ({ owner, repo, releaseTag }) =>
`https://github.com/${owner}/${repo}/releases/tag/${encodeURIComponent(releaseTag)}`
},
wiki: {
label: "Wiki",
fields: ["owner", "repo"],
note: "Repository wiki home.",
build: ({ owner, repo }) => `https://github.com/${owner}/${repo}/wiki`
},
wikiPage: {
label: "Wiki Page",
fields: ["owner", "repo", "wikiPage"],
note: "Direct link to a wiki page.",
build: ({ owner, repo, wikiPage }) =>
`https://github.com/${owner}/${repo}/wiki/${encodeURIComponent(wikiPage)}`
},
pages: {
label: "GitHub Pages Site",
fields: ["owner", "repo"],
note: "Standard Pages URL pattern for a project site.",
build: ({ owner, repo }) => `https://${owner}.github.io/${repo}/`
},
file: {
label: "File View",
fields: ["owner", "repo", "branch", "path"],
note: "GitHub file view using /blob/.",
build: ({ owner, repo, branch, path }) =>
`https://github.com/${owner}/${repo}/blob/${branch}/${cleanPath(path)}`
},
rawFile: {
label: "Raw File",
fields: ["owner", "repo", "branch", "path"],
note: "Raw file URL from raw.githubusercontent.com.",
build: ({ owner, repo, branch, path }) =>
`https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${cleanPath(path)}`
},
folder: {
label: "Folder View",
fields: ["owner", "repo", "branch", "path"],
note: "GitHub folder view using /tree/.",
build: ({ owner, repo, branch, path }) =>
`https://github.com/${owner}/${repo}/tree/${branch}/${cleanPath(path)}`
},
editFile: {
label: "Edit File",
fields: ["owner", "repo", "branch", "path"],
note: "Direct edit link for a file in the browser.",
build: ({ owner, repo, branch, path }) =>
`https://github.com/${owner}/${repo}/edit/${branch}/${cleanPath(path)}`
},
deleteFile: {
label: "Delete File",
fields: ["owner", "repo", "branch", "path"],
note: "Direct delete link for a file in the browser.",
build: ({ owner, repo, branch, path }) =>
`https://github.com/${owner}/${repo}/delete/${branch}/${cleanPath(path)}`
},
compare: {
label: "Compare Branches",
fields: ["owner", "repo", "base", "head"],
note: "Compare base and head refs.",
build: ({ owner, repo, base, head }) =>
`https://github.com/${owner}/${repo}/compare/${encodeURIComponent(base)}...${encodeURIComponent(head)}`
},
commit: {
label: "Commit",
fields: ["owner", "repo", "commit"],
note: "Direct commit URL.",
build: ({ owner, repo, commit }) =>
`https://github.com/${owner}/${repo}/commit/${encodeURIComponent(commit)}`
}
};
function cleanPath(path) {
return String(path || "").replace(/^\/+/, "");
}
function escapeHtml(value) {
return String(value)
.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/</g, "<")
.replace(/>/g, ">");
}
function buildBadgeImageUrl(label, color) {
const safeLabel = encodeURIComponent(label || "Open on GitHub");
const safeColor = encodeURIComponent(color || "pink");
return `https://img.shields.io/badge/${safeLabel}-${safeColor}?style=for-the-badge&logo=github`;
}
const patternSelect = document.getElementById("pattern");
const patternNote = document.getElementById("patternNote");
const dynamicFields = document.querySelectorAll(".dynamic-field");
const inputs = {
owner: document.getElementById("owner"),
repo: document.getElementById("repo"),
branch: document.getElementById("branch"),
path: document.getElementById("path"),
template: document.getElementById("template"),
labels: document.getElementById("labels"),
title: document.getElementById("title"),
body: document.getElementById("body"),
discussionCategory: document.getElementById("discussionCategory"),
wikiPage: document.getElementById("wikiPage"),
base: document.getElementById("base"),
head: document.getElementById("head"),
commit: document.getElementById("commit"),
releaseTag: document.getElementById("releaseTag"),
linkText: document.getElementById("linkText"),
badgeLabel: document.getElementById("badgeLabel"),
badgeColor: document.getElementById("badgeColor")
};
const outputs = {
plainUrl: document.getElementById("plainUrl"),
markdownLink: document.getElementById("markdownLink"),
badgeMarkdown: document.getElementById("badgeMarkdown"),
htmlLink: document.getElementById("htmlLink")
};
const liveLinkPreview = document.getElementById("liveLinkPreview");
const liveBadgePreviewLink = document.getElementById("liveBadgePreviewLink");
const liveBadgePreviewImg = document.getElementById("liveBadgePreviewImg");
function populatePatternOptions() {
Object.entries(patterns).forEach(([key, config]) => {
const option = document.createElement("option");
option.value = key;
option.textContent = config.label;
patternSelect.appendChild(option);
});
}
function updateVisibleFields() {
const selected = patterns[patternSelect.value];
const needed = new Set(selected.fields);
dynamicFields.forEach((field) => {
const name = field.getAttribute("data-field");
field.classList.toggle("hidden", !needed.has(name));
});
patternNote.textContent = selected.note || "";
}
function getFormValues() {
return Object.fromEntries(
Object.entries(inputs).map(([key, el]) => [key, el.value.trim()])
);
}
function validate(values, selectedPattern) {
const required = selectedPattern.fields;
const missing = required.filter((key) => !values[key]);
if (missing.length) {
alert(`Please fill in: ${missing.join(", ")}`);
return false;
}
return true;
}
function generateOutputs() {
const selectedPattern = patterns[patternSelect.value];
const values = getFormValues();
if (!validate(values, selectedPattern)) return;
const url = selectedPattern.build(values);
const linkText = values.linkText || "Open on GitHub";
const badgeUrl = buildBadgeImageUrl(values.badgeLabel, values.badgeColor);
const markdownLink = `[${linkText}](${url})`;
const badgeMarkdown = `[(${url})`;
const htmlLink = `<a href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">${escapeHtml(linkText)}</a>`;
outputs.plainUrl.value = url;
outputs.markdownLink.value = markdownLink;
outputs.badgeMarkdown.value = badgeMarkdown;
outputs.htmlLink.value = htmlLink;
liveLinkPreview.href = url;
liveLinkPreview.textContent = linkText;
liveBadgePreviewLink.href = url;
liveBadgePreviewImg.src = badgeUrl;
liveBadgePreviewImg.alt = values.badgeLabel || "Open on GitHub";
}
function resetForm() {
Object.values(inputs).forEach((el) => {
if (el.tagName === "TEXTAREA" || el.tagName === "INPUT") {
if (el.id === "branch" || el.id === "base") {
el.value = "main";
} else if (el.id === "linkText") {
el.value = "Open on GitHub";
} else if (el.id === "badgeLabel") {
el.value = "Open on GitHub";
} else if (el.id === "badgeColor") {
el.value = "pink";
} else {
el.value = "";
}
}
});
patternSelect.value = "repo";
updateVisibleFields();
Object.values(outputs).forEach((el) => (el.value = ""));
liveLinkPreview.href = "#";
liveLinkPreview.textContent = "Open on GitHub";
liveBadgePreviewLink.href = "#";
liveBadgePreviewImg.removeAttribute("src");
liveBadgePreviewImg.alt = "Badge preview";
}
async function copyTarget(id, button) {
const target = document.getElementById(id);
if (!target || !target.value) return;
try {
await navigator.clipboard.writeText(target.value);
const old = button.textContent;
button.textContent = "Copied";
setTimeout(() => {
button.textContent = old;
}, 1200);
} catch (err) {
alert("Copy failed. You can still copy it manually.");
}
}
document.getElementById("generateBtn").addEventListener("click", generateOutputs);
document.getElementById("resetBtn").addEventListener("click", resetForm);
patternSelect.addEventListener("change", updateVisibleFields);
document.querySelectorAll(".copy-btn").forEach((btn) => {
btn.addEventListener("click", () => {
copyTarget(btn.getAttribute("data-copy-target"), btn);
});
});
populatePatternOptions();
patternSelect.value = "repo";
updateVisibleFields();
</script>
</body>
</html>