lzakharov / csv2md

Command line tool for converting CSV files into Markdown tables.
MIT License
104 stars 8 forks source link

Escape pipe symbol in items #21

Closed varac closed 1 day ago

varac commented 4 days ago

Problem

Given a table with a pipe symbol (|) in an item, glow wrongly splits the item into pieces seperated by the pipe symbol. This messes up the table when it is post-rendered by other markdown tools (i.e. glow).

Steps to reproduce:

See the wrongly formatted line 2 of the table in the last command output (glow):

$ cat table-with-pipe-symbols.csv 
"a","b"
"foo","bar"
"test|with|pipe|symbol","baz"
"test\|with\|pipe\|symbol","baz"
"test|with|escaped|pipe","baz"

$ cat table-with-pipe-symbols.csv | csv2md
| a                                     | b   |
| ------------------------------------- | --- |
| foo                                   | bar |
| test|with|pipe|symbol                 | baz |
| test\|with\|pipe\|symbol              | baz |
| test|with|escaped|pipe | baz |

$ cat table-with-pipe-symbols.csv | csv2md | glow

  a                                                │b                             
  ─────────────────────────────────────────────────┼──────────────────────────────
  foo                                              │bar                           
  test                                             │with                          
  test|with|pipe|symbol                            │baz                           
  test|with|escaped|pipe                           │baz          

Solution

Please properly escape pipe symbols (|) in csv items, as the Markdownguide: Escaping Pipe Characters in ables proposes:

You can display a pipe (|) character in a table by using its HTML character code (|).

Escaping with a backslash (\|) also works, and it much more readabled, so this might be the preferred solution.

varac commented 4 days ago

Just found out that miller also had this problem and they fixed it with backslahing the pipe: https://github.com/johnkerl/miller/pull/691

lzakharov commented 2 days ago

Hi, @varac! Thanks for this issue, seems reasonable. Will try to fix it this week.

varac commented 1 day ago

Thanks !!