-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
52 lines (47 loc) · 1.17 KB
/
index.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
'use strict';
var stripColorCodes = require('stripcolorcodes');
var ultramarked = require('ultramarked');
var cheerio = require('cheerio');
var htmlmd = require('html-md-2');
var he = require('he');
function filterInput (input, options) {
if (!options.ignore && !options.root) {
return input;
}
var $ = cheerio.load(input);
var ignored;
var ignores = options.ignore;
var rooted = $;
var root = options.root;
if (root) {
rooted = rooted(root).eq(0);
}
if (ignores) {
ignored = Array.isArray(ignores) ? ignores.join(',') : ignores;
rooted.find(ignored).remove();
}
var html = rooted.html();
return html;
}
function parse (input, options) {
var o = options || {};
if (!o.root) { o.root = 'body,*'; }
var html = filterInput(input, o);
if (o.html) {
return sanitize(html);
}
var md = htmlmd(html);
if (o.markdown) {
return sanitize(md);
}
var term = ultramarked(md, { terminal: true });
var stripped = stripColorCodes(term);
var sanitized = sanitize(stripped);
return sanitized;
}
function sanitize (input) {
var decoded = he.decode(input);
var trimmed = decoded.trim();
return trimmed;
}
module.exports = parse;