forked from karrot-dev/yunity.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.js
38 lines (34 loc) · 871 Bytes
/
clean.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
var sanitizeHtml = require('sanitize-html');
var toMarkdown = require('to-markdown');
var fs = require('fs');
var args = process.argv.slice(2);
var dirty = fs.readFileSync(args[0]);
var clean = sanitizeHtml(dirty, {
allowedTags: [
'p', 'img', 'em', 'a', 'blockquote',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'hr',
'ul', 'ol', 'li'
],
allowedAttributes: {
'a': ['href'],
'img': ['src', 'title']
}
});
var markdown = toMarkdown(clean, {
gfm: true,
converters: [
{
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
replacement: function(innerHTML, node) {
var hLevel = parseInt(node.tagName.charAt(1), 10) + 1;
var hPrefix = '';
for(var i = 0; i < hLevel; i++) {
hPrefix += '#';
}
return '\n' + hPrefix + ' ' + innerHTML + '\n\n';
}
}
]
});
console.log(markdown);