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
6
7
<h3 class="text-xl leading-none font-semibold">
  <%= @announcement.title %>
</h3>

<div class="prose prose-blue overflow-hidden max-w-none">
  <%= render_markdown(@announcement.body, :full) %>
</div>

This is an example of how the renderer is called for the announcement body, with the :full mode.