首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Redcarpet和Markdown渲染表格

如何使用Redcarpet和Markdown渲染表格
EN

Stack Overflow用户
提问于 2012-09-06 16:58:27
回答 2查看 8.3K关注 0票数 16

我正在尝试使用Redcarpet渲染一个这样的表

代码语言:javascript
复制
| header 1 | header 2 |
| -------- | -------- |
| cell 1   | cell 2   |
| cell 3   | cell 4   |

但它不起作用。

有没有可能用Redcarpet渲染一个表格?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-13 22:25:53

是的,您可以呈现这样的表,但您必须启用:tables选项。

代码语言:javascript
复制
require 'redcarpet'
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :tables => true)

text = <<END
| header 1 | header 2 |
| -------- | -------- |
| cell 1   | cell 2   |
| cell 3   | cell 4   |
END

puts markdown.render(text)

输出:

代码语言:javascript
复制
<table><thead>
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
</thead><tbody>
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</tbody></table>
票数 31
EN

Stack Overflow用户

发布于 2015-07-05 00:08:08

表格格式的公认答案是很好的。尝试将此内容添加为注释会丢失格式。然而,将其添加为答案也有些可疑。

不管怎样..。这是为了回答有关将markdown table选项与haml一起使用的问题(在Rails上下文中)。

application_helper.rb

代码语言:javascript
复制
  def markdown(content)
    return '' unless content.present?
    @options ||= {
        autolink: true,
        space_after_headers: true,
        fenced_code_blocks: true,
        underline: true,
        highlight: true,
        footnotes: true,
        tables: true,
        link_attributes: {rel: 'nofollow', target: "_blank"}
    }
    @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, @options)
    @markdown.render(content).html_safe
  end

然后在视图(views/product_line/show.html.haml)中:

代码语言:javascript
复制
= markdown(product_line.description)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12296453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档