github / cmark-gfm

GitHub's fork of cmark, a CommonMark parsing and rendering library and program in C
Other
875 stars 171 forks source link

commonmark renderer renders incompatible table with width limit #353

Open matthewhughes934 opened 8 months ago

matthewhughes934 commented 8 months ago

Given the content:

# Table test

| column 1 | column 2 |
| --- | --- |
| this is a very long row | longer than width limit |

Rendering with a width less than that of one of the rows of the table gives:

$ cmark-gfm --extension table --width 40 --to commonmark table.md 
# Table test

| column 1 | column 2 |
| --- | --- |
| this is a very long row | longer than
width limit |

Which is not compatible with the original content since it will render an extra row:

$ cmark-gfm --extension table --width 40 --to commonmark table.md | cmark-gfm --to html --extension table
<h1>Table test</h1>
<table>
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>this is a very long row</td>
<td>longer than</td>
</tr>
<tr>
<td>width limit</td>
<td></td>
</tr>
</tbody>
</table>

From what I can tell reading the spec https://github.github.com/gfm/#tables-extension- there's no support for breaking table rows over several lines, so I suppose the commonmark render should never try to break these lines, regardless of width?

matthewhughes934 commented 8 months ago

Here's a quick hack copying some behaviour from how titles behave (which also can't be broken)

diff --git a/extensions/table.c b/extensions/table.c
index e8359f2..61ee353 100644
--- a/extensions/table.c
+++ b/extensions/table.c
@@ -567,6 +567,7 @@ static void commonmark_render(cmark_syntax_extension *extension,
   if (node->type == CMARK_NODE_TABLE) {
     renderer->blankline(renderer);
   } else if (node->type == CMARK_NODE_TABLE_ROW) {
+    renderer->no_linebreaks = entering;
     if (entering) {
       renderer->cr(renderer);
       renderer->out(renderer, node, "|", false, LITERAL);

At a glance I didn't see any tests covering conversion to commonmark though, so not sure how valid this is