FIX: Make table builder escape | (#27726)

The original table builder does not escape |, which causes syntax like ![image|50x50](url) to be recognized as two different cells.
This commit fixes this issue

Related meta topic: https://meta.discourse.org/t/table-editor-breaks-embedded-images/314831
This commit is contained in:
锦心 2024-07-05 10:42:56 +08:00 committed by GitHub
parent 37413c0ecd
commit df544a51ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 4 deletions

View File

@ -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";
});

View File

@ -430,6 +430,23 @@ module("Unit | Utilities | table-builder", function (hooks) {
);
});
test("arrayToTable should escape `|`", function (assert) {
const tableData = [
{
col0: "`a|b`",
col1: "![image|200x50](/images/discourse-logo-sketch.png)",
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` | ![image\\|200x50](/images/discourse-logo-sketch.png) | | \\||\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|`;