【问题标题】:How can I automatically render partials using markdown in Rails 3?如何在 Rails 3 中使用 markdown 自动渲染局部?
【发布时间】:2011-05-08 23:49:24
【问题描述】:

我想将我的一些部分作为 markdown sn-ps。使用标准 rails erb 模板渲染它们的最简单方法是什么?

理想情况下,我想做这样的事情:

如果我在 app/views/_my_partial.md.erb 中有部分内容:

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

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

我从这样的视图中引用:

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

我想得到如下所示的输出:

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

【问题讨论】:

标签: ruby-on-rails-3 markdown partials


【解决方案1】:

事实证明,执行此操作的正确方法 (tm) 是使用 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

如果您在config/application.rb(或初始化程序)中使用require 'markdown_handler',则可以使用扩展名.html.md 将任何视图或部分视图呈现为带有ERb 插值的Markdown:

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

【讨论】:

  • 不错。让我补充一下,不要忘记将 gem 'rdiscount' 添加到您的 gemfile 中,并且您可以将处理程序放在 init 目录中,它将始终被加载。 config/initializers/markdown_handler.rb这意味着您可以跳过对config/applicaiton.rb的更改
  • 有趣,我怀疑这可以通过在生成的源中添加 .html_safe 来缓解。 erb.call(template) 返回的代码已经被渲染的 ERb 转义了,我们这里没有做任何插值,所以应该没问题。
  • 如果有人有兴趣使用红地毯,唯一可以更改的线路是"Redcarpet::Markdown.new(Redcarpet::Render::HTML, no_intra_emphasis: true, autolink: true).render(begin;#{compiled_source};end).html_safe"
  • 这个答案实际上被低估了:即使是 3 颗宝石 kinda 做同样的事情 maildown、markdown-rails、markerb 也没有取得如此好的结果。我认为它应该默认在 Rails 中。
  • 提醒任何发现这个线程并试图让它与 Rails 6.0 一起工作的人:ActionView::Template.registered_template_handler 发生了变化,现在它需要两个参数,template 和 source (而不仅仅是模板)。我正在想办法解决它,如果我解决了,会回答问题under this question here
【解决方案2】:

不是纯 markdown 解决方案,但您可以使用 HAML filters 来呈现 markdown,以及其他标记语言。

例如在app/views/_my_partial.html.haml:

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

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

【讨论】:

  • 我不喜欢这种方法的地方是它强制缩进应该可以说是一个纯粹的降价文件。如果我希望复制人能够控制某些内容,我希望他们拥有整个文件,而不必记住将所有内容都缩进一级。
  • 但好在它不会两次加载相同的功能,对吧? gem 'rdiscount' 还需要吗?也许这个答案是一个很好的妥协? stackoverflow.com/a/8026947/1620081
【解决方案3】:

已经找到在这种情况下不使用haml的方法。

在视图/布局/_markdown.html.erb

中
<%= m yield %>

在 app/helpers/application_helper.rb

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

在Gemfile

中
gem 'rdiscount'

因此,鉴于您可以这样称呼它:

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

而contract.markdown会被格式化为markdown

【讨论】:

    【解决方案4】:

    我刚刚发布了一个markdown-rails gem,它处理.html.md 视图。

    你不能用 Erb 链接它——它只用于静态视图和局部视图。要嵌入 Ruby 代码,您必须使用 tjwallace 的解决方案和 :markdown。

    【讨论】:

      【解决方案5】:

      在已经提出的解决方案的基础上,这是 Rails 3 中的一种插值方法,可以使用 Haml 的 :markdown 过滤器和 RDiscount gem 从部分视图中渲染纯 Markdown 文件,而无需进行不必要的缩进。唯一的问题是你的 Markdown 文件是一个 Haml 文件,但这对于像复制人这样的人来说应该没关系。

      在Gemfile中:

      gem 'rdiscount'
      

      在 app/views/my_page.html.haml

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

      在 app/views/_my_partial.html.haml

      My awesome view
      ===============
      
      Look, I can **use** #{language}!
      

      如果您不需要将 :language 变量传递给 Markdown 文件,您可以完全取消 Markdown 成为 Haml 文件:

      在 app/views/my_page.html.haml

      :markdown
        #{render 'my_partial.md'}
      

      在app/views/_my_partial.md

      My awesome view
      ===============
      
      Sorry, cannot **use** #{language} here!
      

      不喜欢 Markdown 文件中那些讨厌的下划线?

      在 app/views/my_page.html.haml

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

      在 app/views/my_markdown.md

      My awesome view
      ===============
      
      Sorry, cannot **use** #{language} here!
      

      【讨论】:

      • 哈哈,讨厌。我喜欢这个。
      【解决方案6】:

      利用 your answer 制作 gem 以在 Rails 中为 GitHub Flavored Markdown 呈现(通过 HTML::Pipeline):https://github.com/afeld/html_pipeline_rails

      【讨论】:

      • 谢谢,这个 gem 对我来说是完美的解决方案(实际上是 Rails 4)。
      • 这个 gem 非常适合 Rails 4。允许我将所有 Markdown 内容放在一个文件夹中,并通过部分将其包含在需要的地方。不需要有整个视图 Markdown,只需内容部分。
      【解决方案7】:

      这是一个类似于@Jacob 的版本,但使用的是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
      

      完全归功于 lencioni 发布此 in this gist 的人。

      并且如果您想评估 erb:

      erb = ERB.new(template.source).result
      @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
      "#{@markdown.render(erb).inspect}.html_safe"
      

      【讨论】:

      • 本身,这是一个很好的补充,但请将您的自定义类 RedcarpetHeaderFix 更改为默认的 Redcarpet::Render::HTML。这让我头疼;)
      【解决方案8】:

      您可以在 Rails 5 中使用嵌入式 Markdown。嵌入式 Markdown 基于 Jacob above 提供的解决方案

      1. 将这些添加到应用程序的 Gemfile 中:
          gem 'coderay' #optional for Syntax Highlighting
          gem 'redcarpet'
          gem 'emd'
      
      1. bundle install.

      2. 然后创建一个视图 app/view/home/changelog.html.md 并将您的 markdown 粘贴到该 .md 文件中。

      3. 使用以下命令生成家庭控制器

        rails generate controller home

      4. 将以下行添加到您的 route.rb:

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

      5. 就是这样。访问 http://localhost:3000/changelog 以查看您呈现的降价

      来源:http://github.com/ytbryan/emd

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-26
        • 2018-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多