Class: WaxTasks::LunrIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/wax_tasks/lunr_index.rb

Overview

A LunrIndex document that combines data from all collections in site config that have ‘lunr_index` parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collections) ⇒ LunrIndex

Creates a new LunrIndex object



11
12
13
14
# File 'lib/wax_tasks/lunr_index.rb', line 11

def initialize(collections)
  @collections  = collections
  @fields       = total_fields
end

Instance Attribute Details

#collectionsArray

a list of LunrCollection objects

Returns:

  • (Array)

    the current value of collections



7
8
9
# File 'lib/wax_tasks/lunr_index.rb', line 7

def collections
  @collections
end

#fieldsArray

shared list of fields to index among LunrCollections

Returns:

  • (Array)

    the current value of fields



7
8
9
# File 'lib/wax_tasks/lunr_index.rb', line 7

def fields
  @fields
end

Instance Method Details

#default_uiString

Creates a default LunrUI / JS file for displaying the Index

Returns:



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
58
59
60
61
62
63
64
65
# File 'lib/wax_tasks/lunr_index.rb', line 32

def default_ui
  <<~HEREDOC
    ---
    layout: none
    ---
    $.getJSON({{ site.baseurl }}/js/lunr-index.json, function(index_json) {
      window.index = new elasticlunr.Index;
      window.store = index_json;
      index.saveDocument(false);
      index.setRef('lunr_id');
      #{@fields.map { |f| "index.addField('#{f}');" }.join("\n")}
      // add docs
      for (i in store){
        index.addDoc(store[i]);
      }
      $('input#search').on('keyup', function() {
        var results_div = $('#results');
        var query = $(this).val();
        var results = index.search(query, { boolean: 'AND', expand: true });
        results_div.empty();
        if (results.length > 10) {
          results_div.prepend("<p><small>Displaying 10 of " + results.length + " results.</small></p>");
        }
        for (var r in results.slice(0, 9)) {
          var ref = results[r].ref;
          var item = store[ref];
          #{@fields.map { |f| "var #{f} = item.#{f};" }.join("\n")}
          var result = '<div class="result"><b><a href="' + item.link + '">' + title + '</a></b></p></div>';
          results_div.append(result);
        }
      });
    });
  HEREDOC
end

#to_sString

Returns writes index data as pretty JSON with YAML front-matter.

Returns:

  • (String)

    writes index data as pretty JSON with YAML front-matter



23
24
25
26
27
# File 'lib/wax_tasks/lunr_index.rb', line 23

def to_s
  data = @collections.map(&:data).flatten
  data.each_with_index.map { |d, id| d['lunr_index'] = id }
  "---\nlayout: none\n---\n#{JSON.pretty_generate(data)}"
end

#total_fieldsArray

Returns shared list of fields to index among LunrCollections.

Returns:

  • (Array)

    shared list of fields to index among LunrCollections



17
18
19
20
# File 'lib/wax_tasks/lunr_index.rb', line 17

def total_fields
  total_fields = @collections.map(&:fields).reduce([], :concat)
  total_fields.uniq
end