@platecms/delta-cast 0.4.1 → 0.7.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.
package/README.md CHANGED
@@ -51,3 +51,63 @@ const castTree = {
51
51
  const hast = toHast(castTree);
52
52
  const htmlString = toHtml(hast);
53
53
  ```
54
+
55
+
56
+ # Normalization
57
+
58
+ A CAST tree can and should be normalized to ensure that it is in a consistent and predictable state. Different trees that represent the same content should be normalized to a single, canonical form.
59
+
60
+ ## Usage
61
+
62
+ ```typescript
63
+ import { normalizeCast } from '@platecms/delta-cast';
64
+
65
+ const normalizedTree = normalizeCast(castTree);
66
+ ```
67
+
68
+ ## Normalization Rules
69
+
70
+ The `normalizeCast` function applies the following normalization rules in an iterative process:
71
+
72
+ ### 1. Adjacent Node Merging
73
+ - **Text nodes**: Merge adjacent `text` nodes by concatenating their values
74
+ - **Formatting nodes**: Merge adjacent formatting nodes of the same type by combining their children
75
+ - **Mergeable types**: `text`, `bold`, `italic`, `underline`, `strikethrough`, `highlight`
76
+ - **Non-mergeable types**: `contentValue`, `externalLink`, `internalLink`, `inlineCode`
77
+
78
+ ### 2. Formatting Node Nesting Order
79
+ Formatting nodes are reordered to follow a consistent hierarchy:
80
+ ```
81
+ bold -> italic -> underline -> strikethrough -> highlight
82
+ ```
83
+
84
+ ### 3. Duplicate Formatting Removal
85
+ - Remove duplicate formatting nodes in a single path (e.g., `bold` containing `bold`)
86
+ - This prevents redundant nesting that doesn't change the visual output
87
+
88
+ ### 4. Empty Node Cleanup
89
+ - Remove empty formatting (inline) nodes that contain no children
90
+ - Block-level container nodes (paragraphs, headings, lists, etc.) are preserved even when empty
91
+
92
+ ## Special Behavior
93
+
94
+ - **ContentValue nodes**: Completely untouched - their nested CAST trees are never normalized
95
+ - **Property preservation**: All node properties (`url`, `target`, `prn`, `level`, `lang`, etc.) are preserved
96
+ - **Iterative process**: Normalization is an iterative process that continues until no more changes are detected
97
+ - **Immutable**: Returns a new tree without modifying the input
98
+
99
+ ## Formatting Priority
100
+
101
+ The `FORMATTING_PRIORITY` constant defines the canonical nesting order for formatting nodes:
102
+
103
+ ```typescript
104
+ export const FORMATTING_PRIORITY = {
105
+ 'bold': 1, // Highest priority
106
+ 'italic': 2,
107
+ 'underline': 3,
108
+ 'strikethrough': 4,
109
+ 'highlight': 5 // Lowest priority
110
+ } as const;
111
+ ```
112
+
113
+ This constant should be used across all packages (e.g., `cast-util-to-slate`, `cast-util-from-slate`) to ensure consistent formatting hierarchy throughout the platform.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@platecms/delta-cast",
3
3
  "description": "Package containing the definition of CAST",
4
- "version": "0.4.1",
4
+ "version": "0.7.0",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -22,7 +22,6 @@
22
22
  "tslib": "2.8.1"
23
23
  },
24
24
  "peerDependencies": {
25
- "@platecms/delta-plate-resource-notation": "0.4.1"
26
- },
27
- "type": "commonjs"
25
+ "@platecms/delta-plate-resource-notation": "0.7.0"
26
+ }
28
27
  }
@@ -1,3 +1,4 @@
1
1
  export type * from "./lib/schemas/schema";
2
2
  export * from "./lib/validate-cast";
3
3
  export * from "./lib/invalid-cast.error";
4
+ export * from "./lib/normalize-cast";
@@ -0,0 +1,6 @@
1
+ export class InvalidCastError extends Error {
2
+ public constructor(message?: string) {
3
+ super(message);
4
+ this.name = this.constructor.name;
5
+ }
6
+ }