@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.js CHANGED
@@ -466,6 +466,46 @@ function promoteAlignFromCellOps(cell) {
466
466
  }
467
467
  return cell;
468
468
  }
469
+ function parseVerticalAlign(value) {
470
+ const normalized = value?.trim().toLowerCase();
471
+ if (!normalized) return null;
472
+ if (normalized === "top" || normalized === "baseline" || normalized === "text-top") return "top";
473
+ if (normalized === "middle" || normalized === "center") return "middle";
474
+ if (normalized === "bottom" || normalized === "text-bottom") return "bottom";
475
+ return null;
476
+ }
477
+ function extractInlineVerticalAlign(element) {
478
+ const verticalAlign = element.style?.getPropertyValue?.("vertical-align");
479
+ const fromStyle = parseVerticalAlign(verticalAlign);
480
+ if (fromStyle) return fromStyle;
481
+ const style = element.getAttribute("style") || "";
482
+ const match = style.match(/vertical-align:\s*(top|middle|bottom|center|baseline|text-top|text-bottom)/i);
483
+ if (match?.[1]) {
484
+ const parsed = parseVerticalAlign(match[1]);
485
+ if (parsed) return parsed;
486
+ }
487
+ const msoMatch = style.match(/mso-vertical-align:\s*(top|middle|bottom|center)/i);
488
+ if (msoMatch?.[1]) {
489
+ const parsed = parseVerticalAlign(msoMatch[1]);
490
+ if (parsed) return parsed;
491
+ }
492
+ return null;
493
+ }
494
+ function extractCellVAlign(cell) {
495
+ const direct = extractInlineVerticalAlign(cell);
496
+ if (direct) return direct;
497
+ const children = cell.childNodes;
498
+ for (let i = 0; i < children.length; i++) {
499
+ const child = children[i];
500
+ if (!child || !isElement(child)) continue;
501
+ const tag = child.tagName.toLowerCase();
502
+ if (tag === "p" || tag === "div") {
503
+ const vAlign = extractInlineVerticalAlign(child);
504
+ if (vAlign) return vAlign;
505
+ }
506
+ }
507
+ return null;
508
+ }
469
509
  function renderExtendedRow(data, row, cols, defaultCellTag, context, pretty) {
470
510
  const nl = pretty ? "\n" : "";
471
511
  const ind = (level) => pretty ? " ".repeat(level) : "";
@@ -487,14 +527,17 @@ function renderExtendedRow(data, row, cols, defaultCellTag, context, pretty) {
487
527
  }
488
528
  const colDefault = data.colAligns?.[c] ?? "left";
489
529
  const effectiveAlign = resolveCellHorizontalAlign(cell, c, data.colAligns);
490
- let alignStyle = null;
530
+ const styleParts = [];
491
531
  if (effectiveAlign !== "left") {
492
- alignStyle = `text-align: ${effectiveAlign}`;
532
+ styleParts.push(`text-align: ${effectiveAlign}`);
493
533
  } else if (cell.align === "left" && colDefault !== "left") {
494
- alignStyle = "text-align: left";
534
+ styleParts.push("text-align: left");
535
+ }
536
+ if (cell.vAlign !== void 0 && cell.vAlign !== "top") {
537
+ styleParts.push(`vertical-align: ${cell.vAlign}`);
495
538
  }
496
- if (alignStyle) {
497
- attrs.push(`style="${alignStyle}"`);
539
+ if (styleParts.length > 0) {
540
+ attrs.push(`style="${styleParts.join("; ")}"`);
498
541
  }
499
542
  const attrStr = attrs.length > 0 ? " " + attrs.join(" ") : "";
500
543
  let content = "";
@@ -516,6 +559,9 @@ function isGfmCompatible(data) {
516
559
  for (const cell of Object.values(data.cells)) {
517
560
  if (cell !== null && cell.align !== void 0) return false;
518
561
  }
562
+ for (const cell of Object.values(data.cells)) {
563
+ if (cell !== null && cell.vAlign !== void 0) return false;
564
+ }
519
565
  for (const cell of Object.values(data.cells)) {
520
566
  if (cell === null) return false;
521
567
  if (cell.colspan && cell.colspan > 1) return false;
@@ -729,6 +775,10 @@ function parseTableElement(table, context) {
729
775
  delete withoutAlign.align;
730
776
  cellData = withoutAlign;
731
777
  }
778
+ const htmlVAlign = extractCellVAlign(cell);
779
+ if (htmlVAlign && htmlVAlign !== "top") {
780
+ cellData = { ...cellData, vAlign: htmlVAlign };
781
+ }
732
782
  const cellKey = `${rowIdx}:${colIdx}`;
733
783
  cells[cellKey] = cellData;
734
784
  rawAligns[cellKey] = effectiveAlign;