Module: Jekyll::Lunr::IndexableDocument

Defined in:
lib/jekyll/lunr.rb

Overview

Index a Jekyll::Document

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll/lunr.rb', line 26

def self.included(base)
  base.class_eval do
    def to_data
      data
        .slice(*site.indexer.fields)
        .transform_values { |value| extract_data_recursively(value) }
        .merge('content' => Loofah.fragment(content).to_text.strip,
               'id' => url,
               'year' => date&.year)
    end

    def extract_data_recursively(value)
      case value
      when Array then value.map { |v| extract_data(v) }
      when Hash then value.transform_values { |v| extract_data(v) }
      when Jekyll::Document then extract_data(value)
      when Jekyll::Page then extract_data(value)
      when String
        if value.include? '<'
          Loofah.fragment(value).to_text.strip
        else
          value.strip
        end
      else value
      end
    end

    def extract_data(value)
      value.respond_to?(:to_data) ? value.to_data : value
    end
  end
end