node-html-to-text icon indicating copy to clipboard operation
node-html-to-text copied to clipboard

Should a table outputted using the dataTable formatter be longer than the wordwrap limit?

Open scorgn opened this issue 3 years ago • 2 comments

I am setting in the config wordwrap: 80 (just as an example) so that all of the output is wrapped at 80 characters. It seems to work beautifully, except when it comes to dataTable formatted tables. The tables seem to be however long they need to be, but then will wrap each individual cell after the cell length reaches 80 characters.

Looking at the documentation, technically that is what it says about the dataTables maxColumnWidth config.

maxColumnWidth: Data table cell content will be wrapped to fit this width instead of global wordwrap limit. Set this to undefined in order to fall back to wordwrap limit.

This appears to directly say that the max column width will default to the global wordwrap limit. Each column individually won't be longer than the wordwrap limit, but all of the columns side by side will. That also seems to be how it works. But, it seems so counterintuitive that I thought it would be worth asking to see if I'm doing something or understanding something wrong.

I can't imagine a scenario where you would want all output lines to be wrapped at 80 characters except for those lines that are part of a table. Is that how the dataTable formatter is supposed to work?

scorgn avatar Mar 28 '22 23:03 scorgn

Sorry for the late reply.

Yes, you got it right. Existing behavior is motivated more by the ease of implementation than by any specific usage scenario.

Trying to fit an arbitrary table into an overall width limit will require more complicated logic. In the current implementation the width of each column is computed independently in a single pass (and it is designed to work with colspan/rowspan nicely). Adding an overall budget will introduce an optimization problem - which columns should be shrinked when the available width is not enough, how much any column can be shrinked...

I have plans for other dataTable formatter improvements. It may or may not make it easier to approach this one later. I would love to make it possible to stretch smaller tables to a certain width as well. I think I will have to rewrite the table formatting code couple times before I can get through this. I still have higher priority goals, so no eta.

KillyMXI avatar Apr 04 '22 20:04 KillyMXI

Ah okay I see, that makes sense. I am using this for formatting emails for NeoMutt and a lot of emails use tables to align things a certain way. I created a JavaScript formatter that will format the table in the same way that the browser does (as described here). I tested it and validated that the output is the same as it is in Chrome for the same table when I use a monospace font and set the width of the table in ch (eg. 100 characters wide).

I took a stab at it from an email viewpoint based on a few emails I found. These are some things that I took into consideration with my implementation.

"Invalid" tables have each cell displayed just as a normal block element.

  • Tables that are only column wide are invalid.
  • Empty rows and empty columns should be removed from the table, if only one column is left afterwards the table is also invalid. (This was common in my emails because of images or something else.)
  • Nested tables are confusing to calculate and probably won't look good as text-only. Only the inner-most valid table remains valid. (Tables that have a valid child table are invalid.)
  • If the minimum width of all of the columns added together is more wide than the maximum width of the table, the table is too wide and invalid. (We don't want to break a word like the browser does).

The way I have it implemented plays nicely with colspan/rowspan (was a little bit messy when finding empty rows/columns), and would be fairly easy to add an option to add minimum table widths (stretching them) as well. If you're open to it, once you make other dataTable formatter changes I can adjust my formatter to those changes, write some tests, and open up an MR.

scorgn avatar Apr 05 '22 19:04 scorgn