FIX: Remove linebreaks from cell data (#48)

Pasting text with linebreaks into the table UI results in incorrectly
generated table markdown.

This fix strips linebreaks from text during table markdown generation.
This commit is contained in:
Selase Krakani 2023-02-27 15:49:55 +00:00 committed by GitHub
parent 4c6b020fd8
commit 850e31c83d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -33,13 +33,14 @@ export function arrayToTable(array, cols, colPrefix = "col") {
table +=
cols
.map(function (_key, index) {
return String(item[`${colPrefix}${index}`] || "");
return String(item[`${colPrefix}${index}`] || "").replace(
/\r?\n|\r/g,
" "
);
})
.join(" | ") + "|\r\n";
});
// Return table
console.log(table);
return table;
}

View File

@ -91,6 +91,25 @@ discourseModule("Unit | Utilities", function () {
);
});
test("arrayToTable returns valid table with multiline cell data", function (assert) {
const tableData = [
{
col0: "Jane\nDoe",
col1: "Teri",
},
{
col0: "Finch",
col1: "Sami",
},
];
assert.strictEqual(
arrayToTable(tableData, ["Col 1", "Col 2"]),
`|Col 1 | Col 2|\r\n|--- | ---|\r\n|Jane Doe | Teri|\r\n|Finch | Sami|\r\n`,
"it creates a valid table"
);
});
test("findTableRegex", function (assert) {
const oneTable = `|Make|Model|Year|\r\n|--- | --- | ---|\r\n|Toyota|Supra|1998|`;