Class: WaxTasks::LunrCollection
- Inherits:
-
Collection
- Object
- Collection
- WaxTasks::LunrCollection
- 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
-
#content ⇒ Boolean
whether/not page content should be indexed.
-
#data ⇒ Array
hash array of data from the ingested pages.
-
#fields ⇒ Array
the fields (i.e., keys) that should be indexed.
-
#index_config ⇒ Hash
the collection’s lunr_index config.
Attributes inherited from Collection
#config, #name, #page_dir, #site
Instance Method Summary collapse
-
#ingest_pages ⇒ Array
Finds the @page_dir of markdown pages for the collection and ingests them as an array of hashes.
-
#initialize(name, site) ⇒ LunrCollection
constructor
Creates a new LunrCollection with name @name given site config @site.
-
#load_page(page) ⇒ Object
Reads in a markdown file and converts it to a hash with the values from @fields.
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
#content ⇒ Boolean
whether/not page content should be indexed
9 10 11 |
# File 'lib/wax_tasks/lunr_collection.rb', line 9 def content @content end |
#data ⇒ Array
hash array of data from the ingested pages
9 10 11 |
# File 'lib/wax_tasks/lunr_collection.rb', line 9 def data @data end |
#fields ⇒ Array
the fields (i.e., keys) that should be indexed
9 10 11 |
# File 'lib/wax_tasks/lunr_collection.rb', line 9 def fields @fields end |
#index_config ⇒ Hash
the collection’s lunr_index config
9 10 11 |
# File 'lib/wax_tasks/lunr_collection.rb', line 9 def index_config @index_config end |
Instance Method Details
#ingest_pages ⇒ Array
Finds the @page_dir of markdown pages for the collection and ingests them as an array of 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
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 |