Sha256: 2675ad23d7ecc0e0ae5d869e6877bdd1cb767df7eb0f4af64fe21577693e4471

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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::Utils::RequireGems.require_with_graceful_fail("classifier-reborn") if site.lsi
    end

    def build
      return [] unless site.collections.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.collections.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) - [post]
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bridgetown-core-0.21.0.beta2 lib/bridgetown-core/related_posts.rb