Readability-readerable.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * @param {Object} options Configuration object.
  37. * @param {number} [options.minContentLength=140] The minimum node content length used to decide if the document is readerable.
  38. * @param {number} [options.minScore=20] The minumum cumulated 'score' used to determine if the document is readerable.
  39. * @param {Function} [options.visibilityChecker=isNodeVisible] The function used to determine if a node is visible.
  40. * @return {boolean} Whether or not we suspect Readability.parse() will suceeed at returning an article object.
  41. */
  42. function isProbablyReaderable(doc, options = {}) {
  43. // For backward compatibility reasons 'options' can either be a configuration object or the function used
  44. // to determine if a node is visible.
  45. if (typeof options == "function") {
  46. options = { visibilityChecker: options };
  47. }
  48. var defaultOptions = { minScore: 20, minContentLength: 140, visibilityChecker: isNodeVisible };
  49. options = Object.assign(defaultOptions, options);
  50. var nodes = doc.querySelectorAll("p, pre, article");
  51. // Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.
  52. // Some articles' DOM structures might look like
  53. // <div>
  54. // Sentences<br>
  55. // <br>
  56. // Sentences<br>
  57. // </div>
  58. var brNodes = doc.querySelectorAll("div > br");
  59. if (brNodes.length) {
  60. var set = new Set(nodes);
  61. [].forEach.call(brNodes, function (node) {
  62. set.add(node.parentNode);
  63. });
  64. nodes = Array.from(set);
  65. }
  66. var score = 0;
  67. // This is a little cheeky, we use the accumulator 'score' to decide what to return from
  68. // this callback:
  69. return [].some.call(nodes, function (node) {
  70. if (!options.visibilityChecker(node)) {
  71. return false;
  72. }
  73. var matchString = node.className + " " + node.id;
  74. if (REGEXPS.unlikelyCandidates.test(matchString) &&
  75. !REGEXPS.okMaybeItsACandidate.test(matchString)) {
  76. return false;
  77. }
  78. if (node.matches("li p")) {
  79. return false;
  80. }
  81. var textContentLength = node.textContent.trim().length;
  82. if (textContentLength < options.minContentLength) {
  83. return false;
  84. }
  85. score += Math.sqrt(textContentLength - options.minContentLength);
  86. if (score > options.minScore) {
  87. return true;
  88. }
  89. return false;
  90. });
  91. }
  92. if (typeof module === "object") {
  93. module.exports = isProbablyReaderable;
  94. }