Rendering Markdown with Multiple Modes

Curated ago by @jeremysmithco

Description

Sometimes when I’m working with Markdown in a Rails app, I’ll be using different sets of redcarpet flags for different types of content. For example, I have images and links turned on for blog posts, but turned off for comments. Keeping track of all the variations can be difficult. I now prefer to define a set of render “modes” that I can pass into a single renderer class, with clear comments for what is allowed and disallowed.

1
2
3
4
5
module MarkdownHelper
  def render_markdown(content, mode)
    sanitize(MarkdownRenderer.new(content, mode).render)
  end
end