mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 10:52:45 +08:00
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:
parent
37413c0ecd
commit
df544a51ba
|
@ -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";
|
||||
});
|
||||
|
|
|
@ -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|`;
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user