Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails 3.2 Markdown Template Handler
# config/initializers/redcarpet.rb
module ActionView
module Template::Handlers
class Markdown
class_attribute :default_format
self.default_format = Mime::HTML
def call(template)
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
markdown.render(template.source).html_safe.inspect
end
end
end
end
ActionView::Template.register_template_handler(:md, ActionView::Template::Handlers::Markdown.new)
ActionView::Template.register_template_handler(:mdown, ActionView::Template::Handlers::Markdown.new)
ActionView::Template.register_template_handler(:markdown, ActionView::Template::Handlers::Markdown.new)
@lencioni

This comment has been minimized.

Copy link

@lencioni lencioni commented Apr 18, 2014

Thanks for the code! We are in Rails 4.1 (not sure if that matters), and we ended up doing this:

module ActionView
  module Template::Handlers
    # Rails template handler for Markdown
    class Markdown
      class_attribute :default_format
      self.default_format = Mime::HTML

      # @param template [ActionView::Template]
      # @return [String] Ruby code that when evaluated will return the rendered
      #   content
      def call(template)
        @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML,
                                              autolink:             true,
                                              space_after_headers:  true,
                                              fenced_code_blocks:   true)
        "#{@markdown.render(template.source).inspect}.html_safe"
      end
    end
  end
end

ActionView::Template.register_template_handler(
  :md, ActionView::Template::Handlers::Markdown.new)
@antoinelyset

This comment has been minimized.

Copy link

@antoinelyset antoinelyset commented Sep 1, 2014

Thanks both of you, it helped me a lot.
(I did a Minimal Kramdown implementation here : https://gist.github.com/antoinelyset/16715598b6cfadb5f676)

@23tux

This comment has been minimized.

Copy link

@23tux 23tux commented Jan 17, 2015

Thanks, this is awesome! What would be the way to also include :erb? I tried it with

module ActionView
  module Template::Handlers
    # Rails template handler for Markdown
    class Markdown
      class_attribute :default_format
      self.default_format = Mime::HTML

      def erb
        @erb ||= ActionView::Template.registered_template_handler(:erb)
      end

      # @param template [ActionView::Template]
      # @return [String] Ruby code that when evaluated will return the rendered
      #   content
      def call(template)
        compiled_source = erb.call(template)
        @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, space_after_headers: true, fenced_code_blocks: true)
        "#{@markdown.render(compiled_source).inspect}.html_safe"
      end
    end
  end
end

but this gives

<p>@output<em>buffer = output</em>buffer || ActionView::OutputBuffer.new;@output<em>buffer.safe</em>append='# Hello world'.freeze;@output<em>buffer.append=( 3*987 );@output</em>buffer.to_s</p>

So, how could I embed ruby code into a markdown file?

@przbadu

This comment has been minimized.

Copy link

@przbadu przbadu commented Oct 14, 2016

@23tux: I think, you should change

"#{@markdown.render(compiled_source).inspect}.html_safe"

to

"#{@markdown.render(compiled_source)}.html_safe"

remove inspect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.