سؤال

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?

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top