@scrider/formatter 1.8.5 → 1.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -783,6 +783,18 @@ function getGridDimensions(cells) {
783
783
  if (maxRow < 0 || maxCol < 0) return null;
784
784
  return [maxRow + 1, maxCol + 1];
785
785
  }
786
+ function rowspanCrossesHeaderBoundary(cells, headerRows) {
787
+ if (headerRows <= 0) return false;
788
+ for (const [key, cell] of Object.entries(cells)) {
789
+ if (cell === null) continue;
790
+ const parsed = parseCellKey(key);
791
+ if (!parsed) continue;
792
+ const [row] = parsed;
793
+ const rowspan = cell.rowspan ?? 1;
794
+ if (row < headerRows && row + rowspan > headerRows) return true;
795
+ }
796
+ return false;
797
+ }
786
798
  function isValidCellData(cell) {
787
799
  if (typeof cell !== "object" || cell === null || !Array.isArray(cell.ops) || cell.ops.length === 0) {
788
800
  return false;
@@ -1085,6 +1097,26 @@ function collectTableRows(table) {
1085
1097
  }
1086
1098
  return { rows, headerRowCount };
1087
1099
  }
1100
+ function inferHeaderRowsFromThPrefix(rows) {
1101
+ let count = 0;
1102
+ for (const row of rows) {
1103
+ let hasCell = false;
1104
+ let allTh = true;
1105
+ const cellChildren = row.childNodes;
1106
+ for (let i = 0; i < cellChildren.length; i++) {
1107
+ const cell = cellChildren[i];
1108
+ if (!cell || !isElement(cell)) continue;
1109
+ const tag = cell.tagName.toLowerCase();
1110
+ if (tag !== "td" && tag !== "th") continue;
1111
+ hasCell = true;
1112
+ if (tag !== "th") allTh = false;
1113
+ }
1114
+ if (!hasCell) continue;
1115
+ if (!allTh) break;
1116
+ count++;
1117
+ }
1118
+ return count;
1119
+ }
1088
1120
  function extractColWidths(table) {
1089
1121
  const children = table.childNodes;
1090
1122
  for (let i = 0; i < children.length; i++) {
@@ -1172,7 +1204,13 @@ function extractInlineHorizontalAlign(element) {
1172
1204
  return null;
1173
1205
  }
1174
1206
  function parseTableElement(table, context) {
1175
- const { rows, headerRowCount } = collectTableRows(table);
1207
+ const collected = collectTableRows(table);
1208
+ const { rows } = collected;
1209
+ let headerRowCount = collected.headerRowCount;
1210
+ if (headerRowCount === 0) {
1211
+ const inferred = inferHeaderRowsFromThPrefix(rows);
1212
+ if (inferred > 0) headerRowCount = inferred;
1213
+ }
1176
1214
  if (rows.length === 0) return null;
1177
1215
  const cells = {};
1178
1216
  const colAligns = [];
@@ -1390,14 +1428,15 @@ var tableBlockHandler = {
1390
1428
  }
1391
1429
  html += `${ind(1)}</colgroup>${nl}`;
1392
1430
  }
1393
- if (headerRows > 0) {
1431
+ const splitHeaderSection = headerRows > 0 && !rowspanCrossesHeaderBoundary(data.cells, headerRows);
1432
+ if (splitHeaderSection) {
1394
1433
  html += `${ind(1)}<thead>${nl}`;
1395
1434
  for (let r = 0; r < headerRows; r++) {
1396
1435
  html += renderExtendedRow(data, r, cols, "th", context, pretty);
1397
1436
  }
1398
1437
  html += `${ind(1)}</thead>${nl}`;
1399
1438
  }
1400
- const bodyStart = headerRows;
1439
+ const bodyStart = splitHeaderSection ? headerRows : 0;
1401
1440
  if (bodyStart < rows) {
1402
1441
  html += `${ind(1)}<tbody>${nl}`;
1403
1442
  for (let r = bodyStart; r < rows; r++) {