utils.js
1.52 KB
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
'use strict';
const _ = require('lodash');
const cheerio = require('cheerio');
const util = {
// 过滤指定字符的p标签
filterPhtml: (html, filters) => {
if (!html) {
return html;
}
let $ = cheerio.load(html, {decodeEntities: false});
_.each($('p'), (item) => {
let ele = $(item);
let phtml = ele.html();
_.each(filters, ft => {
if (phtml.indexOf(ft) >= 0) {
ele.remove();
}
});
});
html = $.html();
$ = '';
return html;
},
// 过滤 a标签连接和删除html标签中的script和link脚本
filterAhtml: (html) => {
if (!html) {
return html;
}
let $ = cheerio.load(html, {decodeEntities: false});
$('a:not(.a-anchor)').removeAttr('style').attr('href', 'javascript:void(0);').css({cursor: 'text'});// eslint-disable-line
$('script,link').remove();
html = $.html();
$ = '';
return html;
},
// 过滤 a标签连接和删除html标签中的script和link脚本
imgAlt: (html, alt, num) => {
if (!html) {
return html;
}
let $ = cheerio.load(html, {decodeEntities: false});
_.each($('img').slice(0, num), item => {
let $dom = $(item);
$dom.attr('alt', $dom.attr('alt') || alt);
});
html = $.html();
$ = '';
return html;
}
};
module.exports = util;