@scrider/formatter 1.8.5 → 1.8.7

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
@@ -757,6 +757,7 @@ function toCodeWidgetEmbedUrl(url) {
757
757
  var EXT_TABLE_HOST_ATTR = "data-scrider-ext-table";
758
758
  var EXT_TABLE_HOST_CLASS = "scrider-ext-table-host";
759
759
  var VALID_TABLE_BLOCK_FLOATS = ["left", "center", "right"];
760
+ var VALID_TABLE_BLOCK_ALIGNS = ["left", "center", "right", "justify"];
760
761
  var VALID_CELL_HORIZONTAL_ALIGNS = [
761
762
  "left",
762
763
  "center",
@@ -783,6 +784,18 @@ function getGridDimensions(cells) {
783
784
  if (maxRow < 0 || maxCol < 0) return null;
784
785
  return [maxRow + 1, maxCol + 1];
785
786
  }
787
+ function rowspanCrossesHeaderBoundary(cells, headerRows) {
788
+ if (headerRows <= 0) return false;
789
+ for (const [key, cell] of Object.entries(cells)) {
790
+ if (cell === null) continue;
791
+ const parsed = parseCellKey(key);
792
+ if (!parsed) continue;
793
+ const [row] = parsed;
794
+ const rowspan = cell.rowspan ?? 1;
795
+ if (row < headerRows && row + rowspan > headerRows) return true;
796
+ }
797
+ return false;
798
+ }
786
799
  function isValidCellData(cell) {
787
800
  if (typeof cell !== "object" || cell === null || !Array.isArray(cell.ops) || cell.ops.length === 0) {
788
801
  return false;
@@ -948,6 +961,12 @@ function parseTableBlockFloat(value) {
948
961
  if (value === "left" || value === "center" || value === "right") return value;
949
962
  return void 0;
950
963
  }
964
+ function parseTableBlockAlign(value) {
965
+ if (value === "left" || value === "center" || value === "right" || value === "justify") {
966
+ return value;
967
+ }
968
+ return void 0;
969
+ }
951
970
  function extractHostBlockWidthPx(host) {
952
971
  const style = host.getAttribute("style") || "";
953
972
  const widthMatch = style.match(/(?:^|;\s*)width:\s*([\d.]+)px/i);
@@ -958,25 +977,32 @@ function extractHostBlockWidthPx(host) {
958
977
  }
959
978
  function enrichTableBlockFromHost(data, host) {
960
979
  const float = parseTableBlockFloat(host.getAttribute("data-float"));
980
+ const blockAlign = parseTableBlockAlign(host.getAttribute("data-block-align"));
961
981
  const width = extractHostBlockWidthPx(host);
962
- if (float == null && width == null) return data;
982
+ if (float == null && blockAlign == null && width == null) return data;
963
983
  const next = { ...data };
964
984
  if (float != null) next.float = float;
985
+ if (blockAlign != null && float == null) next.blockAlign = blockAlign;
965
986
  if (width != null) next.width = width;
966
987
  return next;
967
988
  }
968
989
  function tableNeedsHostWrapper(data) {
969
- return data.float != null && VALID_TABLE_BLOCK_FLOATS.includes(data.float) || data.width != null && data.width > 0;
990
+ return data.float != null && VALID_TABLE_BLOCK_FLOATS.includes(data.float) || data.width != null && data.width > 0 || data.float == null && data.blockAlign != null && VALID_TABLE_BLOCK_ALIGNS.includes(data.blockAlign);
970
991
  }
971
992
  function renderTableHostWrapperOpen(data, pretty) {
972
993
  const nl = pretty ? "\n" : "";
973
994
  const ind = pretty ? " " : "";
974
995
  const attrs = [`class="${EXT_TABLE_HOST_CLASS}"`, EXT_TABLE_HOST_ATTR];
975
996
  if (data.float) attrs.push(`data-float="${escapeHtml(data.float)}"`);
997
+ if (data.blockAlign && !data.float) {
998
+ attrs.push(`data-block-align="${escapeHtml(data.blockAlign)}"`);
999
+ }
976
1000
  const styleParts = [];
977
1001
  if (data.width != null && data.width > 0) {
978
1002
  styleParts.push(`width: ${Math.round(data.width)}px`);
979
1003
  styleParts.push("max-width: 100%");
1004
+ } else if (data.blockAlign === "justify" && !data.float) {
1005
+ styleParts.push("width: 100%");
980
1006
  }
981
1007
  if (styleParts.length > 0) attrs.push(`style="${styleParts.join("; ")}"`);
982
1008
  return `<div ${attrs.join(" ")}>${nl}${ind}`;
@@ -997,6 +1023,9 @@ function isGfmCompatible(data) {
997
1023
  if (data.float != null) {
998
1024
  return false;
999
1025
  }
1026
+ if (data.blockAlign != null) {
1027
+ return false;
1028
+ }
1000
1029
  for (const cell of Object.values(data.cells)) {
1001
1030
  if (cell !== null && cell.align !== void 0) return false;
1002
1031
  }
@@ -1085,6 +1114,26 @@ function collectTableRows(table) {
1085
1114
  }
1086
1115
  return { rows, headerRowCount };
1087
1116
  }
1117
+ function inferHeaderRowsFromThPrefix(rows) {
1118
+ let count = 0;
1119
+ for (const row of rows) {
1120
+ let hasCell = false;
1121
+ let allTh = true;
1122
+ const cellChildren = row.childNodes;
1123
+ for (let i = 0; i < cellChildren.length; i++) {
1124
+ const cell = cellChildren[i];
1125
+ if (!cell || !isElement(cell)) continue;
1126
+ const tag = cell.tagName.toLowerCase();
1127
+ if (tag !== "td" && tag !== "th") continue;
1128
+ hasCell = true;
1129
+ if (tag !== "th") allTh = false;
1130
+ }
1131
+ if (!hasCell) continue;
1132
+ if (!allTh) break;
1133
+ count++;
1134
+ }
1135
+ return count;
1136
+ }
1088
1137
  function extractColWidths(table) {
1089
1138
  const children = table.childNodes;
1090
1139
  for (let i = 0; i < children.length; i++) {
@@ -1172,7 +1221,13 @@ function extractInlineHorizontalAlign(element) {
1172
1221
  return null;
1173
1222
  }
1174
1223
  function parseTableElement(table, context) {
1175
- const { rows, headerRowCount } = collectTableRows(table);
1224
+ const collected = collectTableRows(table);
1225
+ const { rows } = collected;
1226
+ let headerRowCount = collected.headerRowCount;
1227
+ if (headerRowCount === 0) {
1228
+ const inferred = inferHeaderRowsFromThPrefix(rows);
1229
+ if (inferred > 0) headerRowCount = inferred;
1230
+ }
1176
1231
  if (rows.length === 0) return null;
1177
1232
  const cells = {};
1178
1233
  const colAligns = [];
@@ -1355,6 +1410,9 @@ var tableBlockHandler = {
1355
1410
  if (data.float !== void 0 && !VALID_TABLE_BLOCK_FLOATS.includes(data.float)) {
1356
1411
  return false;
1357
1412
  }
1413
+ if (data.blockAlign !== void 0 && !VALID_TABLE_BLOCK_ALIGNS.includes(data.blockAlign)) {
1414
+ return false;
1415
+ }
1358
1416
  if (data.colAligns !== void 0) {
1359
1417
  if (!Array.isArray(data.colAligns) || data.colAligns.length !== cols) {
1360
1418
  return false;
@@ -1390,14 +1448,15 @@ var tableBlockHandler = {
1390
1448
  }
1391
1449
  html += `${ind(1)}</colgroup>${nl}`;
1392
1450
  }
1393
- if (headerRows > 0) {
1451
+ const splitHeaderSection = headerRows > 0 && !rowspanCrossesHeaderBoundary(data.cells, headerRows);
1452
+ if (splitHeaderSection) {
1394
1453
  html += `${ind(1)}<thead>${nl}`;
1395
1454
  for (let r = 0; r < headerRows; r++) {
1396
1455
  html += renderExtendedRow(data, r, cols, "th", context, pretty);
1397
1456
  }
1398
1457
  html += `${ind(1)}</thead>${nl}`;
1399
1458
  }
1400
- const bodyStart = headerRows;
1459
+ const bodyStart = splitHeaderSection ? headerRows : 0;
1401
1460
  if (bodyStart < rows) {
1402
1461
  html += `${ind(1)}<tbody>${nl}`;
1403
1462
  for (let r = bodyStart; r < rows; r++) {