@claritylabs/cl-sdk 3.1.11 → 3.1.13

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.mjs CHANGED
@@ -10888,7 +10888,7 @@ function appendDistinctText(base, addition) {
10888
10888
  if (!current) return next || void 0;
10889
10889
  if (!next) return current;
10890
10890
  if (current.toLowerCase().includes(next.toLowerCase())) return current;
10891
- const delimiter = /(?:[/(:;-]|,\s*)$/.test(current) ? " " : " / ";
10891
+ const delimiter = /(?:[/(:;-]|,\s*)$/.test(current) || /^[a-z]+$/i.test(next) ? " " : " / ";
10892
10892
  return cleanText(`${current}${delimiter}${next}`, current);
10893
10893
  }
10894
10894
  function mergedBbox(nodes) {
@@ -10917,12 +10917,65 @@ function findCellForContinuation(params) {
10917
10917
  }
10918
10918
  return params.cells[1] ?? params.cells[params.cells.length - 1];
10919
10919
  }
10920
+ function bboxCenterX(node) {
10921
+ const box = node.bbox?.[0];
10922
+ if (!box) return void 0;
10923
+ return box.x + box.width / 2;
10924
+ }
10925
+ function findCellByVisualPosition(sourceCell, targetCells) {
10926
+ const sourceX = bboxCenterX(sourceCell);
10927
+ if (sourceX === void 0) return void 0;
10928
+ const positioned = targetCells.map((cell) => ({ cell, centerX: bboxCenterX(cell) })).filter(
10929
+ (entry) => entry.centerX !== void 0
10930
+ );
10931
+ if (positioned.length === 0) return void 0;
10932
+ positioned.sort(
10933
+ (left, right) => Math.abs(left.centerX - sourceX) - Math.abs(right.centerX - sourceX)
10934
+ );
10935
+ return positioned[0]?.cell;
10936
+ }
10920
10937
  function columnLabelStartIndex(rows) {
10921
10938
  const headerIndex = rows.findIndex(
10922
10939
  ({ row, cells }) => isSourceTreeHeaderRow(row) && cells.length > 1
10923
10940
  );
10924
10941
  return headerIndex >= 0 ? headerIndex : 0;
10925
10942
  }
10943
+ function startsNewVisualTableItem(cells) {
10944
+ const firstText = tableCellText(cells[0]);
10945
+ return /^(?:item\s+\d+\b|[A-Z][.)]\s+|coverage\s+part\b|endorsement\s+(?:no\.?|number|#|\d+)\b)/i.test(firstText);
10946
+ }
10947
+ function rowMatchesColumnLabels(cells, columnLabels) {
10948
+ if (columnLabels.size === 0 || cells.length < columnLabels.size) return false;
10949
+ return cells.every((cell, index) => {
10950
+ const label = columnLabels.get(tableCellColumnIndex(cell, index));
10951
+ return !label || tableCellText(cell).toLowerCase() === label.toLowerCase();
10952
+ });
10953
+ }
10954
+ function implicitContinuationRows(rows, columnLabels, firstLabelRowIndex) {
10955
+ const continuations = [];
10956
+ let targetRowId;
10957
+ const targetColumnLabel = columnLabels.get(1);
10958
+ for (const [rowIndex, { row, cells }] of rows.entries()) {
10959
+ if (rowIndex < firstLabelRowIndex) continue;
10960
+ if (rowIndex === firstLabelRowIndex && rowMatchesColumnLabels(cells, columnLabels)) continue;
10961
+ if (startsNewVisualTableItem(cells)) {
10962
+ targetRowId = row.id;
10963
+ continue;
10964
+ }
10965
+ if (!targetRowId || cells.length === 0) continue;
10966
+ const isSpuriousHeader = isSourceTreeHeaderRow(row) && !rowMatchesColumnLabels(cells, columnLabels);
10967
+ const isShortContinuation = !isSourceTreeHeaderRow(row) && cells.length < Math.max(2, columnLabels.size);
10968
+ if (!isSpuriousHeader && !isShortContinuation) continue;
10969
+ continuations.push({
10970
+ sourceRowNodeId: row.id,
10971
+ targetRowNodeId: targetRowId,
10972
+ targetColumnIndex: 1,
10973
+ targetColumnLabel,
10974
+ reason: "Continuation row inferred from repaired table header."
10975
+ });
10976
+ }
10977
+ return continuations;
10978
+ }
10926
10979
  function metadataWithColumnLabel(metadata, label) {
10927
10980
  const next = {
10928
10981
  ...metadata ?? {},
@@ -10963,8 +11016,8 @@ function applyVisualTableRepair(sourceTree, repair) {
10963
11016
  const normalized = normalizedRepairLabel(label.label);
10964
11017
  if (normalized) columnLabels.set(label.columnIndex, normalized);
10965
11018
  }
11019
+ const firstLabelRowIndex = columnLabelStartIndex(rows);
10966
11020
  if (columnLabels.size > 0) {
10967
- const firstLabelRowIndex = columnLabelStartIndex(rows);
10968
11021
  const normalizedLabels = new Set([...columnLabels.values()].map((label) => label.toLowerCase()));
10969
11022
  for (const [rowIndex, { row, cells }] of rows.entries()) {
10970
11023
  if (removeIds.has(row.id)) continue;
@@ -11002,7 +11055,15 @@ function applyVisualTableRepair(sourceTree, repair) {
11002
11055
  if (cells.length > 0) rowsToRebuildText.add(row.id);
11003
11056
  }
11004
11057
  }
11005
- for (const continuation of tableRepair.continuationRows) {
11058
+ const continuationRows = [
11059
+ ...tableRepair.continuationRows,
11060
+ ...implicitContinuationRows(rows, columnLabels, firstLabelRowIndex)
11061
+ ];
11062
+ const seenContinuationRows = /* @__PURE__ */ new Set();
11063
+ for (const continuation of continuationRows) {
11064
+ const continuationKey = `${continuation.sourceRowNodeId}:${continuation.targetRowNodeId}`;
11065
+ if (seenContinuationRows.has(continuationKey)) continue;
11066
+ seenContinuationRows.add(continuationKey);
11006
11067
  if (!rowIds.has(continuation.sourceRowNodeId) || !rowIds.has(continuation.targetRowNodeId)) continue;
11007
11068
  if (continuation.sourceRowNodeId === continuation.targetRowNodeId) continue;
11008
11069
  const sourceIndex = rowOrder.get(continuation.sourceRowNodeId);
@@ -11024,7 +11085,41 @@ function applyVisualTableRepair(sourceTree, repair) {
11024
11085
  });
11025
11086
  const sourceNodes = [sourceRow, ...sourceCells];
11026
11087
  const sourceSpanIds = sourceSpanIdsForNodes(sourceNodes);
11027
- if (targetCell) {
11088
+ let mergedIntoCells = false;
11089
+ if (sourceCells.length > 0 && targetCells.length > 0) {
11090
+ for (const sourceCell of sourceCells) {
11091
+ const cellText = tableCellValueText(sourceCell);
11092
+ if (!cellText) continue;
11093
+ const visualTargetCell = findCellByVisualPosition(sourceCell, targetCells) ?? targetCell;
11094
+ if (!visualTargetCell) continue;
11095
+ const currentTargetCell = currentNode(visualTargetCell.id) ?? visualTargetCell;
11096
+ const nextCellText = appendDistinctText(tableCellText(currentTargetCell), cellText);
11097
+ if (!nextCellText) continue;
11098
+ updates.set(visualTargetCell.id, {
11099
+ ...currentTargetCell,
11100
+ textExcerpt: nextCellText,
11101
+ description: cleanText(
11102
+ [currentTargetCell.title, nextCellText].filter(Boolean).join(" | "),
11103
+ currentTargetCell.description
11104
+ ),
11105
+ sourceSpanIds: [
11106
+ .../* @__PURE__ */ new Set([
11107
+ ...currentTargetCell.sourceSpanIds,
11108
+ ...sourceRow.sourceSpanIds,
11109
+ ...sourceCell.sourceSpanIds
11110
+ ])
11111
+ ],
11112
+ bbox: mergedBbox([currentTargetCell, sourceCell]),
11113
+ metadata: {
11114
+ ...currentTargetCell.metadata ?? {},
11115
+ visualTableRepair: "merged_continuation"
11116
+ }
11117
+ });
11118
+ mergedIntoCells = true;
11119
+ }
11120
+ if (mergedIntoCells) rowsToRebuildText.add(targetRow.id);
11121
+ }
11122
+ if (!mergedIntoCells && targetCell) {
11028
11123
  const nextCellText = appendDistinctText(tableCellText(targetCell), sourceText);
11029
11124
  if (nextCellText) {
11030
11125
  const mergedNodes = [targetCell, ...sourceNodes];
@@ -11042,11 +11137,16 @@ function applyVisualTableRepair(sourceTree, repair) {
11042
11137
  rowsToRebuildText.add(targetRow.id);
11043
11138
  }
11044
11139
  }
11045
- const nextRowText = appendDistinctText(targetRow.textExcerpt ?? targetRow.description, sourceText);
11140
+ const fallbackRowText = mergedIntoCells ? void 0 : appendDistinctText(targetRow.textExcerpt ?? targetRow.description, sourceText);
11046
11141
  updates.set(targetRow.id, {
11047
11142
  ...currentNode(targetRow.id) ?? targetRow,
11048
- textExcerpt: nextRowText,
11049
- description: cleanText([targetRow.title, nextRowText].filter(Boolean).join(" | "), targetRow.description),
11143
+ ...mergedIntoCells ? {} : {
11144
+ textExcerpt: fallbackRowText,
11145
+ description: cleanText(
11146
+ [targetRow.title, fallbackRowText].filter(Boolean).join(" | "),
11147
+ targetRow.description
11148
+ )
11149
+ },
11050
11150
  sourceSpanIds: [.../* @__PURE__ */ new Set([...targetRow.sourceSpanIds, ...sourceSpanIds])],
11051
11151
  bbox: mergedBbox([targetRow, ...sourceNodes]),
11052
11152
  metadata: {