4

I'm using Rails 3.2.11, Haml 4.0 and Redcarpet 2.2.2.

I would like to configure Haml's :markdown filter to use Redcarpet with with_toc_data: true. In ApplicationHelper I tried defining:

def markdown(text)
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(with_toc_data: true))
  raw markdown.render(text.to_s)
end

Though content in :markdown gets rendered, but without TOC data. How do I alter how :markdown is parsed?

silvenon
  • 2,068
  • 15
  • 29

1 Answers1

8

There’s currently no way to pass options through to the filter engines in Haml. The best solution for now is probably to replace the existing :markdown filter with a new one that has the options you want.

Try adding something like this to an initializer:

module Haml::Filters

  remove_filter("Markdown") #remove the existing Markdown filter

  module Markdown

    include Haml::Filters::Base

    def render(text)
      Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(with_toc_data: true)).render(text)
    end

  end
end
matt
  • 78,533
  • 8
  • 163
  • 197
  • This should've worked, `autolink: true` works, but `with_toc_data: true` doesn't. I'm gonna rais an issue on Redcarpet to see if the problem is actually there. – silvenon Apr 06 '13 at 21:48
  • Ok, `with_toc_data` is an option for creating renderers, I inserted it in the wrong place. This is in fact the correct answer. What now? Do I mark it as correct even though it technically doesn't work (my fault) or do you correct me that it should be `Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(with_toc_data: true)).render(text)`, **then** I approve it? – silvenon Apr 06 '13 at 22:13
  • @matija I’ve updated the answer. I was just looking into it myself, and then came back to update it and saw your second comment. – matt Apr 06 '13 at 22:19
  • @matija you should probably update your question too, so future visitors aren’t confused. – matt Apr 06 '13 at 22:21