How can I automatically render partials using mark

2019-01-21 00:46发布

I want to have some of my partials as markdown snippets. What is the easiest way to render them using the standard rails erb templating?

Ideally, I'd like to do something like this:

If I have a partial in app/views/_my_partial.md.erb:

My awesome view
===============

Look, I can **use** <%= language %>!

which I reference from a view like so:

<%= render "my_partial", :language => "Markdown!" %>

I want to get output that looks like this:

<h1>My awesome view</h1>
<p>Look, I can <strong>use</strong> Markdown!</p>

8条回答
smile是对你的礼貌
2楼-- · 2019-01-21 01:21

Leveraged your answer to make a gem to render for GitHub Flavored Markdown in Rails (via HTML::Pipeline): https://github.com/afeld/html_pipeline_rails

查看更多
何必那么认真
3楼-- · 2019-01-21 01:22

Have found way not to use haml in such situation.

in views/layouts/_markdown.html.erb

<%= m yield %>

in app/helpers/application_helper.rb

def m(string)
   RDiscount.new(string).to_html.html_safe
end  

in Gemfile

gem 'rdiscount'

So, in view you can call it like:

<%= render :partial => "contract.markdown", :layout => 'layouts/markdown.html.erb' %>

And contract.markdown will be formatted as markdown

查看更多
登录 后发表回答