Convert Jekyll posts with Redcarpet

284 views Asked by At

I wrote a Jekyll generator that parses markdown and encodes the resulting HTML into JSON. The problem is, my code blocks are not being parsed. I think it’s because I’ve written my markdown with the Redcarpet style, but my generator doesn’t use Redcarpet.

In my generator, I have something like this:

module Jekyll
  require 'json'

  class JSONGenerator < Generator
    safe true
    priority :low

    def generate(site)
      # Converter for .md > .html
      converter = site.getConverterImpl(Jekyll::Converters::Markdown)

      # Iterate over all posts
      site.posts.each do |post|

        # Encode the HTML to JSON
        hash = { "content" => converter.convert(post.content)}

      end

    end

  end

end

How can I change it so I’m parsing with Redcarpet instead? Redcarpet is set as my default markdown library in my _config.yml file. I tried using this:

markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)

... But this exception was thrown:

Warning: Command failed: error: uninitialized constant Jekyll::JSONGenerator::Redcarpet.
1

There are 1 answers

0
Jezen Thomas On BEST ANSWER

Looks like the way to use Redcarpet in Jekyll is by instantiating that class, and passing in site.config.

converter = Jekyll::Converters::Markdown::RedcarpetParser.new(site.config)