代码之家  ›  专栏  ›  技术社区  ›  Jacob

如何使用Rails 3中的markdown自动呈现部分呢?

  •  36
  • Jacob  · 技术社区  · 14 年前

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

    如果我有一部分 :

    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>
    
    8 回复  |  直到 14 年前
        1
  •  80
  •   Jacob    12 年前

    事实证明,正确的方法是使用 ActionView::Template.register_template_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
    

    require 'markdown_handler' 在你的 config/application.rb (或初始值设定项),则任何视图或部分视图都可以通过使用扩展名的ERb插值呈现为标记 .html.md

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

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

    应用程序/控制器/家庭控制器.rb :

    class HomeController < ApplicationController
      def index
        @language = "Markdown"
      end
    end
    
        2
  •  20
  •   rib_ears tjwallace    8 年前

    不是一个纯粹的降价方案,但是你可以使用 HAML filters 呈现标记,以及其他标记语言。

    app/views/_my_partial.html.haml :

    :markdown
      My awesome view
      ===============
    
      Look, I can **use** #{language}!
    
        3
  •  6
  •   Jo Liss    12 年前

    我刚放了一个 markdown-rails .html.md 意见。

    :markdown .

        4
  •  4
  •   Roman Golomidov    13 年前

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

    在里面 视图/布局/\u markdown.html.erb

    <%= m yield %>
    

    在里面

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

    在里面

    gem 'rdiscount'
    

    所以,你可以这样称呼它:

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

    以及 将被格式化为降价

        5
  •  4
  •   Paul Fioravanti    12 年前

    :markdown 滤镜和宝石。唯一的问题是,你的降价文件是一个Haml文件,但这对像复制人这样的人来说应该无关紧要。

    :

    gem 'rdiscount'
    

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

    app/views/_my_partial.html.haml

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

    :language 变量传递到标记文件中,您可以将标记完全取消为Haml文件:

    应用程序/视图/我的网页.html.haml

    :markdown
      #{render 'my_partial.md'}
    

    应用程序/视图/我的部分.md

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

    应用程序/视图/我的网页.html.haml

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

    应用程序/视图/my_markdown.md

    ===============
    
    对不起,这里不能**使用**{语言}!
    
        6
  •  2
  •   Community T.Woody    7 年前

    your answer 要在Rails中为GitHub风格的标记生成gem(通过HTML::Pipeline): https://github.com/afeld/html_pipeline_rails

        7
  •  1
  •   davegson    6 年前

    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.new(template.source).result
    @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
    "#{@markdown.render(erb).inspect}.html_safe"
    
        8
  •  0
  •   ytbryan    5 年前

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

        gem 'coderay' #optional for Syntax Highlighting
        gem 'redcarpet'
        gem 'emd'
    
    1. bundle install .

    2. 然后创建视图 app/view/home/changelog.html.md .md 文件。

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

      rails generate controller home

    4. 在route.rb中添加以下行:

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

    5. http://localhost:3000/changelog 查看渲染的标记

    http://github.com/ytbryan/emd