如何在 Rails 3 中使用 markdown 自动渲染部分内容?

发布于 2024-10-02 19:30:52 字数 515 浏览 1 评论 0 原文

我想将我的一些部分作为降价片段。使用标准 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

嘿嘿嘿 2024-10-09 19:30:52

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

如果您需要 'markdown_handler' 在您的 config/application.rb (或初始值设定项)中,然后任何视图或部分都可以使用扩展名 .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

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
七度光 2024-10-09 19:30:52

不是纯粹的 Markdown 解决方案,但您可以使用 HAML 过滤器 进行渲染markdown 以及其他标记语言。

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

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

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

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}!
揽月 2024-10-09 19:30:52

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

views/layouts/_markdown.html.erb

<%= m yield %>

app/helpers/application_helper.rb

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

Gemfile

gem 'rdiscount'

所以,在 view 中你可以这样称呼它:

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

并且 contract.markdown 将被格式化为 markdown

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

三人与歌 2024-10-09 19:30:52

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

但你不能将它与 Erb 链接起来——它仅适用于静态视图和局部视图。要嵌入 Ruby 代码,您必须将 tjwallace 的解决方案与 :markdown 结合使用。

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.

度的依靠╰つ 2024-10-09 19:30:52

在已经提出的解决方案的基础上,这是 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 文件的情况:

In app/views/my_page.html.haml

:markdown
  #{render 'my_partial.md'}

In < strong>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!

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!
仙女山的月亮 2024-10-09 19:30:52

利用您的答案制作一个 gem,以在 Rails 中呈现 GitHub Flavored Markdown(通过 HTML::Pipeline):https://github.com/afeld/html_pipeline_rails

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

他不在意 2024-10-09 19:30:52

这是与 @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 davidjrice/3014948" rel="nofollow noreferrer">在这个要点。

并且如果您想评估 erb:

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

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"
饮湿 2024-10-09 19:30:52

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

  1. 将这些添加到您应用程序的Gemfile中:
    gem 'coderay' #optional for Syntax Highlighting
    gem 'redcarpet'
    gem 'emd'
  1. 捆绑安装

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

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

    rails 生成控制器主页

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

    获取 '/changelog', :to 'home#changelog'

  5. 仅此而已。访问 http://localhost:3000/changelog 查看渲染的 Markdown

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

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

  1. Add these to your application's Gemfile:
    gem 'coderay' #optional for Syntax Highlighting
    gem 'redcarpet'
    gem 'emd'
  1. bundle install.

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

  3. Generate a home controller using the following command

    rails generate controller home

  4. Add the following line to your route.rb:

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

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

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

尬尬 2024-10-09 19:30:52

Rails 7.1 更新需要额外的“.to_s”来满足 OutputBuffer:

"RDiscount.new(begin;#{compile_source };end).to_html"

更改为

"RDiscount.new(begin; #{compile_source}.to_s;end).to_html"

# frozen_string_literal: true

require 'rdiscount'

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

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

ActionView::Template.register_template_handler :md, MarkdownHandler

Rails 7.1 update requires an extra ".to_s" to satisfy OutputBuffer:

"RDiscount.new(begin;#{ compiled_source };end).to_html"

changes to

"RDiscount.new(begin;#{ compiled_source }.to_s;end).to_html"

# frozen_string_literal: true

require 'rdiscount'

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

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

ActionView::Template.register_template_handler :md, MarkdownHandler
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文