Module: Jekyll::Tagging::RelatedPosts

Included in:
Document
Defined in:
lib/jekyll/tagging/related_posts.rb,
lib/jekyll/tagging/related_posts/version.rb

Constant Summary collapse

VERSION =
'1.3.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Used to remove #related_posts so that it can be overridden



7
8
9
10
11
# File 'lib/jekyll/tagging/related_posts.rb', line 7

def self.included(klass)
  klass.class_eval do
    remove_method :related_posts
  end
end

Instance Method Details

Calculate related posts. Returns [<Post>]



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll/tagging/related_posts.rb', line 15

def related_posts
  return [] unless docs.count > 1

  highest_freq = tag_freq.values.max
  related_scores = Hash.new(0)

  docs.each do |doc|
    doc.data["tags"].each do |tag|
      if self.data["tags"].include?(tag) && doc != self
        cat_freq = tag_freq[tag]
        related_scores[doc] += (1 + highest_freq - cat_freq)
      end
    end
  end

  sort_related_posts(related_scores)
end