Rendering Markdown Blocks

I have a project which can render markdown files, through redcarpet.

Middleman is version 3.3.3

The page of concern uses erb.

The project pulls some Markdown from a json file like so:

<div>
<% data.songs.songs.each do |f| %>
<% if f.title == "All The Heavens" %>
<%= f.notes.split("\n").join("</br>") %>
<% end %>
<% end %>
</div>

Unfortunately, that just displays the markdown in plain text.

The solution appears to be a Markdown view helper, something like:

module FormatHelpers
  def markdown(source)
    Tilt::RedcarpetTemplate.new { source }.render
  end
end

And make sure the page is named something like reader.html.md.erb
Then do something like:

<div>
<% data.songs.songs.each do |f| %>
<% if f.title == "All The Heavens" %>
<% markdown do %>
<%= f.notes.split("\n").join("</br>") %>
<% end %>
<% end %>
<% end %>
</div>

Thus far… Nothing has worked for me.

Hoping somebody has some ideas on this?

What you have here looks like it should work, but for what it’s worth I’m using the configured markdown renderer:

Tilt['markdown'].new { source }.render

What happens for me, if I have a link, is:

undefined method `link_to' for #<Object:0x00000006295508>

I’m having the same problem. Also, the Tilt['markdown'].new { source }.render helper doesn’t use the markdown engine settings in your config.rb (it’s mentioned in an old post: How to prevent Kramdown from adding IDs to headings) and I don’t know how to pass options to it.

For the link problem, I found this weird behavior: if you set redcarpet as markdown engine but don’t require it and don’t install the gem, you get a warning message saying: Requested Markdown engine (redcarpet) not found. Maybe the gem needs to be installed and required? but the links will render.

The solution for link_to error is to change helper code to:

Tilt[‘markdown’].new(context: @app) { text }.render

I had the same issue, but this solved it.

Source