UNPKG

ts-markdown-builder

Version:
27 lines (26 loc) 1.06 kB
"use strict"; exports.table = table; function table(header, rows, options) { if (header.length === 0) { return ''; } header = header.map(escapeCellContent); rows = rows.map(row => row.map(escapeCellContent)); const widths = !options?.compact ? getColumnsWidth(header, rows) : header.map(() => 1); const separators = header.map((_, i) => '-'.repeat(widths[i] ?? 0)); return [renderRow(header, widths), renderRow(separators, widths), ...rows.map(row => renderRow(row, widths))].join('\n'); } function getColumnsWidth(header, rows) { return header.map((_, index) => getColumnWidth(header, rows, index)); } function getColumnWidth(header, rows, index) { return Math.max(header[index]?.length ?? 0, ...rows.map(row => row[index]?.length ?? 0)); } function renderRow(row, widths) { const paddedCells = row.map((cell, index) => cell.padEnd(widths[index] ?? 0)); return `| ${paddedCells.join(' | ')} |`; } function escapeCellContent(cell) { return cell.replace(/\|/g, '\\|').replace(/\n/g, '<br/>'); } //# sourceMappingURL=table.js.map