3

If you use haml as rails view template, you can write portion of your page using markdown by using the ":markdown" filter.

Is is possible to do the same using erb?

Gaius Parx
  • 1,065
  • 1
  • 11
  • 18

2 Answers2

9

It's pretty easy to write a method that does it, assuming you're using something like Rails which has #capture, #concat, and #markdown helpers. Here's an example, using Maruku:

def markdown_filter(&block)
  concat(markdown(capture(&block)))
end

Then you can use this as so:

<% markdown_filter do %>
# Title

This is a *paragraph*.

This is **another paragraph**.
<% end %>

There are a few things to note here. First, it's important that all the text in the block have no indentation; you could get around this by figuring out the common indentation of the lines and removing it, but I didn't do that in the example helper above. Second, it uses Rails' #markdown helper, which could easily be implemented in other frameworks as so (replacing Maruku with your Markdown processor of choice):

def markdown(text)
  Maruku.new(text).to_html
end

Rails 3 has removed the #markdown helper, so just add the above code in the appropriate helper, substituting the Markdown processor of your choice.

Han
  • 5,374
  • 5
  • 31
  • 31
Natalie Weizenbaum
  • 5,884
  • 28
  • 22
1

ERB does not have filtering like this built-in. You'll need to directly use a gem that handles it, like RDiscount or the venerable BlueCloth.

x1a4
  • 19,417
  • 5
  • 40
  • 40
  • 1
    To be fair, Haml doesn't have Markdown support built-in either: it uses whatever Markdown gems are available on the system. Also, I would recommend the pure-Ruby Maruku library over the C-based RDiscount and BlueCloth, unless speed is a serious issue (which it shouldn't be with good caching). – Natalie Weizenbaum May 28 '10 at 19:03