Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render table with Redcarpet and Markdown

I'm trying to render a table like this with Redcarpet

| header 1 | header 2 |
| -------- | -------- |
| cell 1   | cell 2   |
| cell 3   | cell 4   |

but it's not working.

Is it possible to render a table with Redcarpet ?

like image 849
jrabary Avatar asked Sep 06 '12 08:09

jrabary


1 Answers

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)

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>
like image 199
Gary G Avatar answered Sep 21 '22 18:09

Gary G