Escape pipes and newlines in CSV to Markdown table cells
Summary
CsvConverter writes cell values straight into the Markdown table without escaping. Two cases produce malformed output:
- A cell containing a literal
|adds an extra column separator. - A quoted CSV field with an embedded newline (valid CSV) splits the row in two.
In both cases the data rows no longer match the header column count, so the table renders incorrectly.
Reproduction
input: name,note\nAlice,a|b\nBob,plain
The Alice row renders as three columns instead of two. A quoted multi-line field like "line1\nline2" similarly breaks into two rows.
Fix
Added an _escape_cell helper that escapes | as \| and flattens \r\n / \n / \r to spaces, applied to header and data cells.
| Alice | a\|b |
| Bob | line1 line2 |
Tests
Added packages/markitdown/tests/test_csv_converter.py with test_pipe_in_cell_is_escaped (asserts every row has equal unescaped pipe count) and test_newline_in_cell_does_not_break_row. Both fail on the unpatched converter and pass after the change. The existing module test vectors still pass.
$ python -m pytest tests/test_csv_converter.py -q
2 passed
Developed with AI assistance and verified locally as described above.