如何在 Rails 3 中使用 markdown 自动渲染部分内容?
我想将我的一些部分作为降价片段。使用标准 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
事实证明,执行此操作的正确方法 (tm) 是使用 ActionView::Template.register_template_handler:
lib/markdown_handler.rb:
如果您
需要 'markdown_handler'
在您的config/application.rb
(或初始值设定项)中,然后任何视图或部分都可以使用扩展名.html.md
:app/views/home/index.html.md:
app/controllers/home_controller.rb:
Turns out, the Right Way (tm) to do this is using
ActionView::Template.register_template_handler
:lib/markdown_handler.rb:
If you
require 'markdown_handler'
in yourconfig/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:
app/controllers/home_controller.rb:
不是纯粹的 Markdown 解决方案,但您可以使用 HAML 过滤器 进行渲染markdown 以及其他标记语言。
例如,在
app/views/_my_partial.html.haml
中: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
:已找到在这种情况下不使用 haml 的方法。
在 views/layouts/_markdown.html.erb 中
app/helpers/application_helper.rb
中 Gemfile
所以,在 view 中你可以这样称呼它:
并且 contract.markdown 将被格式化为 markdown
Have found way not to use haml in such situation.
in views/layouts/_markdown.html.erb
in app/helpers/application_helper.rb
in Gemfile
So, in view you can call it like:
And contract.markdown will be formatted as markdown
我刚刚发布了一个 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
.在已经提出的解决方案的基础上,这是 Rails 3 中的一种插值方式,可以使用 Haml 的
:markdown
过滤器和 RDiscount gem 在部分视图中渲染纯 Markdown 文件,而无需不必要的缩进。唯一的问题是你的 Markdown 文件是 Haml 文件,但这对于像复制者这样的人来说应该不重要。在 Gemfile 中:
在 app/views/my_page.html.haml
中 在 app/views/_my_partial.html.haml 中
如果您不需要将
:language
变量传递到 Markdown 文件中,您可以完全取消 Markdown 作为 Haml 文件的情况:In app/views/my_page.html.haml
In < strong>app/views/_my_partial.md
不喜欢 Markdown 文件上那些讨厌的下划线?
在app/views/my_page.html.haml中
在app/views/my_markdown.md中
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:
In app/views/my_page.html.haml
In app/views/_my_partial.html.haml
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
In app/views/_my_partial.md
Don't like those pesky underscores on your Markdown files?
In app/views/my_page.html.haml
In app/views/my_markdown.md
利用您的答案制作一个 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
这是与 @Jacob 类似的版本,但使用 Redcarpet。
完全归功于发布此 lencioni davidjrice/3014948" rel="nofollow noreferrer">在这个要点。
并且如果您想评估 erb:
Here is a version similar to @Jacob's but using Redcarpet.
Full credit to lencioni who posted this in this gist.
And if you'd like to evaluate erb:
您可以在Rails 5中使用嵌入式Markdown。嵌入式Markdown是基于Jacob上面提供的解决方案,
捆绑安装
。然后创建一个视图
app/view/home/changelog.html.md
并将您的 Markdown 粘贴到该.md
文件中。使用以下命令生成家庭控制器
rails 生成控制器主页
将以下行添加到您的route.rb:
获取 '/changelog', :to 'home#changelog'
仅此而已。访问 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
bundle install
.Then create a view
app/view/home/changelog.html.md
and paste your markdown in that.md
file.Generate a home controller using the following command
rails generate controller home
Add the following line to your route.rb:
get '/changelog', :to 'home#changelog'
That's all. Visit http://localhost:3000/changelog to see your rendered markdown
Source: http://github.com/ytbryan/emd
Rails 7.1 更新需要额外的“.to_s”来满足 OutputBuffer:
"RDiscount.new(begin;#{compile_source };end).to_html"
更改为
"RDiscount.new(begin; #{compile_source}.to_s;end).to_html"
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"