Skip to content

Instantly share code, notes, and snippets.

@albertogg
Last active September 24, 2021 12:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertogg/6489447 to your computer and use it in GitHub Desktop.
Save albertogg/6489447 to your computer and use it in GitHub Desktop.
Rails markdown parser using Redcarpet. It also parses erb within the markdown template.
# gem install redcarpet
# config/initializers/redcarpet.rb
# {viewname}.html.md
module ActionView
class Template
module Handlers
class Markdown
class_attribute :default_format
self.default_format = Mime::HTML
def call(template)
erb = ActionView::Template.registered_template_handler(:erb)
source = erb.call(template)
<<-SOURCE
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
:autolink => true,
:space_after_headers => true,
:fenced_code_blocks => true)
markdown.render(begin;#{source};end).html_safe
SOURCE
end
end
end
register_template_handler(:md, ActionView::Template::Handlers::Markdown.new)
register_template_handler(:mdown, ActionView::Template::Handlers::Markdown.new)
register_template_handler(:markdown, ActionView::Template::Handlers::Markdown.new)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment