如何使用Redcarpet和Markdown呈现表格

jra*_*ary 16 markdown redcarpet

我正试图用Redcarpet渲染这样的表格

| header 1 | header 2 |
| -------- | -------- |
| cell 1   | cell 2   |
| cell 3   | cell 4   |
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

是否可以使用Redcarpet呈现表格?

Gar*_*y G 30

Yes, you can render a table like that, but you have to enable the :tables option.

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)
Run Code Online (Sandbox Code Playgroud)

Outputs:

<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>
Run Code Online (Sandbox Code Playgroud)