-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.html
90 lines (73 loc) · 2.24 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Playground for slug module</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<main>
<h1>Playground for slug module</h1>
<form id="form" onSubmit="" style="max-width:60ch">
<label for="input">Input text</label>
<input type="text" id="input" placeholder="An Apple computer" autofocus>
<input type="submit" value="Slug it">
<div>Result: <mark id="slugOutput"></mark></div>
<fieldset>
<legend>Options</legend>
<div>
<label for="replacement">Replacement character</label>
<input type="text" id="replacement" name="replacement" placeholder="-">
</div>
<div>
<label>
<input type="checkbox" name="lowercase" checked> Lowercase
</label>
</div>
<div>
<label>
<input type="checkbox" name="trim" checked> Trim leading and trailing replacement characters
</label>
</div>
<div>
<label>
<input type="checkbox" name="fallback" checked> Fallback
</label>
</div>
</fieldset>
</form>
</main>
<script type="module">
import slug from './slug.js';
const form = document.getElementById('form')
form.addEventListener('submit', function (e) {
e.preventDefault()
if (form === null) {
console.error('form element not found')
return
}
const fd = new FormData(form)
const replacement = fd.get('replacement')
const lowercase = fd.get('lowercase')
const trim = fd.get('trim')
const fallback = fd.get('fallback')
const remove = fd.get('remove')
const regexG = fd.get('regex_g') === null ? '' : 'g'
const regexI = fd.get('regex_i') === null ? '' : 'i'
const opts = {}
if (replacement.length > 0) {
opts.replacement = replacement
}
if (remove !== null && remove.length > 0) {
const regex = new RegExp(`/${remove}/${regexG}${regexI}`)
opts.remove = regex
}
opts.lower = lowercase !== null
opts.trim = trim !== null
opts.fallback = fallback !== null
const output = slug(document.getElementById('input').value, opts)
document.getElementById('slugOutput').innerText = output
})
</script>
</body>
</html>