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条回答
倾城 Initia
2楼-- · 2019-01-21 00:59

Piling on the solutions already presented, this is an interpolation-ary way in Rails 3 to render a pure Markdown file in a view from a partial without unnecessary indentation using Haml's :markdown filter and the RDiscount gem. The only catch is that your Markdown file is a Haml file, but that shouldn't matter for someone like a copy person.

In Gemfile:

gem 'rdiscount'

In app/views/my_page.html.haml

:markdown
  #{render 'my_partial', language: 'Markdown!'}

In app/views/_my_partial.html.haml

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

Look, I can **use** #{language}!

If you didn't need the :language variable passed in to the markdown file, you could do away altogether with your Markdown being a Haml file:

In app/views/my_page.html.haml

:markdown
  #{render 'my_partial.md'}

In app/views/_my_partial.md

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

Sorry, cannot **use** #{language} here!

Don't like those pesky underscores on your Markdown files?

In app/views/my_page.html.haml

:markdown
  #{render file: 'my_markdown.md'}

In app/views/my_markdown.md

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

Sorry, cannot **use** #{language} here!
查看更多
够拽才男人
3楼-- · 2019-01-21 01:02

You can use embedded markdown in Rails 5. Embedded markdown is based on the solution provided by Jacob above

  1. Add these two lines to your application's Gemfile:

    gem 'redcarpet' gem 'emd'

  2. bundle install.

  3. Then create a view app/view/home/changelog.html.md and paste your markdown in that .md file.

  4. Generate a home controller using the following command

    rails generate controller home

  5. At your route.rb, add this line:

    get '/changelog', :to 'home#changelog'

  6. That's all. Visit http://localhost:3000/changelog to see your rendered markdown

Source: http://github.com/ytbryan/emd

查看更多
等我变得足够好
4楼-- · 2019-01-21 01:05

Here is a version similar to @Jacob's but using Redcarpet.

module MarkdownHandler
  def self.erb
    @erb ||= ActionView::Template.registered_template_handler(:erb)
  end

  def self.call(template)
    options = {
      fenced_code_blocks:           true,
      smartypants:                  true,
      disable_indented_code_blocks: true,
      prettify:                     true,
      tables:                       true,
      with_toc_data:                true,
      no_intra_emphasis:            true
    }
    @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
    "#{@markdown.render(template.source).inspect}.html_safe"
  end
end
ActionView::Template.register_template_handler :md, MarkdownHandler

Full credit to lencioni who posted this in this gist.

And if you'd like to evaluate erb:

erb = ERB.new(template.source).result
@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
"#{@markdown.render(erb).inspect}.html_safe"
查看更多
Evening l夕情丶
5楼-- · 2019-01-21 01:12

I just released a markdown-rails gem, which handles .html.md views.

You cannot chain it with Erb though -- it's only for static views and partials. To embed Ruby code, you'd have to use tjwallace's solution with :markdown.

查看更多
别忘想泡老子
6楼-- · 2019-01-21 01:13

Turns out, the Right Way (tm) to do this is using ActionView::Template.register_template_handler:

lib/markdown_handler.rb:

require 'rdiscount'

module MarkdownHandler
  def self.erb
    @erb ||= ActionView::Template.registered_template_handler(:erb)
  end

  def self.call(template)
    compiled_source = erb.call(template)
    "RDiscount.new(begin;#{compiled_source};end).to_html"
  end
end

ActionView::Template.register_template_handler :md, MarkdownHandler

If you require 'markdown_handler' in your config/application.rb (or an initializer), then any view or partial can be rendered as Markdown with ERb interpolation using the extension .html.md:

app/views/home/index.html.md:

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

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

app/controllers/home_controller.rb:

class HomeController < ApplicationController
  def index
    @language = "Markdown"
  end
end
查看更多
乱世女痞
7楼-- · 2019-01-21 01:16

Not a pure markdown solution but you can use HAML filters to render markdown, as well as other markup languages.

For example, in app/views/_my_partial.html.haml:

:markdown
  My awesome view
  ===============

  Look, I can **use** #{language}!
查看更多
登录 后发表回答