From 850e31c83d310a1b52e6105fe201eb96f9e33deb Mon Sep 17 00:00:00 2001 From: Selase Krakani <849886+s3lase@users.noreply.github.com> Date: Mon, 27 Feb 2023 15:49:55 +0000 Subject: [PATCH] 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. --- .../discourse-table-builder/lib/utilities.js | 7 ++++--- test/unit/lib/utilities-test.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/javascripts/discourse-table-builder/lib/utilities.js b/javascripts/discourse-table-builder/lib/utilities.js index 2c270ae..6a14164 100644 --- a/javascripts/discourse-table-builder/lib/utilities.js +++ b/javascripts/discourse-table-builder/lib/utilities.js @@ -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; } diff --git a/test/unit/lib/utilities-test.js b/test/unit/lib/utilities-test.js index bcb8a90..4d0b365 100644 --- a/test/unit/lib/utilities-test.js +++ b/test/unit/lib/utilities-test.js @@ -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|`;