class Jekyll::Lunr::Indexer

Generates a Lunr index from documents

Attributes

site[R]

Public Class Methods

new(site) click to toggle source
   # File lib/jekyll/lunr.rb
56 def initialize(site)
57   @site = site
58 end

Public Instance Methods

data() click to toggle source

The data is the register where Lunr looks for results. TODO: Write a single data file per doc?

   # File lib/jekyll/lunr.rb
62 def data
63   @data ||= site.documents.select do |doc|
64     doc.respond_to? :to_data
65   end.reject do |doc|
66     doc.data['sitemap'] == false
67   end.map(&:to_data)
68 end
data_file() click to toggle source
   # File lib/jekyll/lunr.rb
70 def data_file
71   @data_file ||= Jekyll::StaticFile.new(site, site.source, '.', 'data.json')
72 end
dir() click to toggle source
   # File lib/jekyll/lunr.rb
95 def dir
96   File.realpath(File.join([__dir__, '..', '..']))
97 end
env() click to toggle source
    # File lib/jekyll/lunr.rb
107 def env
108   @env ||= { 'NODE_PATH' => File.join(site.source, 'node_modules') }
109 end
fields() click to toggle source

Indexable fields

    # File lib/jekyll/lunr.rb
134 def fields
135   @fields ||= Set.new((site.config.dig('jekyll-lunr', 'fields') || []) + %w[title description]).freeze
136 end
free() click to toggle source
    # File lib/jekyll/lunr.rb
138 def free
139   @data = nil
140   @indexable_data = nil
141 end
index() click to toggle source
    # File lib/jekyll/lunr.rb
111 def index
112   Open3.popen2(env, *indexer) do |stdin, stdout, wait|
113     indexable_data.each do |data|
114       stdin.puts data.to_json
115     end
116     stdin.close
117 
118     File.open(index_file.path, 'w') do |idx|
119       idx.write(stdout.read)
120     end
121 
122     site.static_files << index_file
123 
124     wait.value
125   end
126 end
index_file() click to toggle source
    # File lib/jekyll/lunr.rb
 99 def index_file
100   @index_file ||= Jekyll::StaticFile.new(site, site.source, '.', 'idx.json')
101 end
indexable_data() click to toggle source

Convert data to strings since Lunr can't index objects

   # File lib/jekyll/lunr.rb
75 def indexable_data
76   @indexable_data ||= data.map do |d|
77     d.transform_values do |v|
78       case v
79       when Array then v.map { |vv| vv.is_a?(Hash) ? vv['title'] : vv }.compact.join(', ')
80       when Hash then v['title'] || v.values.map(&:to_s).join(', ')
81       else v.to_s
82       end
83     end
84   end
85 end
indexer() click to toggle source
    # File lib/jekyll/lunr.rb
103 def indexer
104   @indexer ||= ['node', File.join(dir, 'lib', 'assets', 'javascript', 'indexer.js'), lang].freeze
105 end
lang() click to toggle source

Site lang

    # File lib/jekyll/lunr.rb
129 def lang
130   @lang ||= site.config.dig('lang').freeze
131 end
write() click to toggle source
   # File lib/jekyll/lunr.rb
87 def write
88   File.open(data_file.path, 'w') do |df|
89     df.write data.to_json
90   end
91 
92   site.static_files << data_file
93 end