-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathutilities.ts
More file actions
1548 lines (1433 loc) · 52.4 KB
/
utilities.ts
File metadata and controls
1548 lines (1433 loc) · 52.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Shared utility functions for PM adapter
*
* Contains type guards, normalization, conversion, and position mapping utilities
* used across multiple adapter modules.
*/
import type {
BoxSpacing,
CustomGeometry,
DrawingBlock,
DrawingContentSnapshot,
ImageBlock,
ShapeGroupChild,
ShapeGroupDrawing,
ShapeGroupImageChild,
ShapeGroupTransform,
FlowBlock,
ImageRun,
ParagraphBlock,
Run,
TableBlock,
} from '@superdoc/contracts';
import type { PMNode, PositionMap, BlockIdGenerator } from './types.js';
import { TWIPS_PER_INCH, PX_PER_INCH, PX_PER_PT, ATOMIC_INLINE_TYPES } from './constants.js';
export type LineEnd = {
type?: string;
width?: string;
length?: string;
};
export type LineEnds = {
head?: LineEnd;
tail?: LineEnd;
};
export type EffectExtent = {
left: number;
top: number;
right: number;
bottom: number;
};
// ============================================================================
// Unit Conversion Utilities
// ============================================================================
/**
* Converts a value from twips to pixels.
*
* Twips (twentieth of a point) are a common unit in document formats like DOCX.
* This function converts them to pixels using standard conversion ratios.
*
* @param value - The value in twips to convert
* @returns The equivalent value in pixels
*
* @example
* ```typescript
* const pixels = twipsToPx(1440); // 96px (1 inch at 96 DPI)
* ```
*/
export const twipsToPx = (value: number): number => (value / TWIPS_PER_INCH) * PX_PER_INCH;
/**
* Converts a value from points to pixels.
*
* @param pt - The value in points to convert (optional, nullable)
* @returns The equivalent value in pixels, or undefined if input is null/undefined/not finite
*
* @example
* ```typescript
* const pixels = ptToPx(12); // 16px (12pt font at 96 DPI)
* ptToPx(null); // undefined
* ptToPx(NaN); // undefined
* ```
*/
export const ptToPx = (pt?: number | null): number | undefined => {
if (pt == null || !Number.isFinite(pt)) return undefined;
return pt * PX_PER_PT;
};
// ============================================================================
// Type Guards
// ============================================================================
/**
* Type guard to check if a value is a finite number.
*
* Ensures the value is of type 'number' and is not NaN, Infinity, or -Infinity.
*
* @param value - The value to check
* @returns True if the value is a finite number, false otherwise
*
* @example
* ```typescript
* isFiniteNumber(42); // true
* isFiniteNumber(3.14); // true
* isFiniteNumber(NaN); // false
* isFiniteNumber(Infinity); // false
* isFiniteNumber("42"); // false
* isFiniteNumber(null); // false
* ```
*/
export const isFiniteNumber = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value);
/**
* Type guard to check if a value is a plain object.
*
* A plain object is a non-null, non-array object that can be indexed by string keys.
* This includes class instances (like Date, RegExp, etc.) - not just POJOs.
*
* @param value - The value to check
* @returns True if the value is a plain object, false otherwise
*
* @example
* ```typescript
* isPlainObject({ key: 'value' }); // true
* isPlainObject({}); // true
* isPlainObject([]); // false
* isPlainObject(null); // false
* isPlainObject("string"); // false
* isPlainObject(new Date()); // true (class instances are considered objects)
* isPlainObject(new Map()); // true (any object that's not an array)
* ```
*/
export const isPlainObject = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === 'object' && !Array.isArray(value);
// ============================================================================
// Normalization / Coercion Functions
// ============================================================================
/**
* Normalizes a prefix string, ensuring it's a valid string.
*
* @param value - The prefix value to normalize (optional)
* @returns Empty string if value is falsy, otherwise the string representation of the value
*
* @example
* ```typescript
* normalizePrefix("abc"); // "abc"
* normalizePrefix(""); // ""
* normalizePrefix(undefined); // ""
* normalizePrefix(null); // ""
* ```
*/
export const normalizePrefix = (value?: string): string => {
if (!value) return '';
return String(value);
};
/**
* Attempts to extract a numeric value from unknown input.
*
* If the value is already a finite number, returns it. If it's a string,
* attempts to parse it as a float.
*
* @param value - The value to extract a number from
* @returns The numeric value, or undefined if conversion is not possible
*
* @example
* ```typescript
* pickNumber(42); // 42
* pickNumber("3.14"); // 3.14
* pickNumber("invalid"); // undefined (NaN result)
* pickNumber(true); // undefined
* pickNumber(null); // undefined
* ```
*/
export const pickNumber = (value: unknown): number | undefined => {
if (isFiniteNumber(value)) return value;
if (typeof value === 'string') {
const parsed = parseFloat(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
return undefined;
};
/**
* Normalizes a color string, ensuring it has a leading '#' symbol.
*
* Filters out special values like 'auto' and 'none'. Prepends '#' if not present.
*
* @param value - The color value to normalize
* @returns The normalized color string with '#' prefix, or undefined if invalid/special
*
* @example
* ```typescript
* normalizeColor("FF0000"); // "#FF0000"
* normalizeColor("#00FF00"); // "#00FF00"
* normalizeColor("auto"); // undefined
* normalizeColor("none"); // undefined
* normalizeColor(""); // undefined
* normalizeColor(123); // undefined
* ```
*/
export const normalizeColor = (value: unknown): string | undefined => {
if (typeof value !== 'string') return undefined;
const trimmed = value.trim();
if (!trimmed || trimmed === 'auto' || trimmed === 'none') return undefined;
return trimmed.startsWith('#') ? trimmed : `#${trimmed}`;
};
/**
* Normalizes a string by trimming whitespace.
*
* Returns undefined for non-strings or empty/whitespace-only strings.
*
* @param value - The string value to normalize
* @returns The trimmed string, or undefined if empty or not a string
*
* @example
* ```typescript
* normalizeString(" hello "); // "hello"
* normalizeString(""); // undefined
* normalizeString(" "); // undefined
* normalizeString(123); // undefined
* normalizeString(null); // undefined
* ```
*/
export const normalizeString = (value: unknown): string | undefined => {
if (typeof value !== 'string') return undefined;
const trimmed = value.trim();
return trimmed || undefined;
};
/**
* Coerces a value to a number if possible.
*
* Accepts numbers and numeric strings. Returns undefined for invalid inputs.
*
* @param value - The value to coerce to a number
* @returns The numeric value, or undefined if coercion fails
*
* @example
* ```typescript
* coerceNumber(42); // 42
* coerceNumber("3.14"); // 3.14
* coerceNumber(" 100 "); // 100
* coerceNumber("invalid"); // undefined
* coerceNumber(""); // undefined
* coerceNumber(true); // undefined
* coerceNumber(NaN); // undefined
* ```
*/
export function coerceNumber(value: unknown): number | undefined {
if (isFiniteNumber(value)) return Number(value);
if (typeof value === 'string' && value.trim() !== '') {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
return undefined;
}
/**
* Coerces a value to a positive number, with a fallback.
*
* Returns the coerced value if it's a positive number, otherwise returns the fallback.
* Validates that the fallback itself is a positive number.
*
* @param value - The value to coerce to a positive number
* @param fallback - The fallback value to use if coercion fails (must be positive)
* @returns The coerced positive number or the fallback
* @throws {Error} If the fallback is not a positive finite number
*
* @example
* ```typescript
* coercePositiveNumber(10, 5); // 10
* coercePositiveNumber("15", 5); // 15
* coercePositiveNumber(0, 5); // 5 (not positive)
* coercePositiveNumber(-10, 5); // 5 (not positive)
* coercePositiveNumber("invalid", 5); // 5
* coercePositiveNumber(10, -5); // throws Error
* coercePositiveNumber(10, 0); // throws Error
* ```
*/
export function coercePositiveNumber(value: unknown, fallback: number): number {
if (!isFiniteNumber(fallback) || fallback <= 0) {
throw new Error(`coercePositiveNumber: fallback must be a positive number, got ${fallback}`);
}
const numeric = coerceNumber(value);
if (numeric != null && numeric > 0) {
return numeric;
}
return fallback;
}
/**
* Coerces a value to a boolean with comprehensive string parsing.
*
* This is the most comprehensive boolean coercion function, supporting multiple
* string formats including 'yes'/'no' and 'on'/'off'. Use this when you need
* maximum flexibility in accepting boolean-like values from external sources.
*
* Recognized truthy strings: 'true', '1', 'yes', 'on'
* Recognized falsy strings: 'false', '0', 'no', 'off'
*
* @param value - The value to coerce to a boolean
* @returns Boolean value, or undefined if the value cannot be interpreted as boolean
*
* @example
* ```typescript
* coerceBoolean(true); // true
* coerceBoolean(1); // true
* coerceBoolean("yes"); // true
* coerceBoolean("on"); // true
* coerceBoolean(false); // false
* coerceBoolean(0); // false
* coerceBoolean("no"); // false
* coerceBoolean("off"); // false
* coerceBoolean(2); // undefined
* coerceBoolean("maybe"); // undefined
* ```
*/
export function coerceBoolean(value: unknown): boolean | undefined {
if (typeof value === 'boolean') return value;
if (typeof value === 'number') {
if (value === 1) return true;
if (value === 0) return false;
return undefined;
}
if (typeof value === 'string') {
const normalized = value.trim().toLowerCase();
if (['true', '1', 'yes', 'on'].includes(normalized)) return true;
if (['false', '0', 'no', 'off'].includes(normalized)) return false;
}
return undefined;
}
/**
* Coerces a value to a boolean with basic string parsing.
*
* This is a simpler boolean coercion function that only recognizes 'true'/'false'
* and '1'/'0' strings. Use this when you have more controlled input and don't need
* to support 'yes'/'no' or 'on'/'off' variations.
*
* Note: Unlike coerceBoolean, this does NOT support 'yes'/'no' or 'on'/'off'.
*
* Recognized truthy strings: 'true', '1'
* Recognized falsy strings: 'false', '0'
*
* @param value - The value to coerce to a boolean
* @returns Boolean value, or undefined if the value cannot be interpreted as boolean
*
* @example
* ```typescript
* toBoolean(true); // true
* toBoolean(1); // true
* toBoolean("true"); // true
* toBoolean("1"); // true
* toBoolean(false); // false
* toBoolean(0); // false
* toBoolean("false"); // false
* toBoolean("0"); // false
* toBoolean("yes"); // undefined (not supported)
* toBoolean("on"); // undefined (not supported)
* toBoolean(2); // undefined
* ```
*/
export const toBoolean = (value: unknown): boolean | undefined => {
if (typeof value === 'boolean') return value;
if (typeof value === 'string') {
const v = value.trim().toLowerCase();
if (v === 'true' || v === '1') return true;
if (v === 'false' || v === '0') return false;
}
if (typeof value === 'number') {
if (value === 1) return true;
if (value === 0) return false;
}
return undefined;
};
// ============================================================================
// Box Spacing Utilities
// ============================================================================
/**
* Converts a spacing object to a BoxSpacing type with validated numeric values.
*
* Extracts top, right, bottom, and left spacing values, keeping only those that
* are finite numbers. Returns undefined if no valid spacing values exist.
*
* @param spacing - Object potentially containing spacing values
* @returns BoxSpacing object with validated numeric values, or undefined if no valid values
*
* @example
* ```typescript
* toBoxSpacing({ top: 10, right: 20, bottom: 10, left: 20 });
* // { top: 10, right: 20, bottom: 10, left: 20 }
*
* toBoxSpacing({ top: 10, right: "invalid" });
* // { top: 10 }
*
* toBoxSpacing({ invalid: 10 });
* // undefined (no recognized spacing properties)
*
* toBoxSpacing(null);
* // undefined
* ```
*/
export function toBoxSpacing(spacing?: Record<string, unknown>): BoxSpacing | undefined {
if (!spacing) {
return undefined;
}
const result: BoxSpacing = {};
(['top', 'right', 'bottom', 'left'] as const).forEach((side) => {
const value = spacing[side];
if (isFiniteNumber(value)) {
result[side] = Number(value);
}
});
return Object.keys(result).length > 0 ? result : undefined;
}
// ============================================================================
// Position Map Building
// ============================================================================
/**
* Builds a position map for ProseMirror nodes, tracking start/end positions.
*
* This function recursively traverses a ProseMirror node tree and calculates the
* absolute position (offset from document start) for each node. Text nodes are
* sized by character count, atomic inline nodes (like images) take 1 position,
* and block nodes add opening/closing positions (except for the root 'doc' node).
*
* The resulting WeakMap allows O(1) lookup of any node's position range without
* storing references that would prevent garbage collection.
*
* @param root - The root ProseMirror node to build position map from
* @param options - Optional atom node type metadata for schema-aware position sizing
* @returns A WeakMap mapping each node to its { start, end } position range
*
* @example
* ```typescript
* const doc = {
* type: 'doc',
* content: [
* { type: 'paragraph', content: [{ type: 'text', text: 'Hello' }] }
* ]
* };
* const map = buildPositionMap(doc, { atomNodeTypes: ['customAtom'] });
* const paragraph = doc.content[0];
* map.get(paragraph); // { start: 0, end: 7 } (1 open + 5 text + 1 close)
* ```
*/
type BuildPositionMapOptions = {
atomNodeTypes?: Iterable<string>;
};
export const buildPositionMap = (root: PMNode, options?: BuildPositionMapOptions): PositionMap => {
const map: PositionMap = new WeakMap();
const atomNodeTypes = new Set(ATOMIC_INLINE_TYPES);
if (options?.atomNodeTypes) {
for (const nodeType of options.atomNodeTypes) {
if (typeof nodeType === 'string' && nodeType.length > 0) {
atomNodeTypes.add(nodeType);
}
}
}
const visit = (node: PMNode, pos: number): number => {
if (node.type === 'text') {
const size = node.text?.length ?? 0;
const end = pos + size;
map.set(node, { start: pos, end });
return end;
}
if (atomNodeTypes.has(node.type)) {
const end = pos + 1;
map.set(node, { start: pos, end });
return end;
}
const open = node.type === 'doc' ? 0 : 1;
const close = node.type === 'doc' ? 0 : 1;
let nextPos = pos + open;
const content = Array.isArray(node.content) ? node.content : [];
map.set(node, { start: pos, end: pos }); // placeholder, end updated after children
content.forEach((child) => {
nextPos = visit(child, nextPos);
});
const end = nextPos + close;
map.set(node, { start: pos, end });
return end;
};
visit(root, 0);
return map;
};
// ============================================================================
// Block ID Generation
// ============================================================================
/**
* Creates a block ID generator function with sequential numbering.
*
* Returns a closure that generates unique block IDs by combining an optional prefix,
* an auto-incrementing counter, and a kind identifier. This ensures stable, predictable
* IDs during document transformation.
*
* @param prefix - Optional prefix to prepend to all generated IDs (defaults to empty string)
* @returns A generator function that takes a kind string and returns a unique ID
*
* @example
* ```typescript
* const genId = createBlockIdGenerator('doc-');
* genId('paragraph'); // 'doc-0-paragraph'
* genId('paragraph'); // 'doc-1-paragraph'
* genId('image'); // 'doc-2-image'
*
* const genIdNoPrefix = createBlockIdGenerator();
* genIdNoPrefix('heading'); // '0-heading'
* genIdNoPrefix('heading'); // '1-heading'
* ```
*/
export const createBlockIdGenerator = (prefix: string = ''): BlockIdGenerator => {
let counter = 0;
return (kind: string) => `${prefix}${counter++}-${kind}`;
};
// ============================================================================
// Drawing/Shape Utilities
// ============================================================================
/**
* Converts an unknown value to a validated DrawingContentSnapshot.
*
* Validates that the value has a string 'name' property and optionally
* includes 'attributes' (as a plain object) and 'elements' (as an array of objects).
* Performs validation on array contents to ensure they are objects.
*
* @param value - The value to convert to a DrawingContentSnapshot
* @returns A validated DrawingContentSnapshot, or undefined if validation fails
*
* @example
* ```typescript
* toDrawingContentSnapshot({ name: 'rect' });
* // { name: 'rect' }
*
* toDrawingContentSnapshot({
* name: 'group',
* attributes: { fill: 'red' },
* elements: [{ type: 'circle' }]
* });
* // { name: 'group', attributes: { fill: 'red' }, elements: [{ type: 'circle' }] }
*
* toDrawingContentSnapshot({ name: 'rect', elements: [null, { valid: true }] });
* // { name: 'rect', elements: [{ valid: true }] } (null filtered out)
*
* toDrawingContentSnapshot('invalid');
* // undefined
* ```
*/
export function toDrawingContentSnapshot(value: unknown): DrawingContentSnapshot | undefined {
if (!value || typeof value !== 'object') return undefined;
const raw = value as Record<string, unknown>;
const name = raw.name;
if (typeof name !== 'string') return undefined;
const snapshot: DrawingContentSnapshot = { name };
// Validate attributes is a plain object (not an array)
if (raw.attributes && typeof raw.attributes === 'object' && !Array.isArray(raw.attributes)) {
snapshot.attributes = { ...(raw.attributes as Record<string, unknown>) };
}
// Validate elements array contents
if (Array.isArray(raw.elements)) {
const validElements = raw.elements.filter(
(el): el is Record<string, unknown> => el != null && typeof el === 'object',
);
if (validElements.length > 0) {
snapshot.elements = validElements;
}
}
return snapshot;
}
/**
* Type guard to check if a value is a ShapeGroupTransform.
*
* A valid ShapeGroupTransform must have at least one finite numeric property
* among: x, y, width, height, childWidth, childHeight, childX, childY.
*
* @param value - The value to check
* @returns True if the value has at least one valid transform property
*
* @example
* ```typescript
* isShapeGroupTransform({ x: 10, y: 20 }); // true
* isShapeGroupTransform({ width: 100 }); // true
* isShapeGroupTransform({ childX: 5, childY: 10 }); // true
* isShapeGroupTransform({}); // false
* isShapeGroupTransform({ invalid: 10 }); // false
* isShapeGroupTransform(null); // false
* ```
*/
export function isShapeGroupTransform(value: unknown): value is ShapeGroupTransform {
if (!value || typeof value !== 'object') return false;
const maybe = value as Record<string, unknown>;
return (
isFiniteNumber(maybe.x) ||
isFiniteNumber(maybe.y) ||
isFiniteNumber(maybe.width) ||
isFiniteNumber(maybe.height) ||
isFiniteNumber(maybe.childWidth) ||
isFiniteNumber(maybe.childHeight) ||
isFiniteNumber(maybe.childX) ||
isFiniteNumber(maybe.childY)
);
}
/**
* Normalizes a shape size object, extracting width and height properties.
*
* Coerces width and height to numbers if possible. Returns undefined if both
* properties are missing or invalid.
*
* @param value - Object potentially containing width and height
* @returns Object with validated width/height, or undefined if none are valid
*
* @example
* ```typescript
* normalizeShapeSize({ width: 100, height: 50 });
* // { width: 100, height: 50 }
*
* normalizeShapeSize({ width: "200", height: 100 });
* // { width: 200, height: 100 }
*
* normalizeShapeSize({ width: 100 });
* // { width: 100 }
*
* normalizeShapeSize({ invalid: 100 });
* // undefined
*
* normalizeShapeSize(null);
* // undefined
* ```
*/
export function normalizeShapeSize(value: unknown): { width?: number; height?: number } | undefined {
if (!value || typeof value !== 'object') return undefined;
const maybe = value as Record<string, unknown>;
const width = coerceNumber(maybe.width);
const height = coerceNumber(maybe.height);
if (width == null && height == null) {
return undefined;
}
const result: { width?: number; height?: number } = {};
if (width != null) result.width = width;
if (height != null) result.height = height;
return result;
}
/** Valid size values for line end markers (sm, med, lg) */
const LINE_END_SIZES = new Set(['sm', 'med', 'lg']);
/**
* Normalizes a single line end configuration from an unknown value.
*
* @param value - The value to normalize
* @returns A validated LineEnd object, or undefined if invalid or type is 'none'
*/
const normalizeLineEnd = (value: unknown): LineEnd | undefined => {
if (!value || typeof value !== 'object') return undefined;
const maybe = value as Record<string, unknown>;
const type = typeof maybe.type === 'string' ? maybe.type : undefined;
if (!type || type === 'none') return undefined;
const width = typeof maybe.width === 'string' && LINE_END_SIZES.has(maybe.width) ? maybe.width : undefined;
const length = typeof maybe.length === 'string' && LINE_END_SIZES.has(maybe.length) ? maybe.length : undefined;
return { type, width, length };
};
/**
* Normalizes line end markers (arrowheads) configuration from an unknown value.
*
* Validates and extracts head and tail line end configurations.
* Returns undefined if input is invalid or neither head nor tail is present.
*
* @param value - Value to normalize (expected to have head/tail properties)
* @returns A validated LineEnds object, or undefined if invalid
*
* @example
* ```typescript
* normalizeLineEnds({ head: { type: 'triangle', width: 'sm' } });
* // { head: { type: 'triangle', width: 'sm' } }
*
* normalizeLineEnds({ tail: { type: 'none' } });
* // undefined (type 'none' is filtered out)
*
* normalizeLineEnds(null);
* // undefined
* ```
*/
export function normalizeLineEnds(value: unknown): LineEnds | undefined {
if (!value || typeof value !== 'object') return undefined;
const maybe = value as Record<string, unknown>;
const head = normalizeLineEnd(maybe.head);
const tail = normalizeLineEnd(maybe.tail);
if (!head && !tail) return undefined;
return { head, tail };
}
/**
* Normalizes effect extent values from an unknown value.
*
* Effect extents define additional space around a shape for effects like shadows
* or arrowheads. Negative values are clamped to 0.
*
* @param value - Value to normalize (expected to have left/top/right/bottom properties)
* @returns A validated EffectExtent object, or undefined if all values are null/undefined
*
* @example
* ```typescript
* normalizeEffectExtent({ left: 10, top: 5, right: 10, bottom: 5 });
* // { left: 10, top: 5, right: 10, bottom: 5 }
*
* normalizeEffectExtent({ left: -5, right: 10 });
* // { left: 0, top: 0, right: 10, bottom: 0 }
*
* normalizeEffectExtent(null);
* // undefined
* ```
*/
export function normalizeEffectExtent(value: unknown): EffectExtent | undefined {
if (!value || typeof value !== 'object') return undefined;
const maybe = value as Record<string, unknown>;
const left = coerceNumber(maybe.left);
const top = coerceNumber(maybe.top);
const right = coerceNumber(maybe.right);
const bottom = coerceNumber(maybe.bottom);
if (left == null && top == null && right == null && bottom == null) {
return undefined;
}
const clamp = (val: number | null | undefined) => (val != null && val > 0 ? val : 0);
return {
left: clamp(left),
top: clamp(top),
right: clamp(right),
bottom: clamp(bottom),
};
}
/**
* Normalizes and validates shape group children from an array.
*
* Filters out invalid entries, keeping only objects that have a string 'shapeType' property.
* Returns an empty array if input is not an array.
*
* @param value - Value to extract shape group children from
* @returns Array of validated ShapeGroupChild objects (may be empty)
*
* @example
* ```typescript
* normalizeShapeGroupChildren([
* { shapeType: 'rect', x: 0, y: 0 },
* { shapeType: 'circle', cx: 50, cy: 50 }
* ]);
* // [{ shapeType: 'rect', x: 0, y: 0 }, { shapeType: 'circle', cx: 50, cy: 50 }]
*
* normalizeShapeGroupChildren([
* { shapeType: 'rect' },
* null,
* { invalid: true },
* { shapeType: 'line' }
* ]);
* // [{ shapeType: 'rect' }, { shapeType: 'line' }]
*
* normalizeShapeGroupChildren(null);
* // []
*
* normalizeShapeGroupChildren("not an array");
* // []
* ```
*/
export function normalizeShapeGroupChildren(value: unknown): ShapeGroupChild[] {
if (!Array.isArray(value)) return [];
return value.filter((child): child is ShapeGroupChild => {
if (!child || typeof child !== 'object') return false;
return typeof (child as { shapeType?: unknown }).shapeType === 'string';
});
}
/**
* Normalizes a custom geometry value, validating its structure.
* Returns undefined if the value is not a valid CustomGeometry object.
*/
export function normalizeCustomGeometry(value: unknown): CustomGeometry | undefined {
if (!value || typeof value !== 'object') return undefined;
const obj = value as Record<string, unknown>;
if (typeof obj.width !== 'number' || typeof obj.height !== 'number') return undefined;
if (!Array.isArray(obj.paths) || obj.paths.length === 0) return undefined;
const validPaths = obj.paths.filter(
(p: unknown) => p && typeof p === 'object' && typeof (p as Record<string, unknown>).d === 'string',
);
if (validPaths.length === 0) return undefined;
return {
paths: validPaths.map((p: Record<string, unknown>) => ({
d: p.d as string,
fill: typeof p.fill === 'string' ? p.fill : 'norm',
stroke: p.stroke !== false,
})),
width: obj.width,
height: obj.height,
};
}
// ============================================================================
// Media/Image Utilities
// ============================================================================
/**
* Normalizes a media key by removing leading path prefixes and converting to forward slashes.
*
* Converts backslashes to forward slashes, then removes all leading './' and '/' prefixes.
* This ensures consistent path formatting across different file systems and sources.
*
* @param value - The media key/path to normalize (optional)
* @returns The normalized media key, or undefined if no value provided
*
* @example
* ```typescript
* normalizeMediaKey('word/media/image1.jpg'); // 'word/media/image1.jpg'
* normalizeMediaKey('/media/image1.jpg'); // 'media/image1.jpg'
* normalizeMediaKey('./media/image1.jpg'); // 'media/image1.jpg'
* normalizeMediaKey('///media/image1.jpg'); // 'media/image1.jpg'
* normalizeMediaKey('.////media/image1.jpg'); // 'media/image1.jpg'
* normalizeMediaKey('word\\media\\image1.jpg'); // 'word/media/image1.jpg'
* normalizeMediaKey('\\\\media\\image1.jpg'); // 'media/image1.jpg'
* normalizeMediaKey(undefined); // undefined
* ```
*/
export function normalizeMediaKey(value?: string): string | undefined {
if (!value) return undefined;
return value
.replace(/\\/g, '/') // Convert backslashes first
.replace(/^(\.\/|\/)+/, ''); // Remove all leading ./ and /
}
/**
* Infers the file extension from a file path string.
*
* Handles edge cases like hidden files (starting with '.'), trailing dots,
* and paths with multiple directory separators. Only returns valid extensions
* from the filename portion of the path.
*
* @param value - The file path to extract extension from (optional, nullable)
* @returns The lowercase file extension, or undefined if none exists
*
* @example
* ```typescript
* inferExtensionFromPath('image.jpg'); // 'jpg'
* inferExtensionFromPath('document.PDF'); // 'pdf'
* inferExtensionFromPath('path/to/file.png'); // 'png'
* inferExtensionFromPath('path\\to\\file.gif'); // 'gif'
* inferExtensionFromPath('.gitignore'); // undefined (hidden file)
* inferExtensionFromPath('file.'); // undefined (trailing dot)
* inferExtensionFromPath('noextension'); // undefined
* inferExtensionFromPath('file.tar.gz'); // 'gz'
* inferExtensionFromPath(null); // undefined
* inferExtensionFromPath(''); // undefined
* ```
*/
export function inferExtensionFromPath(value?: string | null): string | undefined {
if (!value) return undefined;
// Extract filename only (handle both forward and backward slashes)
const fileName = value.split('/').pop()?.split('\\').pop();
if (!fileName || fileName.startsWith('.')) return undefined; // Hidden file or no filename
const parts = fileName.split('.');
if (parts.length < 2) return undefined; // No extension
const ext = parts.at(-1);
if (!ext || ext.length === 0) return undefined; // Trailing dot
return ext.toLowerCase();
}
/**
* Hydrates image blocks by converting file path references to base64 data URLs.
*
* This function processes multiple types of blocks containing images:
* - **ImageBlocks**: Top-level image blocks with `kind: 'image'`
* - **ParagraphBlocks**: Paragraphs containing ImageRuns (inline images)
* - **DrawingBlocks**: Drawing blocks with `drawingKind === 'shapeGroup'` that contain image children
* - **TableBlocks**: Tables containing cells with any of the above block types
*
* For each image, attempts to resolve the image source by checking multiple
* candidate paths against the provided media files map. Uses path normalization
* and extension inference to maximize match success rate.
*
* **Candidate Path Search Order:**
* 1. Block's `src` property (normalized)
* 2. Block's `attrs.src` if present (normalized)
* 3. `word/media/{rId}.{ext}` if `attrs.rId` exists
* 4. `media/{rId}.{ext}` if `attrs.rId` exists
*
* Extension is inferred from:
* - `attrs.extension` (highest priority)
* - Extension from the src path
* - Default to 'jpeg' if neither available
*
* **Images are left unchanged if:**
* - No media files are provided
* - The src already starts with 'data:' (already a data URL)
* - No matching media file is found in any candidate path
*
* @param blocks - Array of FlowBlocks to process
* @param mediaFiles - Map of file paths to base64-encoded image data (without 'data:' prefix)
* @returns New array of FlowBlocks with image blocks hydrated to data URLs
*
* @example
* ```typescript
* // Hydrating a top-level ImageBlock
* const blocks = [
* { kind: 'image', src: 'word/media/image1.jpg', attrs: { rId: 'rId5' } }
* ];
* const mediaFiles = { 'word/media/image1.jpg': 'iVBORw0KGgoAAAANS...' };
* const hydrated = hydrateImageBlocks(blocks, mediaFiles);
* // Result: [{ kind: 'image', src: 'data:image/jpg;base64,iVBORw0KGgoAAAANS...' }]
* ```
*
* @example
* ```typescript
* // Hydrating a DrawingBlock with shapeGroup containing image children
* const blocks = [
* {
* kind: 'drawing',
* drawingKind: 'shapeGroup',
* shapes: [
* { shapeType: 'image', attrs: { src: 'word/media/img.png', x: 0, y: 0 } }
* ]
* }
* ];
* const mediaFiles = { 'word/media/img.png': 'base64data...' };
* const hydrated = hydrateImageBlocks(blocks, mediaFiles);
* // Image child's src is hydrated to data URL
* ```
*
* @example
* ```typescript
* // Using rId fallback when direct path doesn't match
* const blocks = [
* { kind: 'image', src: './image.png', attrs: { rId: 'rId3', extension: 'png' } }
* ];
* const mediaFiles = { 'word/media/rId3.png': 'base64data...' };
* const hydrated = hydrateImageBlocks(blocks, mediaFiles);
* // Matches via candidate path: word/media/rId3.png
* ```
*/
export function hydrateImageBlocks(blocks: FlowBlock[], mediaFiles?: Record<string, string>): FlowBlock[] {
if (!mediaFiles || Object.keys(mediaFiles).length === 0) {
return blocks;
}
const normalizedMedia = new Map<string, string>();
Object.entries(mediaFiles).forEach(([key, value]) => {
const normalized = normalizeMediaKey(key);
if (normalized) {
normalizedMedia.set(normalized, value);
}
});
if (normalizedMedia.size === 0) {
return blocks;
}
/**
* Helper to resolve an image source path to a data URL.
* Tries multiple candidate paths to find a match in the media files.
*/
const resolveImageSrc = (src: string, relId?: string, attrSrc?: string, extension?: string): string | undefined => {
if (!src || src.startsWith('data:')) {
return undefined;
}
const candidates = new Set<string>();
candidates.add(src);
if (attrSrc) candidates.add(attrSrc);
if (relId) {
const inferredExt = extension ?? inferExtensionFromPath(src) ?? 'jpeg';
candidates.add(`word/media/${relId}.${inferredExt}`);
candidates.add(`media/${relId}.${inferredExt}`);
}
for (const candidate of candidates) {
const normalized = normalizeMediaKey(candidate);
if (!normalized) continue;