-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitize.js
63 lines (58 loc) · 1.96 KB
/
sanitize.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
54
55
56
57
58
59
60
61
62
63
const ben = require('ben-jsutils');
const he = require('he');
const path = './Code2/';
ben.fs.listDir(path).then(titles => {
console.log(titles);
titles.forEach((t, ti) => {
ben.fs.readFile(path+t).then(text => {
// console.log(text);
// const idinds = getIndicesOf('id="id', text);
// idinds.forEach(ii => {
// console.log(ii);
// text = text.slice(0, ii-1) + text.slice(ii+43);
// });
// let idind = text.indexOf('id="id');
// while (idind > -1) {
// // console.log(idind);
// text = text.slice(0, idind-1) + text.slice(idind+43);
// idind = text.indexOf('id="id');
// }
// const textarray = text.split('id="');
// const sanaray = textarray.map(te => {
// if (te[0] === 'i' && te[1] === 'd') {
// return te.slice(40);
// }
// return te;
// });
let lines = text.split("\n");
let newstr = lines.filter(l => !l.startsWith('<!--')).map(he.decode).join('\n');
// console.log(strip_html_tags(newstr));
// console.log(sanaray);
// console.log(text);
ben.fs.writeFile(path+t, strip_html_tags(newstr));
});
});
});
function strip_html_tags(str) {
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace(/<[^>]*>/g, '');
}
function getIndicesOf(searchStr, str, caseSensitive) {
var searchStrLen = searchStr.length;
if (searchStrLen == 0) {
return [];
}
var startIndex = 0, index, indices = [];
if (!caseSensitive) {
str = str.toLowerCase();
searchStr = searchStr.toLowerCase();
}
while ((index = str.indexOf(searchStr, startIndex)) > -1) {
indices.push(index);
startIndex = index + searchStrLen;
}
return indices;
}