Update Blog to use Redcarpet/Markdown

Well, I decided to update the blog to use Redcarpet/Markdown. I started by watching Ryan Bates' Rails Cast episode on the topic. It turned out to be a little out of date. So here is how I implemented it. First I created an application helper markdown method.

module ApplicationHelper
  def markdown(text)
    rnder = HtmlWithPrettyPrint.new(:hard_wrap => true)
    mkdwn = Redcarpet::Markdown.new(rnder, :no_intra_emphasis => true, :fenced_code_blocks => true, :space_after_headers => true)
    mkdwn.render(text).html_safe
  end
end

I eventually want to use Google Prettify so my <pre> tag needs to look more like this: <pre class="prettyprint linenums"> In order to accomplish this I simply extended the renderer like this:

class HtmlWithPrettyPrint < Redcarpet::Render::HTML
  def postprocess(full_document)
    full_document.gsub("<pre>", "<pre class=\"prettyprint linenums\">")
  end
end

Then I was able to use something like this <%= markdown(post.content) %> in my view.

How easy is that?

02/20/2012
Edit | Back