-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
348 lines (315 loc) · 14.9 KB
/
index.html
File metadata and controls
348 lines (315 loc) · 14.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<link rel="manifest" href="/manifest.webmanifest" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-title" content="slugit" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
<meta name="msapplication-TileColor" content="#4A4972" />
<meta name="theme-color" content="#4A4972" />
<title>slugit</title>
<meta name="description" content="slugit is a handy URL based slug generator" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="slugit" />
<meta name="twitter:description" content="slugit is a handy URL based slug generator" />
<meta name="twitter:image" content="https://slugit.dev/android-chrome-512x512.png" />
<meta name="twitter:creator" content="@fgili0" />
<meta property="og:url" content="https://slugit.dev/" />
<meta property="og:type" content="article" />
<meta property="og:title" content="slugit is a handy URL based slug generator" />
<meta property="og:description" content="slugit" />
<meta property="og:image" content="https://slugit.dev/android-chrome-512x512.png" />
</head>
<body>
<main>
<p></p>
<input type="text" />
<br>
<h1 title="Click to copy to clipboard"></h1>
<ul>
<li>
<a href="https://github.com/fgilio/slugit">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<title>GitHub</title>
<path
d="M10 0a10 10 0 0 0-3.16 19.49c.5.1.68-.22.68-.48l-.01-1.7c-2.78.6-3.37-1.34-3.37-1.34-.46-1.16-1.11-1.47-1.11-1.47-.9-.62.07-.6.07-.6 1 .07 1.53 1.03 1.53 1.03.9 1.52 2.34 1.08 2.91.83.1-.65.35-1.09.63-1.34-2.22-.25-4.55-1.11-4.55-4.94 0-1.1.39-1.99 1.03-2.69a3.6 3.6 0 0 1 .1-2.64s.84-.27 2.75 1.02a9.58 9.58 0 0 1 5 0c1.91-1.3 2.75-1.02 2.75-1.02.55 1.37.2 2.4.1 2.64.64.7 1.03 1.6 1.03 2.69 0 3.84-2.34 4.68-4.57 4.93.36.31.68.92.68 1.85l-.01 2.75c0 .26.18.58.69.48A10 10 0 0 0 10 0"
></path>
</svg>
</a>
</li>
<li>
<a href="https://twitter.com/fgili0/">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<title>👋🏼 Twitter</title>
<path
d="M6.29 18.25c7.55 0 11.67-6.25 11.67-11.67v-.53c.8-.59 1.49-1.3 2.04-2.13-.75.33-1.54.55-2.36.65a4.12 4.12 0 0 0 1.8-2.27c-.8.48-1.68.81-2.6 1a4.1 4.1 0 0 0-7 3.74 11.65 11.65 0 0 1-8.45-4.3 4.1 4.1 0 0 0 1.27 5.49C2.01 8.2 1.37 8.03.8 7.7v.05a4.1 4.1 0 0 0 3.3 4.03 4.1 4.1 0 0 1-1.86.07 4.1 4.1 0 0 0 3.83 2.85A8.23 8.23 0 0 1 0 16.4a11.62 11.62 0 0 0 6.29 1.84"
></path>
</svg>
</a>
</li>
</ul>
</main>
<style>
html {
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
height: 100vh;
background-color: #f4f4f5;
padding: 0;
margin: 0;
-webkit-font-smoothing: antialiased !important;
-moz-osx-font-smoothing: grayscale !important;
}
main {
width: 100%;
margin: 0 20px;
}
p {
font-size: 15.75px;
line-height: 1.45;
font-family: monospace;
text-align: left;
color: #495254;
padding: 3px 12px;
margin: 0;
width: 90%;
}
input {
font-size: 20px;
line-height: 1.45;
font-family: monospace;
color: #495254;
border-radius: 3px;
border-style: hidden;
padding: 3px 12px;
margin: 30px 0 10px 0;
width: 90%;
}
input:focus {
box-shadow: 0 0 1px rgba(74, 73, 114, 0.26) inset, 0 0 8px rgba(74, 73, 114, 0.1);
outline: 0 none;
}
h1 {
position: relative;
display: inline-block;
font-size: 30px;
line-height: 1.45;
font-family: monospace;
color: #495254;
-webkit-user-select: all;
-moz-user-select: all;
user-select: all;
border-radius: 3px;
transition: all 70ms ease-out;
padding: 10px 40px 10px 10px;
margin: 0;
}
h1:after {
display: block;
height: 30px;
width: 30px;
position: absolute;
top: 50%;
right: 5px;
transform: translateY(-50%);
content: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 128 128' style='enable-background:new 0 0 128 128;' xml:space='preserve'%3E%3Cpath d='M73.5,14.5H45.1c-5.3,0-9.6,4.1-9.6,9.4v2h-1.8c-5.3,0-9.6,4.1-9.6,9.4v68.6c0,5.3,4.4,9.6,9.6,9.6h49.5 c5.3,0,9.4-4.4,9.4-9.6v-1.8h2c5.3,0,9.4-4.4,9.4-9.6V45L73.5,14.5z M73.5,25.1L93.4,45H73.5V25.1z M85,103.9c0,1.1-0.8,2-1.8,2 H33.6c-1,0-2-1-2-2V35.3c0-1,0.9-1.8,2-1.8h1.8v60.8c0,5.3,2.5,7.7,7.7,7.7H85V103.9z M96.4,92.5c0,1.1-0.8,2-1.8,2H45.1 c-1,0-2-1-2-2V23.9c0-1,0.9-1.8,2-1.8h20.8v30.5h30.5V92.5z'/%3E%3C/svg%3E%0A");
opacity: 0.5;
transition: all 70ms ease-out;
}
h1:hover {
background-color: hsl(191, 7%, 31%, 0.1);
transition: all 10ms ease-in;
}
h1:active {
background-color: hsl(191, 7%, 31%, 0.3);
transition: all 10ms ease-in;
color: white;
background-color: #4a4972;
}
h1:active:after {
transition: all 10ms ease-in;
filter: invert(1);
opacity: 1;
}
h1::selection {
background-color: transparent;
}
ul {
list-style: none;
position: fixed;
bottom: 10px;
display: block;
width: 100%;
text-align: center;
right: 0;
left: 0;
margin: 0;
padding: 0;
}
li {
display: inline-block;
width: 30px;
height: 30px;
padding: 10px;
opacity: 0.7;
}
</style>
<script>
/*
* From https://medium.com/@mhagemann/the-ultimate-way-to-slugify-a-url-string-in-javascript-b8e4a0d849e1
*/
const slugify = string => {
const a = 'àáäâãåăæçèéëêǵḧìíïîḿńǹñòóöôœøṕŕßśșțùúüûǘẃẍÿź·/_,:;';
const b = 'aaaaaaaaceeeeghiiiimnnnooooooprssstuuuuuwxyz------';
const p = new RegExp(a.split('').join('|'), 'g');
return string
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, '-and-') // Replace & with ‘and’
.replace(/[^\w\-]+/g, '') // Remove all non-word characters
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
};
const copyToClipboard = string => {
const el = document.createElement('textarea');
el.value = string;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
/*
* decodeURI doesn't cut it because we may have actual % in the path.
* https://github.com/SamVerschueren/decode-uri-component
*/
const decodePath = encodedPath => {
const token = '%[a-f0-9]{2}';
const singleMatcher = new RegExp(token, 'gi');
const multiMatcher = new RegExp(`(${token})+`, 'gi');
function decodeComponents(components, split) {
try {
// Try to decode the entire string first
return decodeURIComponent(components.join(''));
} catch (err) {
// Do nothing
}
if (components.length === 1) {
return components;
}
split = split || 1;
// Split the array in 2 parts
const left = components.slice(0, split);
const right = components.slice(split);
return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
}
function decode(input) {
try {
return decodeURIComponent(input);
} catch (err) {
let tokens = input.match(singleMatcher);
for (let i = 1; i < tokens.length; i++) {
input = decodeComponents(tokens, i).join('');
tokens = input.match(singleMatcher);
}
return input;
}
}
function customDecodeURIComponent(input) {
// Keep track of all the replacements and prefill the map with the `BOM`
const replaceMap = {
'%FE%FF': '\uFFFD\uFFFD',
'%FF%FE': '\uFFFD\uFFFD',
};
let match = multiMatcher.exec(input);
while (match) {
try {
// Decode as big chunks as possible
replaceMap[match[0]] = decodeURIComponent(match[0]);
} catch (err) {
const result = decode(match[0]);
if (result !== match[0]) {
replaceMap[match[0]] = result;
}
}
match = multiMatcher.exec(input);
}
// Add `%C2` at the end of the map to make sure it does not replace the combinator before everything else
replaceMap['%C2'] = '\uFFFD';
const entries = Object.keys(replaceMap);
for (let i = 0; i < entries.length; i++) {
// Replace all decoded components
const key = entries[i];
input = input.replace(new RegExp(key, 'g'), replaceMap[key]);
}
return input;
}
if (typeof encodedPath !== 'string') {
throw new TypeError(
`Expected \`encodedPath\` to be of type \`string\`, got \`${typeof encodedPath}\``
);
}
try {
encodedPath = encodedPath.replace(/\+/g, ' ');
// Try the built in decoder first
return decodeURIComponent(encodedPath);
} catch (err) {
// Fallback to a more advanced decoder
return customDecodeURIComponent(encodedPath);
}
};
const displaySlug = string => {
let pathSlug = '';
try {
pathSlug = slugify(string) || '...';
} catch (err) {
pathSlug = 'derp';
console.error('Parameter `url` must contain valid UTF-8 character sequences');
}
document.querySelector('h1').textContent = pathSlug;
document.querySelector('title').textContent = pathSlug + ' - slugit';
};
/*
* We receive the path as an URL encoded string.
* Which means an `á` will be represented as `%C3%A1`.
* So we use decodePath to get the decoded string.
*/
const pathname = window.location.href.replace(window.location.origin, '');
if (pathname === '/') {
document.querySelector('p').innerHTML = `Use it like this:<br><a href="${
window.location.origin
}/give me a slug">${window.location.origin}/give me a slug`;
}
const defaultt = '...or write here';
const path = decodePath(pathname);
displaySlug(path !== '/' ? path : defaultt);
document.querySelector('input').value = path.slice(1) || defaultt;
document.querySelector('input').addEventListener('keyup', event => {
displaySlug(event.target.value);
});
document.querySelector('h1').addEventListener('click', e => copyToClipboard(e.target.textContent));
</script>
<script>
navigator.serviceWorker.register('/service-worker.js');
navigator.serviceWorker.addEventListener('controllerchange', () => {
console.log('Service Worker changed, reload');
window.location.reload();
});
</script>
</body>
</html>