Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Haml's :markdown filter to use Redcarpet with options?

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?

like image 705
silvenon Avatar asked Feb 16 '23 21:02

silvenon


1 Answers

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
like image 144
matt Avatar answered Apr 07 '23 00:04

matt