Class: WaxTasks::LunrCollection

Inherits:
Collection show all
Defined in:
lib/wax_tasks/lunr_collection.rb

Overview

A Jekyll collection to be Indexed in a Lunr Index / JSON file for client-side search.

Instance Attribute Summary collapse

Attributes inherited from Collection

#config, #name, #page_dir, #site

Instance Method Summary collapse

Methods inherited from Collection

#collection_config, #ingest_file, inherited

Constructor Details

#initialize(name, site) ⇒ LunrCollection

Creates a new LunrCollection with name @name given site config @site



13
14
15
16
17
18
19
20
21
22
# File 'lib/wax_tasks/lunr_collection.rb', line 13

def initialize(name, site)
  super(name, site)

  @index_config = @config['lunr_index']
  @content      = @index_config.fetch('content', false)
  @fields       = @index_config.fetch('fields', [])
  @data         = ingest_pages

  raise Error::MissingFields, "There are no fields for #{@name}.".magenta if @fields.empty?
end

Instance Attribute Details

#contentBoolean

whether/not page content should be indexed

Returns:

  • (Boolean)

    the current value of content



9
10
11
# File 'lib/wax_tasks/lunr_collection.rb', line 9

def content
  @content
end

#dataArray

hash array of data from the ingested pages

Returns:

  • (Array)

    the current value of data



9
10
11
# File 'lib/wax_tasks/lunr_collection.rb', line 9

def data
  @data
end

#fieldsArray

the fields (i.e., keys) that should be indexed

Returns:

  • (Array)

    the current value of fields



9
10
11
# File 'lib/wax_tasks/lunr_collection.rb', line 9

def fields
  @fields
end

#index_configHash

the collection’s lunr_index config

Returns:

  • (Hash)

    the current value of index_config



9
10
11
# File 'lib/wax_tasks/lunr_collection.rb', line 9

def index_config
  @index_config
end

Instance Method Details

#ingest_pagesArray

Finds the @page_dir of markdown pages for the collection and ingests them as an array of hashes

Returns:

  • (Array)

    array of the loaded markdown pages loaded as hashes



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wax_tasks/lunr_collection.rb', line 28

def ingest_pages
  data  = []
  pages = Dir.glob("#{@page_dir}/*.md")
  puts "There are no pages in #{@page_dir} to index.".cyan if pages.empty?
  pages.each do |p|
    begin
      data << load_page(p)
    rescue StandardError => e
      raise Error::LunrPageLoad, "Cannot load page #{p}\n#{e}"
    end
  end
  data
end

#load_page(page) ⇒ Object

Reads in a markdown file and converts it to a hash with the values from @fields. Adds the content of the file (below the YAML) if @content == true

Parameters:

  • page (String)

    the path to a markdown page to load



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wax_tasks/lunr_collection.rb', line 47

def load_page(page)
  yaml = YAML.load_file(page)
  hash = {
    'link' => "{{'#{yaml.fetch('permalink')}' | relative_url }}",
    'collection' => @name
  }
  if @content
    content = WaxTasks::Utils.html_strip(File.read(page))
    hash['content'] = WaxTasks::Utils.remove_diacritics(content)
  end
  fields = @fields.push('pid').uniq
  fields.each { |f| hash[f] = yaml[f].lunr_normalize }
  hash
end