fix: markdown tables drop empty cells and misalign columns (#1164)

* refactor: extract splitTableRow helper for markdown tables

* fix: keep empty interior cells in markdown tables to preserve columns

* test: splitTableRow keeps empty interior cells
This commit is contained in:
Afonso Coutinho
2026-06-02 14:41:27 +01:00
committed by GitHub
parent 6063fc51e0
commit 15a2662119
3 changed files with 67 additions and 1 deletions

View File

@@ -5,6 +5,7 @@
*/
import uiModule from './ui.js';
import { splitTableRow } from './markdown/tableRow.js';
var escapeHtml = uiModule.esc;
@@ -535,7 +536,7 @@ export function mdToHtml(src) {
let html = '<table style="border-collapse: collapse; width: 100%; margin: 10px 0;">';
rows.forEach((row, idx) => {
const cells = row.split('|').filter(cell => cell.trim() !== '');
const cells = splitTableRow(row);
if (cells.length === 0) return;
html += idx === 1 ? '<tbody>' : '';

View File

@@ -0,0 +1,18 @@
// static/js/markdown/tableRow.js
//
// Pure helper for splitting a markdown table row into cells. No DOM —
// safe to import anywhere and to unit-test under node.
// Split a "| a | b | c |" row into trimmed cell strings.
//
// Strip only the optional leading/trailing pipe, then split — filtering out
// every empty cell (the old behaviour) dropped intentionally-empty interior
// cells too, so "| a | | c |" collapsed to 2 columns and misaligned with the
// header.
export function splitTableRow(row) {
return (row || '')
.replace(/^\s*\|/, '')
.replace(/\|\s*$/, '')
.split('|')
.map((cell) => cell.trim());
}