forked from kurttheviking/html2plaintext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (86 loc) · 2.1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var cheerio = require('cheerio');
var decode = require('he').decode;
var plumb = require('plumb');
// "private" helper for ensuring html entities are properly escaped
function _escapeHtml (input) {
return String(input)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// "private" helper for list processing into plaintext
function _list (str, isOrdered) {
if (!str) return str;
var $ = cheerio.load(str);
var listEl = isOrdered ? 'ol' : 'ul';
$(listEl).each(function (i, el) {
var $out = cheerio.load('<p></p>');
var $el = $(el);
$el.find('li').each(function (j, li) {
var tick = isOrdered ? String(j + 1) + '.' : '-';
$out('p').append(tick + ' ' + _escapeHtml($(li).text()) + '<br />');
});
// avoid excess spacing coming off last element
// (we are wrapping with a <p> anyway)
$out('br').last().remove();
$el.replaceWith($out.html());
});
return $.html();
}
function stripStylesAndScripts(str) {
var $ = cheerio.load(str);
$('script').remove();
$('style').remove();
return $.html();
}
function stringify(x) {
if (x === null || x === undefined) {
return ''
};
return String(x);
}
function collapseWhitespace (val) {
var output = val.replace(/\s+/g, ' ');
return output;
}
function linebreaks (str) {
var output = str.replace(/<\s?(p|br|div)[^<]*>/gi, function (x, tag) {
switch (tag.toLowerCase()) {
case 'p':
return '\n\n';
case 'br':
return '\n';
}
return x;
});
return output;
}
function listOrdered (str) {
return _list(str, true);
}
function listUnordered (str) {
return _list(str, false);
}
function stripCssConditionalComment (str) {
return str.replace(/<!--\[if.*?<!\[endif\]-->/g, '');
}
function stripTags (str) {
return str.replace(/<[^<]+>/g, '');
}
function trim (str) {
return str.trim();
}
module.exports = plumb(
stringify,
stripStylesAndScripts,
listOrdered,
listUnordered,
collapseWhitespace,
linebreaks,
stripCssConditionalComment,
stripTags,
decode,
trim
);