From 59b061ccfe86116fabe125416ce6363e2af50a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=94=A6=E5=BF=83?= <41134017+Lhcfl@users.noreply.github.com> Date: Fri, 5 Jul 2024 07:38:11 +0800 Subject: [PATCH] FIX: uses `\n` for line breaks in table builder (#27711) The old implementation used unnecessary `\r\n` and caused the table generator to incorrectly add extra empty lines. This commit replaces it with `\n`, fixing the bug --- app/assets/javascripts/discourse/app/lib/utilities.js | 6 +++--- .../javascripts/discourse/tests/fixtures/md-table.js | 6 +++--- .../discourse/tests/unit/lib/utilities-test.js | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.js index 2de3aa38027..d16c394a342 100644 --- a/app/assets/javascripts/discourse/app/lib/utilities.js +++ b/app/assets/javascripts/discourse/app/lib/utilities.js @@ -645,7 +645,7 @@ export function arrayToTable(array, cols, colPrefix = "col", alignments) { // Generate table headers table += "|"; table += cols.join(" | "); - table += "|\r\n|"; + table += "|\n|"; const alignMap = { left: ":--", @@ -657,7 +657,7 @@ export function arrayToTable(array, cols, colPrefix = "col", alignments) { table += cols .map((_, index) => alignMap[String(alignments?.[index])] || "---") .join(" | "); - table += "|\r\n"; + table += "|\n"; // Generate table body array.forEach(function (item) { @@ -671,7 +671,7 @@ export function arrayToTable(array, cols, colPrefix = "col", alignments) { " " ); }) - .join(" | ") + "|\r\n"; + .join(" | ") + "|\n"; }); return table; diff --git a/app/assets/javascripts/discourse/tests/fixtures/md-table.js b/app/assets/javascripts/discourse/tests/fixtures/md-table.js index cf410f5c6a4..5802a943703 100644 --- a/app/assets/javascripts/discourse/tests/fixtures/md-table.js +++ b/app/assets/javascripts/discourse/tests/fixtures/md-table.js @@ -1,3 +1,3 @@ -export const mdTable = `|Make | Model | Year|\r\n|--- | --- | ---|\r\n|Toyota | Supra | 1998|\r\n|Nissan | Skyline | 1999|\r\n|Honda | S2000 | 2001|\r\n`; -export const mdTableSpecialChars = `|Make | Model | Price|\r\n|--- | --- | ---|\r\n|Toyota | Supra | $50,000|\r\n| | Celica | $20,000|\r\n|Nissan | GTR | $80,000|\r\n`; -export const mdTableNonUniqueHeadings = `|col1 | col2 | col1|\r\n|--- | --- | ---|\r\n|Col A | Col B | Col C|\r\n`; +export const mdTable = `|Make | Model | Year|\n|--- | --- | ---|\n|Toyota | Supra | 1998|\n|Nissan | Skyline | 1999|\n|Honda | S2000 | 2001|\n`; +export const mdTableSpecialChars = `|Make | Model | Price|\n|--- | --- | ---|\n|Toyota | Supra | $50,000|\n| | Celica | $20,000|\n|Nissan | GTR | $80,000|\n`; +export const mdTableNonUniqueHeadings = `|col1 | col2 | col1|\n|--- | --- | ---|\n|Col A | Col B | Col C|\n`; diff --git a/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js b/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js index c36b28e34a3..0bc0471e734 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js @@ -388,7 +388,7 @@ module("Unit | Utilities | table-builder", function (hooks) { assert.strictEqual( arrayToTable(tableData, ["Col 1", "Col 2"], "A"), - `|Col 1 | Col 2|\r\n|--- | ---|\r\n|hey | you|\r\n|over | there|\r\n`, + `|Col 1 | Col 2|\n|--- | ---|\n|hey | you|\n|over | there|\n`, "it works" ); }); @@ -407,7 +407,7 @@ module("Unit | Utilities | table-builder", function (hooks) { 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`, + `|Col 1 | Col 2|\n|--- | ---|\n|Jane Doe | Teri|\n|Finch | Sami|\n`, "it creates a valid table" ); }); @@ -425,13 +425,13 @@ module("Unit | Utilities | table-builder", function (hooks) { "col", alignment ), - "|Col 1 | Col 2 | Col 3 | Col 4|\r\n|:-- | :-: | --: | ---|\r\n|left | center | right | unspecificated|\r\n|111 | 222 | 333 | 444|\r\n", + "|Col 1 | Col 2 | Col 3 | Col 4|\n|:-- | :-: | --: | ---|\n|left | center | right | unspecificated|\n|111 | 222 | 333 | 444|\n", "it creates a valid table" ); }); test("findTableRegex", function (assert) { - const oneTable = `|Make|Model|Year|\r\n|--- | --- | ---|\r\n|Toyota|Supra|1998|`; + const oneTable = `|Make|Model|Year|\n|--- | --- | ---|\n|Toyota|Supra|1998|`; assert.strictEqual( oneTable.match(findTableRegex()).length,