@stll/folio-core 0.9.0 → 0.10.0

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.
Files changed (44) hide show
  1. package/dist/ai-edits/headless.d.ts +52 -12
  2. package/dist/ai-edits/headless.js +181 -55
  3. package/dist/ai-edits/index.d.ts +2 -2
  4. package/dist/ai-edits/index.js +2 -2
  5. package/dist/controller/layoutPipeline.js +2 -1
  6. package/dist/document-operations.d.ts +11 -2
  7. package/dist/document-operations.js +7 -1
  8. package/dist/document-stories.d.ts +10 -0
  9. package/dist/document-stories.js +27 -0
  10. package/dist/docx/corePropertiesParser.d.ts +8 -0
  11. package/dist/docx/corePropertiesParser.js +53 -0
  12. package/dist/docx/index.d.ts +2 -1
  13. package/dist/docx/index.js +2 -1
  14. package/dist/docx/metadataPrivacy.d.ts +40 -0
  15. package/dist/docx/metadataPrivacy.js +131 -0
  16. package/dist/docx/parser.js +4 -1
  17. package/dist/docx/rezip.d.ts +5 -4
  18. package/dist/docx/rezip.js +6 -8
  19. package/dist/layout-bridge/convert/toFlowBlocks.js +1 -0
  20. package/dist/layout-engine/measure/cache.js +2 -7
  21. package/dist/layout-engine/measure/effectiveLineBreakPolicy.d.ts +29 -0
  22. package/dist/layout-engine/measure/effectiveLineBreakPolicy.js +60 -0
  23. package/dist/layout-engine/measure/measureParagraph.js +32 -36
  24. package/dist/layout-engine/measure/tableCellFloating.js +39 -33
  25. package/dist/layout-engine/types.d.ts +5 -3
  26. package/dist/prosemirror/attrs/index.js +1 -0
  27. package/dist/prosemirror/conversion/effectiveTableCellFormatting.d.ts +62 -0
  28. package/dist/prosemirror/conversion/effectiveTableCellFormatting.js +131 -0
  29. package/dist/prosemirror/conversion/fromProseDoc.js +1 -0
  30. package/dist/prosemirror/conversion/toProseDoc.js +40 -68
  31. package/dist/prosemirror/extensions/core/ParagraphExtension.js +1 -1
  32. package/dist/prosemirror/extensions/marks/HighlightExtension.js +1 -1
  33. package/dist/prosemirror/extensions/marks/RunShadingExtension.js +1 -1
  34. package/dist/prosemirror/extensions/marks/TextColorExtension.js +1 -1
  35. package/dist/prosemirror/extensions/nodes/ImageExtension.js +1 -0
  36. package/dist/prosemirror/extensions/nodes/TableExtension.js +1 -1
  37. package/dist/prosemirror/schema/nodes.d.ts +2 -1
  38. package/dist/redline.d.ts +26 -11
  39. package/dist/redline.js +95 -62
  40. package/dist/server.d.ts +5 -4
  41. package/dist/server.js +5 -4
  42. package/dist/version-comparison.d.ts +65 -11
  43. package/dist/version-comparison.js +187 -29
  44. package/package.json +1 -1
@@ -1,6 +1,9 @@
1
1
  import { getFolioParaIdFromBlockId } from "./types/block-id.js";
2
2
  import { diffWordSegments } from "./ai-edits/word-diff.js";
3
+ import { pairFolioDocumentStories } from "./document-stories.js";
3
4
  import { FolioDocxReviewer } from "./ai-edits/headless.js";
5
+ import { FOLIO_DOCUMENT_METADATA_PROPERTIES, FOLIO_DOCUMENT_PRIVACY_TRANSFORMS, PRIVATE_METADATA_PROPERTIES_BY_TRANSFORM, isFolioDocumentPrivacyTransform } from "./docx/metadataPrivacy.js";
6
+ import { TaggedError, panic } from "better-result";
4
7
  //#region src/version-comparison.ts
5
8
  /**
6
9
  * Document version-diff engine: compare two `.docx` buffers block by block
@@ -76,6 +79,16 @@ import { FolioDocxReviewer } from "./ai-edits/headless.js";
76
79
  * content that makes the preview texts disagree, detection backs off to
77
80
  * `unchanged` rather than misattribute properties.
78
81
  */
82
+ /** Independently selectable comparison scopes. */
83
+ const FOLIO_VERSION_COMPARISON_SCOPES = Object.freeze([
84
+ "text",
85
+ "formatting",
86
+ "metadata"
87
+ ]);
88
+ const isFolioVersionComparisonScope = (value) => FOLIO_VERSION_COMPARISON_SCOPES.some((scope) => scope === value);
89
+ var InvalidFolioVersionComparisonOptionsError = class extends TaggedError("InvalidFolioVersionComparisonOptionsError")() {};
90
+ const FOLIO_VERSION_COMPARISON_PRIVACY_TRANSFORMS = FOLIO_DOCUMENT_PRIVACY_TRANSFORMS;
91
+ const isFolioVersionComparisonPrivacyTransform = (value) => isFolioDocumentPrivacyTransform(value);
79
92
  /** Run-level formatting properties compared for `formatChanged` detection. */
80
93
  const FORMAT_PROPERTIES = [
81
94
  "bold",
@@ -324,7 +337,7 @@ const meetsMoveWordCount = (text) => {
324
337
  * per text, so duplicated boilerplate above the word floor pairs
325
338
  * first-to-first rather than fanning out.
326
339
  */
327
- const detectMoves = (changes, counts) => {
340
+ const detectMoves = (changes, counts, firstMoveGroupId) => {
328
341
  const deletedIndexesByText = /* @__PURE__ */ new Map();
329
342
  changes.forEach((change, index) => {
330
343
  if (change.type === "deleted" && meetsMoveWordCount(change.text)) {
@@ -334,7 +347,7 @@ const detectMoves = (changes, counts) => {
334
347
  }
335
348
  });
336
349
  if (deletedIndexesByText.size === 0) return;
337
- let moveGroupId = 0;
350
+ let moveGroupId = firstMoveGroupId - 1;
338
351
  changes.forEach((change, index) => {
339
352
  if (change.type !== "added") return;
340
353
  const deletedIndex = deletedIndexesByText.get(change.text)?.shift();
@@ -347,53 +360,72 @@ const detectMoves = (changes, counts) => {
347
360
  blockId: deleted.blockId,
348
361
  kind: deleted.kind,
349
362
  text: deleted.text,
350
- moveGroupId
363
+ moveGroupId,
364
+ baseHandle: deleted.baseHandle
351
365
  };
352
366
  changes[index] = {
353
367
  type: "movedTo",
354
368
  blockId: change.blockId,
355
369
  kind: change.kind,
356
370
  text: change.text,
357
- moveGroupId
371
+ moveGroupId,
372
+ revisedHandle: change.revisedHandle
358
373
  };
359
374
  counts.deleted--;
360
375
  counts.added--;
361
376
  counts.moved++;
362
377
  });
363
378
  };
364
- /**
365
- * Compare two `.docx` buffers and return a structured, block-level diff.
366
- * See the module doc comment for the as-accepted comparison semantics, the
367
- * three-pass alignment algorithm, move detection, and format-only change
368
- * detection.
369
- */
370
- const compareDocxVersions = async (base, revised) => {
371
- const [baseReviewer, revisedReviewer] = await Promise.all([FolioDocxReviewer.fromBuffer(base), FolioDocxReviewer.fromBuffer(revised)]);
372
- const baseBlocks = baseReviewer.snapshot().blocks;
373
- const revisedBlocks = revisedReviewer.snapshot().blocks;
379
+ const createSummaryCounts = () => ({
380
+ added: 0,
381
+ deleted: 0,
382
+ modified: 0,
383
+ formatChanged: 0,
384
+ moved: 0,
385
+ metadataChanged: 0,
386
+ unchanged: 0
387
+ });
388
+ const addSummaryCounts = (target, source) => {
389
+ target.added += source.added;
390
+ target.deleted += source.deleted;
391
+ target.modified += source.modified;
392
+ target.formatChanged += source.formatChanged;
393
+ target.moved += source.moved;
394
+ target.metadataChanged += source.metadataChanged;
395
+ target.unchanged += source.unchanged;
396
+ };
397
+ const compareStoryBlocks = ({ baseStory, revisedStory, baseBlocks, revisedBlocks, firstMoveGroupId, includeText, includeFormatting }) => {
374
398
  const changes = [];
375
- const counts = {
376
- added: 0,
377
- deleted: 0,
378
- modified: 0,
379
- formatChanged: 0,
380
- moved: 0,
381
- unchanged: 0
382
- };
399
+ const counts = createSummaryCounts();
383
400
  for (const event of alignFolioBlocks(baseBlocks, revisedBlocks)) {
384
401
  if (event.type === "pair") {
402
+ if (!baseStory || !revisedStory) panic("A paired comparison event requires both story handles");
385
403
  const { baseBlock, revisedBlock } = event;
404
+ const baseHandle = {
405
+ story: baseStory,
406
+ blockId: baseBlock.id
407
+ };
408
+ const revisedHandle = {
409
+ story: revisedStory,
410
+ blockId: revisedBlock.id
411
+ };
386
412
  if (baseBlock.text !== revisedBlock.text) {
413
+ if (!includeText) {
414
+ counts.unchanged++;
415
+ continue;
416
+ }
387
417
  counts.modified++;
388
418
  changes.push({
389
419
  type: "modified",
390
420
  blockId: revisedBlock.id,
391
421
  kind: revisedBlock.kind,
392
- segments: diffWordSegments(baseBlock.text, revisedBlock.text)
422
+ segments: diffWordSegments(baseBlock.text, revisedBlock.text),
423
+ baseHandle,
424
+ revisedHandle
393
425
  });
394
426
  continue;
395
427
  }
396
- const changedProperties = diffPreviewRunFormatting(baseBlock, revisedBlock);
428
+ const changedProperties = includeFormatting ? diffPreviewRunFormatting(baseBlock, revisedBlock) : [];
397
429
  if (changedProperties.length > 0) {
398
430
  counts.formatChanged++;
399
431
  changes.push({
@@ -401,7 +433,9 @@ const compareDocxVersions = async (base, revised) => {
401
433
  blockId: revisedBlock.id,
402
434
  kind: revisedBlock.kind,
403
435
  text: revisedBlock.text,
404
- changedProperties
436
+ changedProperties,
437
+ baseHandle,
438
+ revisedHandle
405
439
  });
406
440
  continue;
407
441
  }
@@ -409,28 +443,152 @@ const compareDocxVersions = async (base, revised) => {
409
443
  continue;
410
444
  }
411
445
  if (event.type === "baseOnly") {
446
+ if (!includeText) continue;
447
+ if (!baseStory) panic("A base-only comparison event requires a base story handle");
412
448
  counts.deleted++;
413
449
  changes.push({
414
450
  type: "deleted",
415
451
  blockId: event.block.id,
416
452
  kind: event.block.kind,
417
- text: event.block.text
453
+ text: event.block.text,
454
+ baseHandle: {
455
+ story: baseStory,
456
+ blockId: event.block.id
457
+ }
418
458
  });
419
459
  continue;
420
460
  }
461
+ if (!includeText) continue;
462
+ if (!revisedStory) panic("A revised-only comparison event requires a revised story handle");
421
463
  counts.added++;
422
464
  changes.push({
423
465
  type: "added",
424
466
  blockId: event.block.id,
425
467
  kind: event.block.kind,
426
- text: event.block.text
468
+ text: event.block.text,
469
+ revisedHandle: {
470
+ story: revisedStory,
471
+ blockId: event.block.id
472
+ }
427
473
  });
428
474
  }
429
- detectMoves(changes, counts);
475
+ detectMoves(changes, counts, firstMoveGroupId);
476
+ return {
477
+ baseStory,
478
+ revisedStory,
479
+ changes,
480
+ summaryCounts: counts
481
+ };
482
+ };
483
+ const DEFAULT_COMPARISON_SCOPES = Object.freeze(["text", "formatting"]);
484
+ const resolveComparisonScopes = (options) => {
485
+ const include = options.include ?? DEFAULT_COMPARISON_SCOPES;
486
+ if (include.length === 0 || include.some((scope) => !isFolioVersionComparisonScope(scope))) throw new InvalidFolioVersionComparisonOptionsError({
487
+ message: "Version comparison requires at least one recognized scope",
488
+ option: "include",
489
+ receivedValue: include
490
+ });
491
+ return new Set(include);
492
+ };
493
+ const resolvePrivacyTransforms = (transforms) => {
494
+ if (!Array.isArray(transforms) || transforms.some((transform) => !isFolioVersionComparisonPrivacyTransform(transform))) throw new InvalidFolioVersionComparisonOptionsError({
495
+ message: "Version comparison received an unrecognized privacy transform",
496
+ option: "privacy.transforms",
497
+ receivedValue: transforms
498
+ });
499
+ const requested = new Set(transforms);
500
+ return FOLIO_VERSION_COMPARISON_PRIVACY_TRANSFORMS.filter((transform) => requested.has(transform));
501
+ };
502
+ /** Apply auditable, output-only privacy transforms to a structured version diff. */
503
+ const applyFolioVersionDiffPrivacy = (diff, options) => {
504
+ const requestedTransforms = resolvePrivacyTransforms(options.transforms);
505
+ const appliedTransformSet = /* @__PURE__ */ new Set([...diff.privacyReport.appliedTransforms, ...requestedTransforms]);
506
+ const appliedTransforms = FOLIO_VERSION_COMPARISON_PRIVACY_TRANSFORMS.filter((transform) => appliedTransformSet.has(transform));
507
+ const removedPropertySet = /* @__PURE__ */ new Set();
508
+ for (const transform of appliedTransforms) for (const property of PRIVATE_METADATA_PROPERTIES_BY_TRANSFORM[transform]) removedPropertySet.add(property);
509
+ const actuallyRemovedPropertySet = /* @__PURE__ */ new Set([...diff.privacyReport.removedMetadataProperties, ...diff.metadataChanges.filter(({ property }) => removedPropertySet.has(property)).map(({ property }) => property)]);
510
+ const removedMetadataProperties = FOLIO_DOCUMENT_METADATA_PROPERTIES.filter((property) => actuallyRemovedPropertySet.has(property));
511
+ const metadataChanges = diff.metadataChanges.filter(({ property }) => !removedPropertySet.has(property));
430
512
  return {
513
+ ...diff,
514
+ metadataChanges,
515
+ privacyReport: {
516
+ appliedTransforms,
517
+ removedMetadataProperties
518
+ },
519
+ summaryCounts: {
520
+ ...diff.summaryCounts,
521
+ metadataChanged: metadataChanges.length
522
+ }
523
+ };
524
+ };
525
+ const normalizeMetadataValue = (properties, property) => {
526
+ const value = properties?.[property];
527
+ return value instanceof Date ? value.toISOString() : value ?? null;
528
+ };
529
+ const compareMetadata = (base, revised) => {
530
+ const changes = [];
531
+ for (const property of FOLIO_DOCUMENT_METADATA_PROPERTIES) {
532
+ const baseValue = normalizeMetadataValue(base, property);
533
+ const revisedValue = normalizeMetadataValue(revised, property);
534
+ if (baseValue !== revisedValue) changes.push({
535
+ property,
536
+ baseValue,
537
+ revisedValue
538
+ });
539
+ }
540
+ return changes;
541
+ };
542
+ /**
543
+ * Compare two `.docx` buffers and return a structured, block-level diff.
544
+ * See the module doc comment for the as-accepted comparison semantics, the
545
+ * three-pass alignment algorithm, move detection, and format-only change
546
+ * detection.
547
+ */
548
+ const compareDocxVersions = async (base, revised, options = {}) => {
549
+ const scopes = resolveComparisonScopes(options);
550
+ const [baseReviewer, revisedReviewer] = await Promise.all([FolioDocxReviewer.fromBuffer(base), FolioDocxReviewer.fromBuffer(revised)]);
551
+ const changes = [];
552
+ const stories = [];
553
+ const counts = createSummaryCounts();
554
+ const baseStories = baseReviewer.listStories().map(({ handle }) => handle);
555
+ const revisedStories = revisedReviewer.listStories().map(({ handle }) => handle);
556
+ let nextMoveGroupId = 1;
557
+ for (const pair of pairFolioDocumentStories(baseStories, revisedStories)) {
558
+ const baseBlocks = pair.baseStory ? baseReviewer.readReviewedStory({
559
+ story: pair.baseStory,
560
+ view: "final"
561
+ })?.snapshot.blocks ?? [] : [];
562
+ const revisedBlocks = pair.revisedStory ? revisedReviewer.readReviewedStory({
563
+ story: pair.revisedStory,
564
+ view: "final"
565
+ })?.snapshot.blocks ?? [] : [];
566
+ const storyDiff = compareStoryBlocks({
567
+ ...pair,
568
+ baseBlocks,
569
+ revisedBlocks,
570
+ firstMoveGroupId: nextMoveGroupId,
571
+ includeText: scopes.has("text"),
572
+ includeFormatting: scopes.has("formatting")
573
+ });
574
+ stories.push(storyDiff);
575
+ for (const change of storyDiff.changes) changes.push(change);
576
+ addSummaryCounts(counts, storyDiff.summaryCounts);
577
+ nextMoveGroupId += storyDiff.summaryCounts.moved;
578
+ }
579
+ const metadataChanges = scopes.has("metadata") ? compareMetadata(baseReviewer.getDocumentProperties(), revisedReviewer.getDocumentProperties()) : [];
580
+ counts.metadataChanged = metadataChanges.length;
581
+ const diff = {
431
582
  changes,
583
+ stories,
584
+ metadataChanges,
585
+ privacyReport: {
586
+ appliedTransforms: [],
587
+ removedMetadataProperties: []
588
+ },
432
589
  summaryCounts: counts
433
590
  };
591
+ return options.privacy ? applyFolioVersionDiffPrivacy(diff, options.privacy) : diff;
434
592
  };
435
593
  //#endregion
436
- export { alignFolioBlocks, compareDocxVersions, exceedsLcsBudget };
594
+ export { FOLIO_DOCUMENT_METADATA_PROPERTIES, FOLIO_VERSION_COMPARISON_PRIVACY_TRANSFORMS, FOLIO_VERSION_COMPARISON_SCOPES, InvalidFolioVersionComparisonOptionsError, alignFolioBlocks, applyFolioVersionDiffPrivacy, compareDocxVersions, exceedsLcsBudget, isFolioVersionComparisonPrivacyTransform, isFolioVersionComparisonScope };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stll/folio-core",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Headless, framework-neutral core of folio: the OOXML (.docx) parser, document model, ProseMirror integration, and page-layout engine. No React.",
5
5
  "keywords": [
6
6
  "document-model",