Sha256: eb3538d01e409942fa1152476b211a236647e9045519c4d45dd5aaea6c93bdd6

Contents?: true

Size: 1.06 KB

Versions: 33

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Bridgetown
  class RelatedPosts
    class << self
      attr_accessor :lsi
    end

    attr_reader :post, :site

    def initialize(post)
      @post = post
      @site = post.site
      Bridgetown::External.require_with_graceful_fail("classifier-reborn") if site.lsi
    end

    def build
      return [] unless site.posts.docs.size > 1

      if site.lsi
        build_index
        lsi_related_posts
      else
        most_recent_posts
      end
    end

    def build_index
      self.class.lsi ||= begin
        lsi = ClassifierReborn::LSI.new(auto_rebuild: false)
        Bridgetown.logger.info("Populating LSI...")

        site.posts.docs.each do |x|
          lsi.add_item(x)
        end

        Bridgetown.logger.info("Rebuilding index...")
        lsi.build_index
        Bridgetown.logger.info("")
        lsi
      end
    end

    def lsi_related_posts
      self.class.lsi.find_related(post, 11)
    end

    def most_recent_posts
      @most_recent_posts ||= (site.posts.docs.last(11).reverse - [post]).first(10)
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
bridgetown-core-0.19.3 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.19.2 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.19.1 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.19.0 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.18.6 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.18.5 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.18.4 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.18.3 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.18.2 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.18.1 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.18.0 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.17.1 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.17.0 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.16.0 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.16.0.beta2 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.16.0.beta1 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.15.0 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.15.0.beta4 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.15.0.beta3 lib/bridgetown-core/related_posts.rb
bridgetown-core-0.15.0.beta2 lib/bridgetown-core/related_posts.rb