A platform for community discussion. Free, open, simple.
FIX: lean on markdown tokenization to find tables in editor (#37658)
Samd10b6be86f5a
diff --git a/frontend/discourse/app/components/modal/spreadsheet-editor.gjs b/frontend/discourse/app/components/modal/spreadsheet-editor.gjsindex 0bc62480ac3..b6979f9d786 100644--- a/frontend/discourse/app/components/modal/spreadsheet-editor.gjs+++ b/frontend/discourse/app/components/modal/spreadsheet-editor.gjs@@ -13,7 +13,7 @@ import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import {arrayToTable,- findTableRegex,+ replaceTableRaw,tokenRange,} from "discourse/lib/utilities";import { i18n } from "discourse-i18n";@@ -199,6 +199,8 @@ export default class SpreadsheetEditor extends Component {}buildPopulatedTable(tableTokens) {+ const tableOpen = tableTokens.find((t) => t.type === "table_open");+ this.tableLineRange = tableOpen?.map;const contentRows = tokenRange(tableTokens, "tr_open", "tr_close");const rows = [];let headings;@@ -255,16 +257,10 @@ export default class SpreadsheetEditor extends Component {}buildUpdatedPost(tableIndex, raw, newRaw) {- const tableToEdit = raw.match(findTableRegex());- let editedTable;-- if (tableToEdit.length) {- editedTable = raw.replace(tableToEdit[tableIndex], newRaw);- } else {+ if (!this.tableLineRange) {return raw;}-- // replace null characters+ let editedTable = replaceTableRaw(raw, this.tableLineRange, newRaw);editedTable = editedTable.replace(/\0/g, "\ufffd");return editedTable;}diff --git a/frontend/discourse/app/lib/utilities.js b/frontend/discourse/app/lib/utilities.jsindex e563ca18a8b..d68ef5438b6 100644--- a/frontend/discourse/app/lib/utilities.js+++ b/frontend/discourse/app/lib/utilities.js@@ -690,6 +690,13 @@ export function findTableRegex() {return /((\r?){2}|^)(^\|[^\r\n]*(\r?\n)?)+(?=(\r?\n){2}|$)/gm;}+export function replaceTableRaw(raw, lineRange, newTableRaw) {+ const lines = raw.split("\n");+ const before = lines.slice(0, lineRange[0]);+ const after = lines.slice(lineRange[1]);+ return [...before, ...newTableRaw.trimEnd().split("\n"), ...after].join("\n");+}+export function tokenRange(tokens, start, end) {const contents = [];let startPushing = false;diff --git a/frontend/discourse/tests/unit/lib/utilities-test.js b/frontend/discourse/tests/unit/lib/utilities-test.jsindex 11f9bebb4c7..8ac499e6ec4 100644--- a/frontend/discourse/tests/unit/lib/utilities-test.js+++ b/frontend/discourse/tests/unit/lib/utilities-test.js@@ -16,6 +16,7 @@ import {inCodeBlock,initializeDefaultHomepage,mergeSortedLists,+ replaceTableRaw,setCaretPosition,setDefaultHomepage,slugify,@@ -472,6 +473,19 @@ module("Unit | Utilities | table-builder", function (hooks) {);});+ test("replaceTableRaw", function (assert) {+ const raw = "Some text\n\nMaterial | Value\n--- | ---\nA | 1\n\nMore text";+ const result = replaceTableRaw(+ raw,+ [2, 5],+ "| Material | Value |\n| --- | --- |\n| A | 1 |"+ );+ assert.strictEqual(+ result,+ "Some text\n\n| Material | Value |\n| --- | --- |\n| A | 1 |\n\nMore text"+ );+ });+test("findTableRegex", function (assert) {const oneTable = `|Make|Model|Year|\n|--- | --- | ---|\n|Toyota|Supra|1998|`;