Skip to content

Commit

Permalink
remark markdown table cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
RomneyDa committed Dec 4, 2024
1 parent 60923cb commit df75931
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions gui/src/components/markdown/utils/remarkTables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 = {
Expand All @@ -72,7 +72,7 @@ export function remarkTables() {
children: [{ type: "text", value: cell }],
})),
},
...bodyRows.map((row, i) => {
...bodyCells.map((row, i) => {
return {
type: "tableRow",
data: {
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit df75931

Please sign in to comment.