Skip to content

Instantly share code, notes, and snippets.

@peterwake
Last active March 29, 2017 13:08
Show Gist options
  • Save peterwake/46b2a2397abff714aea4431491ef9a3e to your computer and use it in GitHub Desktop.
Save peterwake/46b2a2397abff714aea4431491ef9a3e to your computer and use it in GitHub Desktop.
Adding a :redcarpet Haml Filter to Rails 4
# config/initializers/haml_filters.rb
# Thanks to https://gist.github.com/sleepingkingstudios/955676
# config/initializers/haml_filters.rb
# in your haml files do e.g.
# %td.description
# :markdown
# #{task.description}
# I put gem 'redcarpet' in my Gemfile
require 'redcarpet' # you might not even need this if it's in your Gemfile
module Haml::Filters
remove_filter("Markdown") #remove the existing Markdown filter
module Markdown # the contents of this are as before, but without the lazy_require call
include Haml::Filters::Base
def render(text)
markdown.render(text)
end
private
def markdown
@markdown ||= Redcarpet::Markdown.new(renderer, extensions)
end
def renderer
@renderer ||= Redcarpet::Render::HTML.new(render_options)
end
def render_options
{
filter_html: true,
hard_wrap: true,
no_styles: true,
prettify: false,
safe_links_only: true,
with_toc_data: false
}
end
def extensions
{
autolink: true,
fenced_code_blocks: false,
footnotes: false,
highlight: true,
no_images: true,
no_intra_emphasis: true,
quote: true,
space_after_headers: false,
strikethrough: true,
superscript: true,
tables: true,
underline: true
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment