我正在尝试使用Redcarpet渲染一个这样的表
| header 1 | header 2 |
| -------- | -------- |
| cell 1 | cell 2 |
| cell 3 | cell 4 |但它不起作用。
有没有可能用Redcarpet渲染一个表格?
发布于 2012-11-13 22:25:53
是的,您可以呈现这样的表,但您必须启用:tables选项。
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)输出:
<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>发布于 2015-07-05 00:08:08
表格格式的公认答案是很好的。尝试将此内容添加为注释会丢失格式。然而,将其添加为答案也有些可疑。
不管怎样..。这是为了回答有关将markdown table选项与haml一起使用的问题(在Rails上下文中)。
application_helper.rb
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)中:
= markdown(product_line.description)https://stackoverflow.com/questions/12296453
复制相似问题