petems / csv-to-md-table-action

A Github action to convert CSV to markdown
MIT License
11 stars 8 forks source link

Support for multiline table cells #241

Open mariuspaliga opened 1 year ago

mariuspaliga commented 1 year ago

It seems that the current version does not correctly convert when there are multiline inputs. Example:

title,description,snippet
first,line,"
  multi
  line
  input
"

This would result in broken md output.

One possible result could possibly be (but still not sure this is the correct one):

title  |  description  |  snippet
-------|---------------|--------------------------
first  |  line         |multi<br>  line<br>  input<br>      |
title description snippet
first line multi
line
input

Would it be possible to implement something like that?

petems commented 2 months ago

Hi @mariuspaliga! Sorry for the delay, I missed this in the Github email firehose.

Happy to give a shot of adding this usecase, I'll see if I can work on this over the weekend

petems commented 2 months ago

Man, this was a nerd snipe. I learnt a lot 😄

It is technically part of the CSV RFC I found out!

  1. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:

    "aaa","b CRLF bb","ccc" CRLF zzz,yyy,xxx

https://datatracker.ietf.org/doc/html/rfc4180

It's a bit of a pain with most of the existing libraries because they do various trim/whitespace removal. I did find one that does it, but it turns it into an array rather than a JSON object, but it does handle newlines in quotes:

//convert.js
const { parseCsv } = require("@jjkavalam/csv-parse")

function convert(csvString) {
  let csvrows = parseCsv(csvString)

  // remove empty elements
  csvrows = csvrows.filter(e => e.length);

  // regex newline-alikes 
  // TODO

  var arrayLength = csvrows.length;
  for (var i = 0; i < arrayLength; i++) {
    console.log(csvrows[i]);
  }

  return csvrows
}

module.exports = convert
console.log
    [ 'first', 'line', '\n  multi\n  line\n  input\n' ]

      at log (convert.js:22:13)

Gonna poke at it more tonight