@scrider/formatter 1.8.3 → 1.8.4

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
@@ -596,6 +596,46 @@ function promoteAlignFromCellOps(cell) {
596
596
  }
597
597
  return cell;
598
598
  }
599
+ function parseVerticalAlign(value) {
600
+ const normalized = value?.trim().toLowerCase();
601
+ if (!normalized) return null;
602
+ if (normalized === "top" || normalized === "baseline" || normalized === "text-top") return "top";
603
+ if (normalized === "middle" || normalized === "center") return "middle";
604
+ if (normalized === "bottom" || normalized === "text-bottom") return "bottom";
605
+ return null;
606
+ }
607
+ function extractInlineVerticalAlign(element) {
608
+ const verticalAlign = element.style?.getPropertyValue?.("vertical-align");
609
+ const fromStyle = parseVerticalAlign(verticalAlign);
610
+ if (fromStyle) return fromStyle;
611
+ const style = element.getAttribute("style") || "";
612
+ const match = style.match(/vertical-align:\s*(top|middle|bottom|center|baseline|text-top|text-bottom)/i);
613
+ if (match?.[1]) {
614
+ const parsed = parseVerticalAlign(match[1]);
615
+ if (parsed) return parsed;
616
+ }
617
+ const msoMatch = style.match(/mso-vertical-align:\s*(top|middle|bottom|center)/i);
618
+ if (msoMatch?.[1]) {
619
+ const parsed = parseVerticalAlign(msoMatch[1]);
620
+ if (parsed) return parsed;
621
+ }
622
+ return null;
623
+ }
624
+ function extractCellVAlign(cell) {
625
+ const direct = extractInlineVerticalAlign(cell);
626
+ if (direct) return direct;
627
+ const children = cell.childNodes;
628
+ for (let i = 0; i < children.length; i++) {
629
+ const child = children[i];
630
+ if (!child || !isElement(child)) continue;
631
+ const tag = child.tagName.toLowerCase();
632
+ if (tag === "p" || tag === "div") {
633
+ const vAlign = extractInlineVerticalAlign(child);
634
+ if (vAlign) return vAlign;
635
+ }
636
+ }
637
+ return null;
638
+ }
599
639
  function renderExtendedRow(data, row, cols, defaultCellTag, context, pretty) {
600
640
  const nl = pretty ? "\n" : "";
601
641
  const ind = (level) => pretty ? " ".repeat(level) : "";
@@ -617,14 +657,17 @@ function renderExtendedRow(data, row, cols, defaultCellTag, context, pretty) {
617
657
  }
618
658
  const colDefault = data.colAligns?.[c] ?? "left";
619
659
  const effectiveAlign = resolveCellHorizontalAlign(cell, c, data.colAligns);
620
- let alignStyle = null;
660
+ const styleParts = [];
621
661
  if (effectiveAlign !== "left") {
622
- alignStyle = `text-align: ${effectiveAlign}`;
662
+ styleParts.push(`text-align: ${effectiveAlign}`);
623
663
  } else if (cell.align === "left" && colDefault !== "left") {
624
- alignStyle = "text-align: left";
664
+ styleParts.push("text-align: left");
665
+ }
666
+ if (cell.vAlign !== void 0 && cell.vAlign !== "top") {
667
+ styleParts.push(`vertical-align: ${cell.vAlign}`);
625
668
  }
626
- if (alignStyle) {
627
- attrs.push(`style="${alignStyle}"`);
669
+ if (styleParts.length > 0) {
670
+ attrs.push(`style="${styleParts.join("; ")}"`);
628
671
  }
629
672
  const attrStr = attrs.length > 0 ? " " + attrs.join(" ") : "";
630
673
  let content = "";
@@ -646,6 +689,9 @@ function isGfmCompatible(data) {
646
689
  for (const cell of Object.values(data.cells)) {
647
690
  if (cell !== null && cell.align !== void 0) return false;
648
691
  }
692
+ for (const cell of Object.values(data.cells)) {
693
+ if (cell !== null && cell.vAlign !== void 0) return false;
694
+ }
649
695
  for (const cell of Object.values(data.cells)) {
650
696
  if (cell === null) return false;
651
697
  if (cell.colspan && cell.colspan > 1) return false;
@@ -859,6 +905,10 @@ function parseTableElement(table, context) {
859
905
  delete withoutAlign.align;
860
906
  cellData = withoutAlign;
861
907
  }
908
+ const htmlVAlign = extractCellVAlign(cell);
909
+ if (htmlVAlign && htmlVAlign !== "top") {
910
+ cellData = { ...cellData, vAlign: htmlVAlign };
911
+ }
862
912
  const cellKey = `${rowIdx}:${colIdx}`;
863
913
  cells[cellKey] = cellData;
864
914
  rawAligns[cellKey] = effectiveAlign;