Skip to content

Commit 1c4fe72

Browse files
author
zhouhongxuan
committed
feat(build): record progress
1 parent aecf176 commit 1c4fe72

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

script/md2html.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ const path = require('path');
33
const fs = require('fs');
44

55
function convertMd2Html() {
6-
const md = fs.readFileSync(path.resolve(__dirname, '../README.md'), 'utf-8');
6+
let md = fs.readFileSync(path.resolve(__dirname, '../README.md'), 'utf-8');
7+
const splitPattern = '## 0. 年度报告';
8+
const parts = md.split(splitPattern);
9+
parts[1] = parts[1].replace(/- \[/g, '- [ ] [');
10+
md = parts.join(splitPattern);
11+
712
showdown.setFlavor('github');
813
const converter = new showdown.Converter();
914
const mdHtml = converter.makeHtml(md);

script/tpl.html

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,91 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="X-UA-Compatible" content="ie=edge">
77
{{style}}
8+
<style>
9+
main input[type=checkbox] {
10+
opacity: 1;
11+
width: auto;
12+
line-height: 1;
13+
height: 1.5em;
14+
}
15+
.progress {
16+
position: fixed;
17+
top: 80px;
18+
right: 40px;
19+
border-left: 5px solid #ddd;
20+
padding-left: 15px;
21+
font-size: 18px;
22+
color: #666;
23+
}
24+
25+
.progress span {
26+
color: #3bc536;
27+
}
28+
</style>
829
<title>Frontend Tech List: an overall frontend-tech list for developers</title>
930
</head>
1031
<body>
11-
<main style="margin: 50px auto; max-width: 800px;">
32+
<div class="progress">当前已完成阅读量:<span id="js-progress">10%</span></div>
33+
<main id="js-main" style="margin: 50px auto; max-width: 800px;">
1234
{{md}}
1335
</main>
36+
<script>
37+
var itemKey = 'hasRead';
38+
function updateProgress(readNum, total) {
39+
var percentage = Math.ceil(readNum / total * 100);
40+
document.getElementById('js-progress').textContent = `${percentage}%`;
41+
}
42+
43+
function init() {
44+
var hasRead = localStorage.getItem(itemKey);
45+
try {
46+
hasRead = JSON.parse(hasRead) || [];
47+
}
48+
catch (e) {
49+
console.error(e);
50+
hasRead = [];
51+
}
52+
53+
var $checkboxList = document.querySelectorAll('[type=checkbox]');
54+
$checkboxList.forEach(function ($n) {
55+
$n.removeAttribute('disabled');
56+
var cnt = $n.nextElementSibling.textContent;
57+
$n.dataset.txt = cnt;
58+
if (hasRead.indexOf(cnt) > -1) {
59+
$n.checked = true;
60+
}
61+
});
62+
63+
document.getElementById('js-main').dataset.total = $checkboxList.length;
64+
updateProgress(hasRead.length, $checkboxList.length);
65+
}
66+
67+
init();
68+
69+
document.addEventListener('click', function (e) {
70+
var target = e.target;
71+
if (target.tagName === 'INPUT') {
72+
var txt = target.dataset.txt;
73+
var hasRead = localStorage.getItem(itemKey);
74+
try {
75+
hasRead = JSON.parse(hasRead) || [];
76+
}
77+
catch (e) {
78+
console.error(e);
79+
hasRead = [];
80+
}
81+
var idx = hasRead.indexOf(txt);
82+
if (target.checked && idx < 0) {
83+
hasRead.push(txt);
84+
}
85+
else if (!target.checked && idx > -1) {
86+
hasRead.splice(idx, 1);
87+
}
88+
localStorage.setItem(itemKey, JSON.stringify(hasRead));
89+
90+
updateProgress(hasRead.length, document.getElementById('js-main').dataset.total);
91+
}
92+
});
93+
</script>
1494
</body>
1595
</html>

0 commit comments

Comments
 (0)