From df759312c85b9bac67c980d7814b5cf6b1988eea Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 3 Dec 2024 16:47:16 -0800 Subject: [PATCH] remark markdown table cleanup --- .../markdown/utils/remarkTables.tsx | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/gui/src/components/markdown/utils/remarkTables.tsx b/gui/src/components/markdown/utils/remarkTables.tsx index 8a17305299..f22542c2fc 100644 --- a/gui/src/components/markdown/utils/remarkTables.tsx +++ b/gui/src/components/markdown/utils/remarkTables.tsx @@ -32,7 +32,7 @@ export function remarkTables() { let match: RegExpExecArray | null; let lastIndex = 0; const newNodes = []; - + let failed = false; while ((match = tableRegex.exec(value)) !== null) { const fullTableString = match[0]; const headerGroup = match[1]; @@ -41,22 +41,22 @@ export function remarkTables() { if (!fullTableString || !headerGroup || !separatorGroup || !bodyGroup) { console.error("Markdown table regex failed to yield table groups"); - return; + failed = true; + break; } const headerCells = splitRow(headerGroup); const alignments = splitRow(separatorGroup).map((cell) => { - const trimmed = cell.trim(); - if (trimmed.startsWith(":") && trimmed.endsWith(":")) return "center"; - if (trimmed.endsWith(":")) return "right"; - if (trimmed.startsWith(":")) return "left"; + if (cell.startsWith(":") && cell.endsWith(":")) return "center"; + if (cell.endsWith(":")) return "right"; + if (cell.startsWith(":")) return "left"; return null; }); - const bodyRows = bodyGroup + const bodyCells = bodyGroup .trim() .split("\n") - .map((bodyRow) => splitRow(bodyRow.trim())); + .map((bodyRow) => splitRow(bodyRow)); try { const tableNode = { @@ -72,7 +72,7 @@ export function remarkTables() { children: [{ type: "text", value: cell }], })), }, - ...bodyRows.map((row, i) => { + ...bodyCells.map((row, i) => { return { type: "tableRow", data: { @@ -112,6 +112,10 @@ export function remarkTables() { lastIndex = tableRegex.lastIndex; } + if (failed) { + return; + } + // Add any remaining text after the last table if (lastIndex < value.length) { newNodes.push({