-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
53 lines (41 loc) · 1.35 KB
/
script.js
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
getReadmeMD().then(cleanUp)
.then(replaceHeaders).then(makeRulers)
.then(addCheckBoxes).then(insertHTML)
.then(updateCheckboxes).then(assignHandlers)
let checked = JSON.parse(localStorage.poc_faboj_checked || '[]')
function getReadmeMD() {
return fetch('README.md').then(resp => resp.text())
}
function cleanUp(md) {
return md.replace(/\r/g, '').replace(/^\s*(\(|- \[).*$|^$/mg, '')
}
function replaceHeaders(md) {
return md.replace(
/^(#+) (.*)$/mg,
(_, hashes, title) => `<h${hashes.length}>${title}</h${hashes.length}>`
)
}
function makeRulers(md) {
return md.replace(/^\s*---+.*$/mg, '\n<hr>')
}
function addCheckBoxes(md) {
return md.replace(/^(?:- |\d+\. )?([^<\n]+)$/mg, (_, label) => `<label><input type="checkbox"> ${label}</label>`)
}
function insertHTML(html) {
wrapper.innerHTML = html
}
function assignHandlers() {
wrapper.onchange = handleCheck
}
function updateCheckboxes() {
wrapper.querySelectorAll('input').forEach(box => box.checked = checked.includes(box.nextSibling.textContent))
}
function handleCheck(e) {
const box = e.target
const label = box.nextSibling.textContent
if (box.checked && !checked.includes(label)) checked.push(label)
else if (!box.checked && checked.includes(label))
checked = checked.filter(l => l != label)
else return
localStorage.poc_faboj_checked = JSON.stringify(checked)
}