-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.js
788 lines (666 loc) · 24 KB
/
helpers.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
const url = require('url')
/**
* sets the default options
*
* @param {Object} options - the options object
*
* @return {Object} options with defaults set if options are not specified
*
*/
module.exports.setDefaultOptions = function (options) {
if (!options.hasOwnProperty('enabled')) {
options.enabled = []
}
if (!options.hasOwnProperty('puppeteer')) {
options.puppeteer = {}
}
if (!options.puppeteer.hasOwnProperty('launch')) {
options.puppeteer.launch = {
headless: true,
defaultViewport: null,
handleSIGINT: false
}
}
if (!options.puppeteer.hasOwnProperty('goto')) {
options.puppeteer.goto = {
waitUntil: 'networkidle2'
}
}
if (!options.puppeteer.hasOwnProperty('setBypassCSP')) {
options.puppeteer.setBypassCSP = true
}
if (!options.hasOwnProperty('striptags')) {
options.striptags = []
}
if (!options.hasOwnProperty('blockedResourceTypes')) {
options.blockedResourceTypes = []
}
if (!options.hasOwnProperty('skippedResources')) {
options.skippedResources = []
}
if (!options.hasOwnProperty('title')) {
options.title = {}
}
if (!options.hasOwnProperty('nlp')) {
options.nlp = {}
}
if (!options.nlp.hasOwnProperty('plugins')) {
options.nlp.plugins = []
}
if (!options.hasOwnProperty('regex')) {
options.regex = {}
}
if (!options.regex.hasOwnProperty('unlikelyCandidatesRe')) {
options.regex.unlikelyCandidatesRe = /combx|modal|comment|disqus|foot|header|menu|meta|nav|rss|shoutbox|sponsor|social|teaserlist|time|tweet|twitter/i
}
if (!options.regex.hasOwnProperty('okMaybeItsACandidateRe')) {
options.regex.okMaybeItsACandidateRe = /and|article|body|column|main|story|entry|^post/im
}
if (!options.regex.hasOwnProperty('positiveRe')) {
options.regex.positiveRe = /article|body|content|entry|hentry|page|pagination|post|section|chapter|description|main|blog|text/i
}
if (!options.regex.hasOwnProperty('negativeRe')) {
options.regex.negativeRe = /combx|comment|contact|foot|footer|footnote|link|media|meta|promo|related|scroll|shoutbox|sponsor|utility|tags|widget/i
}
if (!options.regex.hasOwnProperty('divToPElementsRe')) {
options.regex.divToPElementsRe = /<(a|blockquote|dl|div|img|ol|p|pre|table|ul)/i
}
if (!options.regex.hasOwnProperty('replaceBrsRe')) {
options.regex.replaceBrsRe = /(<br[^>]*>[ \n\r\t]*){2,}/gi
}
if (!options.regex.hasOwnProperty('replaceFontsRe')) {
options.regex.replaceFontsRe = /<(\/?)font[^>]*>/gi
}
if (!options.regex.hasOwnProperty('trimRe')) {
options.regex.trimRe = /^\s+|\s+$/g
}
if (!options.regex.hasOwnProperty('normalizeRe')) {
options.regex.normalizeRe = /\s{2,}/g
}
if (!options.regex.hasOwnProperty('killBreaksRe')) {
options.regex.killBreaksRe = /(<br\s*\/?>(\s| ?)*){1,}/g
}
if (!options.regex.hasOwnProperty('videoRe')) {
options.regex.videoRe = /http:\/\/(www\.)?(youtube|vimeo|youku|tudou|56|yinyuetai)\.com/i
}
if (!options.regex.hasOwnProperty('attributeRe')) {
options.regex.attributeRe = /blog|post|article/i
}
return options
}
module.exports.capitalizeFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
module.exports.toTitleCase = function (str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
})
}
let debug
const dbg = (debug) ? console.log : function () {}
let cleanRules = []
module.exports.setCleanRules = function (rules) {
cleanRules = rules
}
/**
* Prepare the HTML document for readability to process it.
* This includes things like stripping javascript, CSS, and handling terrible markup.
*
* @param {String} document
*
* @return {Void}
**/
module.exports.prepDocument = function (document) {
const frames = document.getElementsByTagName('frame')
if (frames.length > 0) {
let bestFrame = null
let bestFrameSize = 0
Array.prototype.slice.call(frames, 0).forEach(function (frame) {
const frameSize = frame.offsetWidth + frame.offsetHeight
let canAccessFrame = false
try {
if (frame.contentWindow.document.body) {
canAccessFrame = true
}
} catch (e) {}
if (canAccessFrame && frameSize > bestFrameSize) {
bestFrame = frame
bestFrameSize = frameSize
}
})
if (bestFrame) {
const newBody = document.createElement('body')
newBody.innerHTML = bestFrame.contentWindow.document.body.innerHTML
newBody.style.overflow = 'scroll'
document.body = newBody
const frameset = document.getElementsByTagName('frameset')[0]
if (frameset) {
frameset.parentNode.removeChild(frameset)
}
}
}
// Strip out all <script> tags, as they *should* be useless
const scripts = document.getElementsByTagName('script');
[].forEach.call(scripts, function (node) {
node.parentNode.removeChild(node)
})
// turn all double br's into p's
// note, this is pretty costly as far as processing goes. Maybe optimize later.
// document.body.innerHTML = document.body.innerHTML.replace(regexps.replaceBrsRe, '</p><p>').replace(regexps.replaceFontsRe, '<$1span>');
}
/***
* grabArticle - Using a variety of metrics (content score, classname, element types), find the content that is
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
*
* @return {jQuery}
**/
module.exports.grabArticle = function (document, preserveUnlikelyCandidates, regexps) {
/**
* First, node prepping. Trash nodes that look cruddy (like ones with the class name "comment", etc), and turn divs
* into P tags where they have been used inappropriately (as in, where they contain no other block level elements.)
*
* Note: Assignment from index for performance. See http://www.peachpit.com/articles/article.aspx?p=31567&seqNum=5
* TODO: Shouldn't this be a reverse traversal?
**/
const nodes = document.getElementsByTagName('*')
for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i]
// Remove unlikely candidates */
let continueFlag = false
if (!preserveUnlikelyCandidates) {
const unlikelyMatchString = node.className + '\n' + node.id
if (unlikelyMatchString.search(regexps.unlikelyCandidatesRe) !== -1 && unlikelyMatchString.search(regexps.okMaybeItsACandidateRe) === -1 && node.tagName !== 'HTML' && node.tagName !== 'BODY') {
dbg('Removing unlikely candidate - ' + unlikelyMatchString)
node.parentNode.removeChild(node)
continueFlag = true
}
}
// Turn all divs that don't have children block level elements into p's
if (!continueFlag && node.tagName === 'DIV') {
if (node.innerHTML.search(regexps.divToPElementsRe) === -1) {
dbg('Altering div to p')
const newNode = document.createElement('p')
newNode.innerHTML = node.innerHTML
node.parentNode.replaceChild(newNode, node)
} else {
// EXPERIMENTAL
Array.prototype.slice.call(node.childNodes).forEach(function (childNode) {
if (childNode.nodeType === 3 /* TEXT_NODE */) {
const nextSibling = childNode.nextSibling
if (nextSibling && nextSibling.tagName === 'BR') {
dbg('replacing text node followed by br with a p tag with the same content.')
const p = document.createElement('p')
p.innerHTML = childNode.nodeValue
childNode.parentNode.removeChild(nextSibling)
childNode.parentNode.replaceChild(p, childNode)
} else {
// use span instead of p. Need more tests.
dbg('replacing text node with a span tag with the same content.')
const span = document.createElement('span')
span.innerHTML = childNode.nodeValue
childNode.parentNode.replaceChild(span, childNode)
}
}
})
}
}
}
/**
* Loop through all paragraphs, and assign a score to them based on how content-y they look.
* Then add their score to their parent node.
*
* A score is determined by things like number of commas, class names, etc. Maybe eventually link density.
**/
const allParagraphs = document.getElementsByTagName('p')
const candidates = []
for (let i = 0; i < allParagraphs.length; ++i) {
const paragraph = allParagraphs[i]
const parentNode = paragraph.parentNode
const grandParentNode = parentNode.parentNode
const innerText = getInnerText(paragraph, true, regexps)
// If this paragraph is less than 25 characters, don't even count it.
if (innerText.length < 25) continue
// Initialize readability data for the parent.
if (typeof parentNode.readability === 'undefined') {
initializeNode(parentNode, regexps)
candidates.push(parentNode)
}
// Initialize readability data for the grandparent.
if (typeof grandParentNode.readability === 'undefined') {
initializeNode(grandParentNode, regexps)
candidates.push(grandParentNode)
}
let contentScore = 0
// Add a point for the paragraph itself as a base. */
++contentScore
// Add points for any commas within this paragraph */
contentScore += innerText.replace(',', ',').split(',').length
// For every 100 characters in this paragraph, add another point. Up to 3 points. */
contentScore += Math.min(Math.floor(innerText.length / 100), 3)
// Add the score to the parent. The grandparent gets half. */
parentNode.readability.contentScore += contentScore
grandParentNode.readability.contentScore += contentScore / 2
}
/**
* After we've calculated scores, loop through all of the possible candidate nodes we found
* and find the one with the highest score.
**/
let topCandidate = null
candidates.forEach(function (candidate) {
/**
* Scale the final candidates score based on link density. Good content should have a
* relatively small link density (5% or less) and be mostly unaffected by this operation.
**/
candidate.readability.contentScore = candidate.readability.contentScore * (1 - getLinkDensity(candidate, regexps))
dbg('Candidate: ' + candidate + ' (' + candidate.className + ':' + candidate.id + ') with score ' + candidate.readability.contentScore)
if (!topCandidate || candidate.readability.contentScore > topCandidate.readability.contentScore) topCandidate = candidate
})
/**
* If we still have no top candidate, just use the body as a last resort.
* We also have to copy the body node so it is something we can modify.
**/
if (topCandidate === null || topCandidate.tagName === 'BODY') {
// With no top candidate, bail out if no body tag exists as last resort.
if (!document.body) {
return new Error('No body tag was found.')
}
topCandidate = document.createElement('DIV')
topCandidate.innerHTML = document.body.innerHTML
document.body.innerHTML = ''
document.body.appendChild(topCandidate)
initializeNode(topCandidate, regexps)
}
/**
* Now that we have the top candidate, look through its siblings for content that might also be related.
* Things like preambles, content split by ads that we removed, etc.
**/
const articleContent = document.createElement('DIV')
articleContent.id = 'readability-content'
const siblingScoreThreshold = Math.max(10, topCandidate.readability.contentScore * 0.2)
const siblingNodes = topCandidate.parentNode.childNodes
for (let i = 0, il = siblingNodes.length; i < il; i++) {
const siblingNode = siblingNodes[i]
let append = false
dbg('Looking at sibling node: ' + siblingNode + ' (' + siblingNode.className + ':' + siblingNode.id + ')' + ((typeof siblingNode.readability !== 'undefined') ? (' with score ' + siblingNode.readability.contentScore) : ''))
dbg('Sibling has score ' + (siblingNode.readability ? siblingNode.readability.contentScore : 'Unknown'))
if (siblingNode === topCandidate) {
append = true
}
if (typeof siblingNode.readability !== 'undefined' && siblingNode.readability.contentScore >= siblingScoreThreshold) {
append = true
}
if (siblingNode.nodeName === 'P') {
const linkDensity = getLinkDensity(siblingNode, regexps)
const nodeContent = getInnerText(siblingNode, true, regexps)
const nodeLength = nodeContent.length
if (nodeLength > 80 && linkDensity < 0.25) {
append = true
} else if (nodeLength < 80 && linkDensity === 0 && nodeContent.search(/\.( |$)/) !== -1) {
append = true
}
}
if (append) {
dbg('Appending node: ' + siblingNode)
/* Append sibling and subtract from our list because it removes the node when you append to another node */
articleContent.appendChild(siblingNode)
i--
il--
}
}
/**
* So we have all of the content that we need. Now we clean it up for presentation.
**/
prepArticle(articleContent, regexps)
return articleContent
}
/**
* Remove the style attribute on every e and under.
*
* @param {jQuery} element
* @return {Void}
**/
function cleanStyles (e) {
if (!e) return
// Remove any root styles, if we're able.
if (typeof e.removeAttribute === 'function' && e.className !== 'readability-styled') e.removeAttribute('style')
// Go until there are no more child nodes
let cur = e.firstChild
while (cur) {
if (cur.nodeType === 1) {
// Remove style attribute(s) :
if (cur.className !== 'readability-styled') {
cur.removeAttribute('style')
}
cleanStyles(cur)
}
cur = cur.nextSibling
}
}
/**
* Remove extraneous break tags from a node.
*
* @param {jQuery} element
* @return {Void}
**/
function killBreaks (e, regexps) {
e.innerHTML = e.innerHTML.replace(regexps.killBreaksRe, '<br />')
}
/**
* Get the inner text of a node - cross browser compatibly.
* This also strips out any excess whitespace to be found.
*
* @param {jQuery} element
* @return {String}
**/
function getInnerText (e, normalizeSpaces, regexps) {
let textContent = ''
normalizeSpaces = (typeof normalizeSpaces === 'undefined') ? true : normalizeSpaces
textContent = e.textContent.trim()
if (normalizeSpaces) return textContent.replace(regexps.normalizeRe, ' ')
else return textContent
}
/**
* Get the number of times a string s appears in the node e.
*
* @param {jQuery} element
* @param {string} string - character to split on. Default is ","
* @return {Number} (integer)
**/
function getCharCount (e, s, regexps) {
s = s || ','
return getInnerText(e, true, regexps).split(s).length
}
/**
* Get the density of links as a percentage of the content
* This is the amount of text that is inside a link divided by the total text in the node.
*
* @param {jQuery} element
* @return {Number} (float)
**/
function getLinkDensity (e, regexps) {
const links = e.getElementsByTagName('a')
const textLength = getInnerText(e, true, regexps).length
let linkLength = 0
for (let i = 0, il = links.length; i < il; i++) {
const href = links[i].getAttribute('href')
// hack for <h2><a href="#menu"></a></h2> / <h2><a></a></h2>
if (!href || (href.length > 0 && href[0] === '#')) continue
linkLength += getInnerText(links[i], true, regexps).length
}
return linkLength / textLength
}
/**
* Get an elements class/id weight. Uses regular expressions to tell if this
* element looks good or bad.
*
* @param {jQuery} element
* @return {Number} (Integer)
**/
function getClassWeight (e, regexps) {
let weight = 0
/* Look for a special classname */
if (e.className !== '') {
if (e.className.search(regexps.negativeRe) !== -1) weight -= 25
if (e.className.search(regexps.positiveRe) !== -1) weight += 25
}
/* Look for a special ID */
if (typeof (e.id) === 'string' && e.id !== '') {
if (e.id.search(regexps.negativeRe) !== -1) weight -= 25
if (e.id.search(regexps.positiveRe) !== -1) weight += 25
}
return weight
}
/**
* Clean a node of all elements of type "tag".
* (Unless it's a youtube/vimeo video. People love movies.)
*
* @param {jQuery} element
* @param string tag to clean
* @return {Void}
**/
function clean (e, tag, regexps) {
const targetList = e.getElementsByTagName(tag)
const isEmbed = (tag === 'object' || tag === 'embed')
for (let y = targetList.length - 1; y >= 0; y--) {
// ------- user clean handler -----------------
let validRule = false
for (let i = 0; i < cleanRules.length; i++) {
if (cleanRules[i](targetList[y], tag) === true) {
validRule = true
break
}
}
if (validRule) {
continue
}
// ------- end user clean handler -----------------
/* Allow youtube and vimeo videos through as people usually want to see those. */
if (isEmbed) {
if (targetList[y].innerHTML.search(regexps.videoRe) !== -1) {
continue
}
}
targetList[y].parentNode.removeChild(targetList[y])
}
}
/**
* Clean an element of all tags of type "tag" if they look fishy.
* "Fishy" is an algorithm based on content length, classnames, link density, number of images & embeds, etc.
*
* @return {Void}
**/
function cleanConditionally (e, tag, regexps) {
const tagsList = e.getElementsByTagName(tag)
const curTagsLength = tagsList.length
/**
* Gather counts for other typical elements embedded within.
* Traverse backwards so we can remove nodes at the same time without effecting the traversal.
*
* TODO: Consider taking into account original contentScore here.
**/
for (let i = curTagsLength - 1; i >= 0; i--) {
const weight = getClassWeight(tagsList[i], regexps)
dbg('Cleaning Conditionally ' + tagsList[i] + ' (' + tagsList[i].className + ':' + tagsList[i].id + ')' + ((typeof tagsList[i].readability !== 'undefined') ? (' with score ' + tagsList[i].readability.contentScore) : ''))
if (weight < 0) {
tagsList[i].parentNode.removeChild(tagsList[i])
} else if (getCharCount(tagsList[i], ',', regexps) < 10) {
/**
* If there are not very many commas, and the number of
* non-paragraph elements is more than paragraphs or other ominous signs, remove the element.
**/
const p = tagsList[i].getElementsByTagName('p').length
const img = tagsList[i].getElementsByTagName('img').length
const li = tagsList[i].getElementsByTagName('li').length - 100
const input = tagsList[i].getElementsByTagName('input').length
let embedCount = 0
const embeds = tagsList[i].getElementsByTagName('embed')
for (let ei = 0, il = embeds.length; ei < il; ei++) {
if (embeds[ei].src && embeds[ei].src.search(regexps.videoRe) === -1) {
embedCount++
}
}
const linkDensity = getLinkDensity(tagsList[i], regexps)
const contentLength = getInnerText(tagsList[i], true, regexps).length
let toRemove = false
if (img > p && img > 1) {
toRemove = true
} else if (li > p && tag !== 'ul' && tag !== 'ol') {
toRemove = true
} else if (input > Math.floor(p / 3)) {
toRemove = true
} else if (contentLength < 25 && (img === 0 || img > 2)) {
toRemove = true
} else if (weight < 25 && linkDensity > 0.2) {
toRemove = true
} else if (weight >= 25 && linkDensity > 0.5) {
toRemove = true
} else if ((embedCount === 1 && contentLength < 75) || embedCount > 1) {
toRemove = true
}
if (toRemove) {
tagsList[i].parentNode.removeChild(tagsList[i])
}
}
}
}
/**
* Converts relative urls to absolute for images and links
*
* @param {jQuery} element
*
* @return {Void}
*
**/
function fixLinks (e) {
if (!e.ownerDocument.originalURL) {
return
}
function fixLink (link) {
const fixed = url.URL(e.ownerDocument.originalURL, link)
return fixed
}
let i
const imgs = e.getElementsByTagName('img')
for (i = imgs.length - 1; i >= 0; --i) {
const src = imgs[i].getAttribute('src')
if (src) {
imgs[i].setAttribute('src', fixLink(src))
}
}
const as = e.getElementsByTagName('a')
for (i = as.length - 1; i >= 0; --i) {
const href = as[i].getAttribute('href')
if (href) {
as[i].setAttribute('href', fixLink(href))
}
}
}
/**
* Clean out spurious headers from an Element. Checks things like classnames and link density.
*
* @param {jQuery} element
* @return {Void}
**/
function cleanHeaders (e, regexps) {
for (let headerIndex = 1; headerIndex < 7; headerIndex++) {
const headers = e.getElementsByTagName('h' + headerIndex)
for (let i = headers.length - 1; i >= 0; --i) {
if (getClassWeight(headers[i], regexps) < 0 || getLinkDensity(headers[i], regexps) > 0.33) {
headers[i].parentNode.removeChild(headers[i])
}
}
}
}
/**
* Remove the header that doesn't have next sibling.
*
* @param {jQuery} element
* @return {Void}
**/
function cleanSingleHeader (e) {
for (let headerIndex = 1; headerIndex < 7; headerIndex++) {
const headers = e.getElementsByTagName('h' + headerIndex)
for (let i = headers.length - 1; i >= 0; --i) {
if (headers[i].nextSibling === null) {
headers[i].parentNode.removeChild(headers[i])
}
}
}
}
/**
* Cleans the article content
*
* @param {jQuery} element
* @return {Void}
**/
function prepArticle (articleContent, regexps) {
cleanStyles(articleContent)
killBreaks(articleContent, regexps)
/* Clean out junk from the article content */
clean(articleContent, 'form', regexps)
clean(articleContent, 'object', regexps)
if (articleContent.getElementsByTagName('h1').length === 1) {
clean(articleContent, 'h1', regexps)
}
/**
* If there is only one h2, they are probably using it
* as a header and not a subheader, so remove it since we already have a header.
***/
if (articleContent.getElementsByTagName('h2').length === 1) clean(articleContent, 'h2', regexps)
clean(articleContent, 'iframe', regexps)
cleanHeaders(articleContent, regexps)
/* Do these last as the previous stuff may have removed junk that will affect these */
cleanConditionally(articleContent, 'table', regexps)
cleanConditionally(articleContent, 'ul', regexps)
cleanConditionally(articleContent, 'div', regexps)
/* Remove extra paragraphs */
const articleParagraphs = articleContent.getElementsByTagName('p')
for (let i = articleParagraphs.length - 1; i >= 0; i--) {
const imgCount = articleParagraphs[i].getElementsByTagName('img').length
const embedCount = articleParagraphs[i].getElementsByTagName('embed').length
const objectCount = articleParagraphs[i].getElementsByTagName('object').length
if (imgCount === 0 && embedCount === 0 && objectCount === 0 && getInnerText(articleParagraphs[i], true, regexps) === '') {
articleParagraphs[i].parentNode.removeChild(articleParagraphs[i])
}
}
cleanSingleHeader(articleContent)
try {
articleContent.innerHTML = articleContent.innerHTML.replace(/<br[^>]*>\s*<p/gi, '<p')
} catch (e) {
dbg('Cleaning innerHTML of breaks failed. This is an IE strict-block-elements bug. Ignoring.')
}
fixLinks(articleContent)
}
/**
* Initialize a node with the readability object. Also checks the
* className/id for special names to add to its score.
*
* @param {jQuery} element
* @return {Void}
**/
function initializeNode (node, regexps) {
node.readability = { contentScore: 0 }
switch (node.tagName) {
case 'ARTICLE':
node.readability.contentScore += 10
break
case 'SECTION':
node.readability.contentScore += 8
break
case 'DIV':
node.readability.contentScore += 5
break
case 'PRE':
case 'TD':
case 'BLOCKQUOTE':
node.readability.contentScore += 3
break
case 'ADDRESS':
case 'OL':
case 'UL':
case 'DL':
case 'DD':
case 'DT':
case 'LI':
case 'FORM':
node.readability.contentScore -= 3
break
case 'H1':
case 'H2':
case 'H3':
case 'H4':
case 'H5':
case 'H6':
case 'TH':
node.readability.contentScore -= 5
break
}
if (node.attributes.itemscope) {
node.readability.contentScore += 5
if (node.attributes.itemtype &&
regexps.attributeRe.test(node.getAttribute('itemtype'))) {
node.readability.contentScore += 30
}
}
node.readability.contentScore += getClassWeight(node, regexps)
}