A platform for community discussion. Free, open, simple.
FIX: Make table builder escape `|` (#27726)
锦心df544a51baea
diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.jsindex d16c394a342..88a76ebc0f4 100644--- a/app/assets/javascripts/discourse/app/lib/utilities.js+++ b/app/assets/javascripts/discourse/app/lib/utilities.js@@ -666,10 +666,9 @@ export function arrayToTable(array, cols, colPrefix = "col", alignments) {table +=cols.map(function (_key, index) {- return String(item[`${colPrefix}${index}`] || "").replace(- /\r?\n|\r/g,- " "- );+ return String(item[`${colPrefix}${index}`] || "")+ .replace(/\r?\n|\r/g, " ")+ .replaceAll("|", "\\|");}).join(" | ") + "|\n";});diff --git a/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js b/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.jsindex 0bc0471e734..70d548b5844 100644--- a/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js+++ b/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js@@ -430,6 +430,23 @@ module("Unit | Utilities | table-builder", function (hooks) {);});+ test("arrayToTable should escape `|`", function (assert) {+ const tableData = [+ {+ col0: "`a|b`",+ col1: "",+ col2: "",+ col3: "|",+ },+ { col0: "1|1", col1: "2|2", col2: "3|3", col3: "4|4" },+ ];+ assert.strictEqual(+ arrayToTable(tableData, ["Col 1", "Col 2", "Col 3", "Col 4"]),+ "|Col 1 | Col 2 | Col 3 | Col 4|\n|--- | --- | --- | ---|\n|`a\\|b` |  | | \\||\n|1\\|1 | 2\\|2 | 3\\|3 | 4\\|4|\n",+ "it creates a valid table"+ );+ });+test("findTableRegex", function (assert) {const oneTable = `|Make|Model|Year|\n|--- | --- | ---|\n|Toyota|Supra|1998|`;