-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub-link-generator.html
More file actions
747 lines (650 loc) · 22.7 KB
/
github-link-generator.html
File metadata and controls
747 lines (650 loc) · 22.7 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GitHub Link Generator</title>
<style>
html { -webkit-text-size-adjust: 100%; }
img, video, canvas, svg { max-width: 100%; height: auto; }
:root {
--bg: #f7f8fc;
--panel: #ffffff;
--text: #1f2937;
--muted: #6b7280;
--border: #dbe1ea;
--accent: #ec4899;
--accent-dark: #db2777;
--shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
--radius: 18px;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
background:
radial-gradient(circle at top left, rgba(236,72,153,0.08), transparent 25%),
linear-gradient(180deg, #fafbff 0%, #f5f7fb 100%);
color: var(--text);
}
.wrap {
max-width: 1100px;
margin: 0 auto;
padding: 28px 18px 40px;
}
.hero {
margin-bottom: 22px;
}
.hero h1 {
margin: 0 0 8px;
font-size: clamp(1.9rem, 4vw, 2.9rem);
line-height: 1.05;
}
.hero p {
margin: 0;
color: var(--muted);
font-size: 1rem;
max-width: 760px;
}
.grid {
display: grid;
grid-template-columns: 1.1fr 1fr;
gap: 20px;
}
.card {
background: var(--panel);
border: 1px solid var(--border);
border-radius: var(--radius);
box-shadow: var(--shadow);
padding: 18px;
}
.card h2 {
margin: 0 0 14px;
font-size: 1.1rem;
}
.form-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
}
.field {
display: flex;
flex-direction: column;
gap: 7px;
}
.field.full {
grid-column: 1 / -1;
}
label {
font-size: 0.92rem;
font-weight: 700;
}
input,
select,
textarea {
width: 100%;
padding: 12px 13px;
border: 1px solid var(--border);
border-radius: 12px;
background: #fff;
color: var(--text);
font: inherit;
}
textarea {
min-height: 110px;
resize: vertical;
line-height: 1.45;
}
input:focus,
select:focus,
textarea:focus {
outline: 2px solid rgba(236,72,153,0.2);
border-color: var(--accent);
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-top: 16px;
}
button {
appearance: none;
border: none;
border-radius: 999px;
padding: 11px 16px;
font: inherit;
font-weight: 700;
cursor: pointer;
transition: transform 0.15s ease, opacity 0.15s ease;
}
button:hover {
transform: translateY(-1px);
}
.primary {
background: var(--accent);
color: white;
}
.primary:hover {
background: var(--accent-dark);
}
.secondary {
background: #f3f4f6;
color: var(--text);
border: 1px solid var(--border);
}
.output-block {
margin-top: 14px;
display: flex;
flex-direction: column;
gap: 8px;
}
.output-block label {
font-size: 0.88rem;
}
.output-row {
display: flex;
gap: 8px;
align-items: stretch;
}
.output-row textarea,
.output-row input {
flex: 1;
min-width: 0;
background: #fcfcff;
}
.copy-btn {
border-radius: 12px;
min-width: 86px;
background: #111827;
color: #fff;
padding: 0 14px;
}
.hint,
.pattern-note {
color: var(--muted);
font-size: 0.9rem;
line-height: 1.45;
}
.preview {
margin-top: 14px;
padding: 14px;
border: 1px dashed var(--border);
border-radius: 14px;
background: #fafbff;
}
.badge-preview img {
max-width: 100%;
height: auto;
}
.hidden {
display: none !important;
}
.inline-note {
margin-top: 8px;
font-size: 0.86rem;
color: var(--muted);
}
code {
background: #f3f4f6;
padding: 2px 6px;
border-radius: 8px;
font-size: 0.95em;
}
@media (max-width: 840px) {
.grid {
grid-template-columns: 1fr;
}
.form-grid {
grid-template-columns: 1fr;
}
.output-row {
flex-direction: column;
}
.copy-btn {
min-height: 42px;
}
}
</style>
</head>
<body>
<div class="wrap">
<section class="hero">
<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="grid">
<section class="card">
<h2>Build Link</h2>
<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">
<h2>Output</h2>
<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>
</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>