1
0

Readability-readerable.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* eslint-env es6:false */
  2. /*
  3. * Copyright (c) 2010 Arc90 Inc
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * This code is heavily based on Arc90's readability.js (1.7.1) script
  19. * available at: http://code.google.com/p/arc90labs-readability
  20. */
  21. var REGEXPS = {
  22. // NOTE: These two regular expressions are duplicated in
  23. // Readability.js. Please keep both copies in sync.
  24. unlikelyCandidates: /-ad-|ai2html|banner|breadcrumbs|combx|comment|community|cover-wrap|disqus|extra|footer|gdpr|header|legends|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|ad-break|agegate|pagination|pager|popup|yom-remote/i,
  25. okMaybeItsACandidate: /and|article|body|column|content|main|shadow/i,
  26. };
  27. function isNodeVisible(node) {
  28. // Have to null-check node.style and node.className.indexOf to deal with SVG and MathML nodes.
  29. return (!node.style || node.style.display != "none")
  30. && !node.hasAttribute("hidden")
  31. //check for "fallback-image" so that wikimedia math images are displayed
  32. && (!node.hasAttribute("aria-hidden") || node.getAttribute("aria-hidden") != "true" || (node.className && node.className.indexOf && node.className.indexOf("fallback-image") !== -1));
  33. }
  34. /**
  35. * Decides whether or not the document is reader-able without parsing the whole thing.
  36. *
  37. * @return boolean Whether or not we suspect Readability.parse() will suceeed at returning an article object.
  38. */
  39. function isProbablyReaderable(doc, isVisible) {
  40. if (!isVisible) {
  41. isVisible = isNodeVisible;
  42. }
  43. var nodes = doc.querySelectorAll("p, pre");
  44. // Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.
  45. // Some articles' DOM structures might look like
  46. // <div>
  47. // Sentences<br>
  48. // <br>
  49. // Sentences<br>
  50. // </div>
  51. var brNodes = doc.querySelectorAll("div > br");
  52. if (brNodes.length) {
  53. var set = new Set(nodes);
  54. [].forEach.call(brNodes, function(node) {
  55. set.add(node.parentNode);
  56. });
  57. nodes = Array.from(set);
  58. }
  59. var score = 0;
  60. // This is a little cheeky, we use the accumulator 'score' to decide what to return from
  61. // this callback:
  62. return [].some.call(nodes, function(node) {
  63. if (!isVisible(node))
  64. return false;
  65. var matchString = node.className + " " + node.id;
  66. if (REGEXPS.unlikelyCandidates.test(matchString) &&
  67. !REGEXPS.okMaybeItsACandidate.test(matchString)) {
  68. return false;
  69. }
  70. if (node.matches("li p")) {
  71. return false;
  72. }
  73. var textContentLength = node.textContent.trim().length;
  74. if (textContentLength < 140) {
  75. return false;
  76. }
  77. score += Math.sqrt(textContentLength - 140);
  78. if (score > 20) {
  79. return true;
  80. }
  81. return false;
  82. });
  83. }
  84. if (typeof module === "object") {
  85. module.exports = isProbablyReaderable;
  86. }