Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/utils/markdown-table.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, test } from 'bun:test';
import { formatResponse, parseMarkdownTable, renderBoxTable } from './markdown-table.js';

describe('markdown table outer pipes', () => {
const headers = ['Name', 'Revenue', 'Profit'];
const rows = [['Acme', '100', '20']];
const borders = [
{ name: 'no outer pipes', left: '', right: '' },
{ name: 'a leading pipe only', left: '| ', right: '' },
{ name: 'a trailing pipe only', left: '', right: ' |' },
{ name: 'both outer pipes', left: '| ', right: ' |' },
];

for (const { name, left, right } of borders) {
test(`keeps all columns with ${name}`, () => {
const table = [headers, ['---', '---', '---'], ...rows]
.map(cells => `${left}${cells.join(' | ')}${right}`)
.join('\n');

expect(parseMarkdownTable(table)).toEqual({ headers, rows });
expect(formatResponse(table)).toBe(renderBoxTable(headers, rows));
});
}

for (const emptyIndex of [0, 1, 2]) {
test(`preserves an empty header in column ${emptyIndex + 1}`, () => {
const emptyHeaders = headers.map((header, index) => index === emptyIndex ? '' : header);
const table = [emptyHeaders, ['---', '---', '---'], ...rows]
.map(cells => `| ${cells.join(' | ')} |`)
.join('\n');

expect(parseMarkdownTable(table)).toEqual({ headers: emptyHeaders, rows });
});
}
});
15 changes: 5 additions & 10 deletions src/utils/markdown-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,11 @@ export function parseMarkdownTable(tableText: string): { headers: string[]; rows

const headers = headerLine
.split('|')
.map(cell => cell.trim())
.filter((_, i, arr) => i > 0 && i < arr.length - 1 || arr.length === 1);

// Handle edge case where there's no leading/trailing pipe
if (headers.length === 0) {
const rawHeaders = headerLine.split('|').map(cell => cell.trim());
if (rawHeaders.length > 0) {
headers.push(...rawHeaders);
}
}
.map(cell => cell.trim());

// Remove only the empty cells introduced by outer pipes.
if (headerLine.startsWith('|')) headers.shift();
if (headerLine.endsWith('|')) headers.pop();

if (headers.length === 0) return null;

Expand Down
Loading