lib/read.js
function getArticle (candidates, $) {
  let topCandidate = null

  candidates.forEach(elem => {
    const linkDensity = getLinkDensity(elem, $)
    const siblings = elem.children('p').length
    const score = elem.data('readabilityScore')
    elem.data(
      'readabilityScore',
      Math.min(2, Math.max(siblings, 1)) * score * (1 - linkDensity)
    )
    if (
      !topCandidate ||
      elem.data('readabilityScore') > topCandidate.data('readabilityScore')
    ) {
      topCandidate = elem
    }
  })
  /**
   * If we still have no top candidate, just use the body as a last resort.
   * Should not happen.
   * */
  if (topCandidate === null) {
    return $('body')
  }

  // Perhaps the topCandidate is the parent?
  let parent
  if (
    !(parent = topCandidate.parent()) ||
    parent.length === 0 ||
    topCandidate.children('p').length > 5
  ) {
    return filterCandidates(topCandidate, topCandidate.children(), $)
  }
  return filterCandidates(topCandidate, parent.children(), $)
}
function getArticle (candidates, $) {
  let topCandidate = null

  candidates.forEach(elem => {
    const linkDensity = getLinkDensity(elem, $)
    const siblings = elem.children('p').length
    const score = elem.data('readabilityScore')
    elem.data(
      'readabilityScore',
      Math.min(2, Math.max(siblings, 1)) * score * (1 - linkDensity)
    )
    if (
      !topCandidate ||
      elem.data('readabilityScore') > topCandidate.data('readabilityScore')
    ) {
      topCandidate = elem
    }
  })
  /**
   * If we still have no top candidate, just use the body as a last resort.
   * Should not happen.
   * */
  if (topCandidate === null) {
    return $('body')
  }

  // Perhaps the topCandidate is the parent?
  let parent
  if (
    !(parent = topCandidate.parent()) ||
    parent.length === 0 ||
    topCandidate.children('p').length > 5
  ) {
    return filterCandidates(topCandidate, topCandidate.children(), $)
  }
  return filterCandidates(topCandidate, parent.children(), $)
}
function getArticle ( candidates , $ ) { let topCandidate = null candidates . forEach ( elem => { const linkDensity = getLinkDensity ( elem , $ ) const siblings = elem . children ( 'p' ) . length const score = elem . data ( 'readabilityScore' ) elem . data ( 'readabilityScore' , Math . min ( 2 , Math . max ( siblings , 1 ) ) * score * ( 1 - linkDensity ) ) if ( ! topCandidate || elem . data ( 'readabilityScore' ) > topCandidate . data ( 'readabilityScore' ) ) { topCandidate = elem } } ) /**
   * If we still have no top candidate, just use the body as a last resort.
   * Should not happen.
   * */ if ( topCandidate === null ) { return $ ( 'body' ) } // Perhaps the topCandidate is the parent? let parent if ( ! ( parent = topCandidate . parent ( ) ) || parent . length === 0 || topCandidate . children ( 'p' ) . length > 5 ) { return filterCandidates ( topCandidate , topCandidate . children ( ) , $ ) } return filterCandidates ( topCandidate , parent . children ( ) , $ ) }
Select the TopCandidate from all possible candidates
Select the TopCandidate from all possible candidates