export type TableCell = { text: string; from: number; to: number }; export type TableRow = { from: number; to: number; indent: string; cells: TableCell[] }; export type MarkdownTable = { rows: TableRow[]; columns: number; eol: string; end: number }; export function hasTableDelimiter(line: string): boolean { return /(^|[^\\])(?:\\\\)*\|/.test(line); } function row(text: string, from: number): TableRow { const pipes: number[] = []; for (let index = 0; index < text.length; index++) { if (text[index] !== "|") continue; let slashes = 0; for (let before = index - 1; before >= 0 && text[before] === "\\"; before--) slashes++; if (slashes % 2 === 0) pipes.push(index); } const start = text.search(/\S/); const end = text.trimEnd().length; const left = pipes[0] === start ? pipes.shift()! + 1 : 0; const right = pipes.at(-1) === end - 1 ? pipes.pop()! : text.length; const boundaries = [left - 1, ...pipes, right]; const cells = boundaries.slice(0, -1).map((boundary, index) => { const rawFrom = boundary + 1, rawTo = boundaries[index + 1]; const raw = text.slice(rawFrom, rawTo); const leading = raw.length - raw.trimStart().length; const cellFrom = from + rawFrom + leading; return { text: raw.trim(), from: cellFrom, to: Math.max(cellFrom, from + rawTo - (raw.length - raw.trimEnd().length)) }; }); return { from, to: from + text.length, indent: /^\s*/.exec(text)![0], cells }; } export function parseMarkdownTable(source: string): MarkdownTable | null { const rows: TableRow[] = []; let offset = 0; const eol = source.includes("\r\n") ? "\r\n" : "\n"; for (const line of source.split(eol)) { if (!line.trim() || (rows.length >= 2 && !hasTableDelimiter(line))) break; rows.push(row(line, offset)); offset += line.length + eol.length; } if (rows.length < 2 || !source.slice(rows[0].from, rows[0].to).includes("|")) return null; const columns = rows[0].cells.length; if (rows[1].cells.length !== columns || !rows[1].cells.every(cell => /^:?-+:?$/.test(cell.text))) return null; return { rows, columns, eol, end: rows.at(-1)!.to }; } export function appendTableColumn(source: string): string { const table = parseMarkdownTable(source); if (!table) return source; const body = table.rows.map((row, index) => { const cells = row.cells.map(cell => cell.text); while (cells.length < table.columns) cells.push(""); cells.splice(table.columns, 0, index === 1 ? "---" : ""); return `${row.indent}| ${cells.join(" | ")} |`; }).join(table.eol); return body + source.slice(table.end); } export function appendTableRow(source: string): string { const table = parseMarkdownTable(source); if (!table) return source; const row = `${table.rows[0].indent}| ${Array.from({ length: table.columns }, () => "").join(" | ")} |`; return source.slice(0, table.end) + table.eol + row + source.slice(table.end); } // Change only the selected cell: padding, alignment, and every other row stay intact. export function replaceTableCell(source: string, rowIndex: number, column: number, text: string): string { const table = parseMarkdownTable(source); if (!table || rowIndex === 1 || column < 0 || column >= table.columns || /[\r\n]/.test(text)) return source; const row = table.rows[rowIndex]; if (!row) return source; const cell = row.cells[column]; if (cell) return source.slice(0, cell.from) + text + source.slice(cell.to); const cells = row.cells.map(cell => cell.text); while (cells.length <= column) cells.push(""); cells[column] = text; return source.slice(0, row.from) + `${row.indent}| ${cells.join(" | ")} |` + source.slice(row.to); }