Class: Jekyll::Documents::JsonIndexGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll/documents/json_index_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jekyll/documents/json_index_generator.rb', line 12

def generate(site)
  cfg = Configuration.read(site)
  return unless cfg["json_index"]

  docs = site.collections["documents"]&.docs || []
  return if docs.empty?

  items = docs.map do |doc|
    data = doc.data
    {
      "url" => doc.url,
      "title" => data["title"],
      "category" => data["category"],
      "date" => (data["date"] || Time.at(0)).strftime("%Y-%m-%d"),
      "slug" => data["slug"],
      "file_type" => data["file_type"],
      "extension" => data["extension"]
    }
  end

  json = JSON.pretty_generate(items)
  path = cfg["json_index_path"] || "/documents.json"

  site.static_files << TextStaticFile.new(site, site.source, path, json)
end