-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimes_unfluff.js
47 lines (40 loc) · 1.27 KB
/
times_unfluff.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
var cheerio = require('cheerio');
var unfluff_wrapper = require('./unfluff_wrapper.js');
function getLinks(html) {
link_urls = [];
var $ = cheerio.load(html);
link_objs = $('p.story-body-text > a');
Object.values(link_objs).forEach(function (currentValue) {
if (typeof currentValue === 'object' && 'attribs' in currentValue) {
link_url = currentValue['attribs']['href'];
text_children = currentValue['children'].filter((child) => {
return child['type'] === 'text';
});
//index.js are expecting link object in this format
link_urls.push({ "href": link_url, "text": (text_children.length > 0 ? text_children[0]['data'] : "") });
}
});
return link_urls;
}
function getText(html) {
var $ = cheerio.load(html);
var textTags = $('.story-body-text')
return textTags.text()
}
function times_unfluff(html, overriden_elements) {
unfluffed = times_unfluff.super_.apply(this, [html,
{
links: getLinks(html),
text: getText(html)
}
]);
return unfluffed
}
times_unfluff.super_ = unfluff_wrapper;
times_unfluff.prototype = Object.create(unfluff_wrapper.prototype, {
constructor: {
value: times_unfluff,
enumerable: false
}
});
module.exports = times_unfluff;