-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathslug-generator.html
More file actions
464 lines (408 loc) · 13.4 KB
/
Copy pathslug-generator.html
File metadata and controls
464 lines (408 loc) · 13.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
<!--
Slug Generator
Single-file client-side tool to convert text into clean, SEO-friendly URL
slugs with customizable formatting.
Built for One File Tools.
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Slug Generator - One File Tools</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=Space+Grotesk:wght@600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet" />
<style>
:root {
--bg: #030712;
--grid-line: #111827;
--panel: rgba(17, 24, 39, 0.7);
--panel-border: #1f2937;
--accent: #22d3ee;
--accent-hover: #06b6d4;
--accent-glow: rgba(34, 211, 238, 0.15);
--text: #f9fafb;
--text-muted: #9ca3af;
--text-dim: #4b5563;
--radius-sm: 12px;
--radius-md: 20px;
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
--font-display: "Space Grotesk", sans-serif;
--color-weak: #ef4444;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-sans);
background-color: var(--bg);
background-image: linear-gradient(var(--grid-line) 1px, transparent 1px), linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
background-size: 32px 32px;
color: var(--text);
padding: 48px 20px 80px;
min-height: 100vh;
display: flex;
justify-content: center;
}
.app {
width: 100%;
max-width: 780px;
}
.hero {
text-align: center;
margin-bottom: 28px;
}
.eyebrow {
font-family: var(--font-mono);
font-size: 12px;
letter-spacing: 0.2em;
color: var(--accent);
text-transform: uppercase;
margin-bottom: 12px;
font-weight: 500;
}
.wordmark {
font-family: var(--font-display);
font-weight: 700;
font-size: clamp(26px, 5vw, 38px);
letter-spacing: -0.02em;
margin-bottom: 12px;
}
.tagline {
color: var(--text-muted);
font-size: 15px;
line-height: 1.5;
max-width: 560px;
margin: 0 auto;
}
.studio {
background: var(--panel);
backdrop-filter: blur(20px);
border: 1px solid var(--panel-border);
border-radius: var(--radius-md);
padding: 24px;
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.5);
margin-bottom: 24px;
}
.section-label {
font-family: var(--font-mono);
font-size: 12px;
font-weight: 600;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.1em;
display: block;
margin-bottom: 14px;
}
textarea {
width: 100%;
background: rgba(17, 24, 39, 0.8);
border: 1px solid var(--panel-border);
border-radius: 8px;
color: var(--text);
font-family: var(--font-sans);
font-size: 15px;
padding: 12px 14px;
resize: vertical;
min-height: 80px;
line-height: 1.5;
}
textarea:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.options-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 12px;
margin-top: 18px;
}
.option-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 12px 14px;
}
.option-row .option-label {
font-size: 13px;
color: var(--text-muted);
}
/* Toggle switch */
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 22px;
flex-shrink: 0;
}
.switch input {
position: absolute;
width: 1px;
height: 1px;
opacity: 0;
}
.switch-track {
position: absolute;
inset: 0;
background: rgba(31, 41, 55, 0.8);
border: 1px solid var(--panel-border);
border-radius: 99px;
transition: background 0.2s;
}
.switch-track::before {
content: "";
position: absolute;
top: 2px;
left: 2px;
width: 16px;
height: 16px;
border-radius: 50%;
background: var(--text-muted);
transition: all 0.2s;
}
.switch input:checked + .switch-track {
background: var(--accent);
border-color: var(--accent);
}
.switch input:checked + .switch-track::before {
transform: translateX(18px);
background: #030712;
}
.switch input:focus-visible + .switch-track {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.separator-select {
background: rgba(17, 24, 39, 0.8);
border: 1px solid var(--panel-border);
border-radius: 6px;
color: var(--text);
font-family: var(--font-mono);
font-size: 13px;
padding: 6px 10px;
}
.slug-output {
margin-top: 18px;
font-family: var(--font-mono);
font-size: 18px;
color: var(--accent);
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 16px;
word-break: break-all;
min-height: 30px;
}
.slug-output:empty::before {
content: "your-slug-will-appear-here";
color: var(--text-dim);
}
.actions {
display: flex;
gap: 10px;
margin-top: 14px;
flex-wrap: wrap;
}
.btn {
background: var(--accent);
color: #030712;
border: none;
border-radius: 8px;
padding: 11px 18px;
font-family: var(--font-sans);
font-size: 14px;
font-weight: 700;
cursor: pointer;
}
.btn:hover {
background: var(--accent-hover);
}
.btn:focus-visible,
.btn-ghost:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.btn-ghost {
background: rgba(31, 41, 55, 0.6);
border: 1px solid var(--panel-border);
color: var(--text);
border-radius: 8px;
padding: 11px 16px;
font-family: var(--font-sans);
font-size: 13px;
font-weight: 600;
cursor: pointer;
}
.btn-ghost:hover {
background: rgba(55, 65, 81, 0.8);
}
.footer-note {
text-align: center;
color: var(--text-dim);
font-size: 13px;
margin-top: 8px;
}
.footer-note a {
color: var(--text-muted);
}
.toast {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%) translateY(100px);
background: var(--text);
color: var(--bg);
padding: 12px 22px;
border-radius: 99px;
font-family: var(--font-sans);
font-weight: 600;
font-size: 14px;
opacity: 0;
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
z-index: 100;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.toast.show {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
@media (max-width: 640px) {
.studio {
padding: 18px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="hero">
<div class="eyebrow">Web & SEO</div>
<h1 class="wordmark">Slug Generator</h1>
<p class="tagline">Turn any title or phrase into a clean, SEO-friendly URL slug — instantly, with customizable formatting.</p>
</header>
<main class="studio">
<label class="section-label" for="textInput">Your text</label>
<textarea id="textInput" spellcheck="true" placeholder="Paste a title, heading, or phrase…">Café & Résumé: 10 Tips for Beginners!</textarea>
<div class="options-grid">
<div class="option-row">
<span class="option-label">Lowercase</span>
<label class="switch">
<input type="checkbox" id="toggleLowercase" checked />
<span class="switch-track"></span>
</label>
</div>
<div class="option-row">
<span class="option-label">Keep numbers</span>
<label class="switch">
<input type="checkbox" id="toggleNumbers" checked />
<span class="switch-track"></span>
</label>
</div>
<div class="option-row">
<span class="option-label">Separator</span>
<select id="separatorSelect" class="separator-select">
<option value="-">Hyphen (-)</option>
<option value="_">Underscore (_)</option>
</select>
</div>
</div>
<div class="section-label" style="margin-top: 18px">Generated slug</div>
<div class="slug-output" id="slugOutput"></div>
<div class="actions">
<button class="btn" id="btnCopy" type="button">Copy slug</button>
<button class="btn-ghost" id="btnClear" type="button">Clear</button>
</div>
</main>
<p class="footer-note">Part of <a href="https://github.com/praveenscience/One-File-Tools">One File Tools</a>. Runs completely offline.</p>
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<script>
/* ==========================================================
Slug Generator
Vanilla JS, no dependencies. Uses String.normalize (NFD) to
strip accents/diacritics before slugifying.
========================================================== */
const textInput = document.getElementById("textInput");
const toggleLowercase = document.getElementById("toggleLowercase");
const toggleNumbers = document.getElementById("toggleNumbers");
const separatorSelect = document.getElementById("separatorSelect");
const slugOutput = document.getElementById("slugOutput");
const btnCopy = document.getElementById("btnCopy");
const btnClear = document.getElementById("btnClear");
const toast = document.getElementById("toast");
/** Shows a brief toast, also useful as an aria-live announcement. */
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
setTimeout(() => toast.classList.remove("show"), 2000);
}
/**
* Converts input text into a slug based on the current options:
* - Normalizes accented/Unicode characters to their ASCII base
* (e.g. "café" -> "cafe") via Unicode NFD decomposition.
* - Optionally lowercases.
* - Strips punctuation/special characters (keeps letters, digits, spaces).
* - Optionally strips digits when "keep numbers" is off.
* - Collapses whitespace/repeated separators and trims leading/trailing ones.
*/
function slugify(text) {
let result = text.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); // Strip diacritics.
if (toggleLowercase.checked) {
result = result.toLowerCase();
}
// Keep letters, digits, and whitespace; drop everything else
// (punctuation, symbols, emoji, etc.).
result = result.replace(/[^A-Za-z0-9\s]/g, " ");
if (!toggleNumbers.checked) {
result = result.replace(/[0-9]/g, " ");
}
const separator = separatorSelect.value;
result = result
.trim()
.replace(/\s+/g, separator)
// Collapse any run of the separator (e.g. from adjacent removed chars).
.replace(new RegExp("\\" + separator + "{2,}", "g"), separator)
.replace(new RegExp("^\\" + separator + "+|\\" + separator + "+$", "g"), "");
return result;
}
/** Recomputes and renders the slug from the current input + options. */
function render() {
slugOutput.textContent = slugify(textInput.value);
}
textInput.addEventListener("input", render);
toggleLowercase.addEventListener("change", render);
toggleNumbers.addEventListener("change", render);
separatorSelect.addEventListener("change", render);
btnCopy.addEventListener("click", () => {
const text = slugOutput.textContent;
if (!text) {
showToast("Nothing to copy yet");
return;
}
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard
.writeText(text)
.then(() => showToast("Slug copied!"))
.catch(() => showToast("Copy failed — select the text manually"));
} else {
showToast("Clipboard unavailable");
}
});
btnClear.addEventListener("click", () => {
textInput.value = "";
render();
textInput.focus();
});
render();
</script>
</body>
</html>