Skip to content

Instantly share code, notes, and snippets.

@egardner
Created March 11, 2015 21:13
Show Gist options
  • Save egardner/b6bd5d785048ec2d0b2b to your computer and use it in GitHub Desktop.
Save egardner/b6bd5d785048ec2d0b2b to your computer and use it in GitHub Desktop.
Jekyll Plugin: JSON Generator for Collection docs
module Jekyll
class JSONPage < Page
def initialize(site, base, dir, name, content)
@site = site
@base = base
@dir = dir
@name = name
self.data = {}
self.content = content
process(@name)
end
def read_yaml(*)
# Do nothing
end
def render_with_liquid?
false
end
end
class JSONPostGenerator < Generator
safe true
def generate(site)
require 'kramdown'
site.documents.each do |document|
# Set the path of the JSON version
path = "#{document.collection.label}" + document.cleaned_relative_path + ".json"
output = document.to_liquid
output['content'] = Kramdown::Document.new(document.content).to_html.gsub(/\n/, "")
# Delete unnecessary metadata
['layout', 'output'].each { |key| output.delete(key) }
site.pages << JSONPage.new(site, site.source, File.dirname(path), File.basename(path), output.to_json)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment