Question

I'm using Middleman (and Ruby, and Rails) for the first time, and I've hit a bit of a roadblock when it comes to rendering individual pages using Redcarpet as my markdown processor, and ERb for layout.

I want to use Markdown to style individual blocks of content, but each page will have more than one piece of content, uniquely styled.

Rather than using partials, is there a way to instantiate the Redcarpet renderer on multiple areas of the page? So in index.html.erb, there would be something like this:

<div class="grid5 container">
    <% markdown do %>
    # Some markdown
    <% end %>
</div>

<section class="grid6">
    <% markdown do %>
    ## More markdown
    <% end %>
</section>

I've tried to build a helper based on several tutorials, but I'm just not that good at Rails yet.

Edit My config.rb helper looks like:

module MD
    def markdown(text)
        Redcarpet.new(text).to_html
    end
end

helpers MD

per ASCIIcasts, linked above, and my ERb template uses similar code to the above:

<span class="g6 pre3">
<% markdown do %>
...etc...
<% end %>

but I'm getting an error when I load the page: ArgumentError at /about wrong number of arguments (0 for 1)

Was it helpful?

Solution

You defined your markdown method to receive one parameter called text. But what you provide in your views is a block.

To make things work, you either change the way you call the markdown helper method in the view

<%= markdown 'this is some markdown text in here' %>

or you change the markdown helper to accept a block

def markdown
  Redcarpet.new(yield).to_html
end

P.S.: The Redcarpet syntax has changed a bit since the Railscast, so if you are using a more recent gem version, implementing it this way won't work.

Starting at least from 3.3.4, perhaps earlier, you have to create a specific renderer and then call render on it with the markdown as an argument, i.e.

def markdown
   Redcarpet::Render::XHTML.new.render(yield)
end

Up to date documentation can be found here: https://github.com/vmg/redcarpet

OTHER TIPS

I had the same issue. Above didn't work. This is a much simpler solution with no custom code.

https://github.com/middleman/middleman/issues/1221#issuecomment-38104894

This is what I did:

helpers do
  def markdown(&block)
    raise ArgumentError, "Missing block" unless block_given?
    content = capture_html(&block)
    concat Tilt['markdown'].new { content }.render
  end
end

It uses the default Middleman markdown render.

Helper to convert a markdown String to html in middleman

helpers do
    def markdown(content)
        Tilt['markdown'].new(context: @app) { content }.render
    end
end

Set context: @app argument to handle images, links with middleman helpers and avoid exceptions.

I tried several methods I found here, with no success. Finally this is the solution that worked for me:

def markdown(text)
  require 'redcarpet'
  require 'redcarpet/render_strip'
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
  Markdown.new(text).to_html
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top