-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdownUtils.js
More file actions
67 lines (54 loc) · 2.11 KB
/
markdownUtils.js
File metadata and controls
67 lines (54 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function formatMarkdownTable(markdownTable) {
// 1. Split into lines and filter empty strings
const lines = markdownTable
.trim()
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0);
// 2. Parse rows into cells
const rows = lines.map((line) => {
// Remove leading/trailing pipes if present to avoid empty first/last cells
const content = line.replace(/^\||\|$/g, '');
return content.split('|').map((cell) => cell.trim());
});
if (rows.length === 0) return '';
const colCount = rows[0].length;
const colWidths = new Array(colCount).fill(0);
// 3. Calculate max width for each column
rows.forEach((row) => {
row.forEach((cell, i) => {
// Ensure we don't exceed the column count defined by the header
if (i < colCount) {
// Minimum width of 3 is standard for markdown (e.g., "---")
colWidths[i] = Math.max(colWidths[i], cell.length, 3);
}
});
});
// 4. Reconstruct the table
const formattedLines = rows.map((row, rowIndex) => {
// Detect if this is the separator row (usually index 1, contains only dashes/colons)
const isSeparator = rowIndex === 1 && /^[:\s-]*$/.test(row.join(''));
const formattedCells = row
.map((cell, i) => {
if (i >= colCount) return null; // Skip extra cells if row is too long
const targetWidth = colWidths[i];
if (isSeparator) {
// Handle alignment markers (e.g., :---, ---:, :---:)
const hasLeftColon = cell.startsWith(':');
const hasRightColon = cell.endsWith(':');
// Build the separator line
const start = hasLeftColon ? ':' : '-';
const end = hasRightColon ? ':' : '-';
const dashes = '-'.repeat(targetWidth - 2);
return start + dashes + end;
} else {
// Standard data cell: Pad with spaces on the right
return cell.padEnd(targetWidth, ' ');
}
})
.filter((c) => c !== null); // Remove skipped cells
return `| ${formattedCells.join(' | ')} |`;
});
return formattedLines.join('\n');
}
export default formatMarkdownTable;