@kidecms/core 0.25.0 → 0.25.1

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.
@@ -18,13 +18,26 @@ export function slugify(value: string) {
18
18
  .replace(/^-+|-+$/g, "");
19
19
  }
20
20
 
21
+ /** Imported values often omit `children` (or `id`); fill them in so the editor never
22
+ * reads `.children.length` off undefined. Extra keys are kept. */
23
+ export function normalizeItems(list: unknown): TreeItem[] {
24
+ if (!Array.isArray(list)) return [];
25
+ return list
26
+ .filter((item): item is Record<string, unknown> => !!item && typeof item === "object" && !Array.isArray(item))
27
+ .map((item) => ({
28
+ ...item,
29
+ id: typeof item.id === "string" && item.id ? item.id : generateId(),
30
+ children: normalizeItems(item.children),
31
+ }));
32
+ }
33
+
21
34
  export function parseItems(value?: string): TreeItem[] {
22
35
  if (!value) return [];
23
36
  try {
24
- const parsed = JSON.parse(value);
25
- if (Array.isArray(parsed)) return parsed;
26
- } catch {}
27
- return [];
37
+ return normalizeItems(JSON.parse(value));
38
+ } catch {
39
+ return [];
40
+ }
28
41
  }
29
42
 
30
43
  export function cloneItems(items: TreeItem[]): TreeItem[] {
@@ -63,6 +63,18 @@ export const FIELD_MODEL: Record<string, FieldModelEntry> = {
63
63
  control: "link control (internal page picker / external URL + label + new-tab)",
64
64
  valueShape: "{ type:'internal'|'external', url:string, label?:string, newTab?:boolean }",
65
65
  },
66
+ "taxonomy-terms": {
67
+ storage: "json",
68
+ control: "term tree editor (nested, sortable; feeds taxonomy-select fields by slug)",
69
+ valueShape:
70
+ "Array<{ id:string, name:string, slug:string, children:Term[] }> — `children` required on every term (use [])",
71
+ },
72
+ "menu-items": {
73
+ storage: "json",
74
+ control: "menu tree editor (nested to 3 levels, sortable, internal/external links)",
75
+ valueShape:
76
+ "Array<{ id:string, label:string, href:string, target?:'_blank', children:Item[] }> — `children` required on every item (use [])",
77
+ },
66
78
  relation: {
67
79
  storage: "text (json when hasMany)",
68
80
  control: "relation picker (single dropdown, or chips + dropdown when hasMany)",
@@ -120,10 +132,10 @@ const typeSpecific = (field: FieldConfig): Record<string, unknown> => {
120
132
  };
121
133
 
122
134
  export const describeField = (name: string, field: FieldConfig): Record<string, unknown> => {
123
- // `color`/`link` are admin.component helpers over text/json — report them as
124
- // their effective semantic type so the manifest is honest about the control.
135
+ // `color`/`link`/tree editors are admin.component helpers over text/json — report
136
+ // them as their effective semantic type so the manifest is honest about the control.
125
137
  const component = field.admin?.component;
126
- const effectiveType = component === "color" || component === "link" ? component : field.type;
138
+ const effectiveType = component && component in FIELD_MODEL ? component : field.type;
127
139
  const model = FIELD_MODEL[effectiveType] ?? { storage: "json", control: "unknown", valueShape: "unknown" };
128
140
  return {
129
141
  name,
package/core/migrate.ts CHANGED
@@ -59,9 +59,14 @@ const validateField = (
59
59
  case "content":
60
60
  validateContent(name, field, value, errors, warnings);
61
61
  break;
62
- case "json":
63
- if (field.itemFields && Array.isArray(value)) validateRows(name, field.itemFields, value, warnings);
62
+ case "json": {
63
+ const component = field.admin?.component;
64
+ if (component === "taxonomy-terms" || component === "menu-items") {
65
+ if (!Array.isArray(value)) errors.push({ field: name, message: `${component} expects an array` });
66
+ else validateTree(name, component, value, warnings);
67
+ } else if (field.itemFields && Array.isArray(value)) validateRows(name, field.itemFields, value, warnings);
64
68
  break;
69
+ }
65
70
  case "blocks":
66
71
  if (!Array.isArray(value)) errors.push({ field: name, message: "blocks expects an array" });
67
72
  else validateBlockList(name, field.types, value, warnings);
@@ -107,6 +112,37 @@ const validateRows = (
107
112
  });
108
113
  };
109
114
 
115
+ const TREE_ITEM_KEYS = {
116
+ "taxonomy-terms": ["name", "slug"],
117
+ "menu-items": ["label", "href"],
118
+ } as const;
119
+
120
+ /** Tree editors (menus, taxonomy terms) need `id` + `children` on every node. */
121
+ const validateTree = (
122
+ prefix: string,
123
+ component: keyof typeof TREE_ITEM_KEYS,
124
+ items: unknown[],
125
+ warnings: ValidationIssue[],
126
+ ) => {
127
+ items.forEach((item, i) => {
128
+ const at = `${prefix}[${i}]`;
129
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
130
+ warnings.push({ field: at, message: "tree item expects an object" });
131
+ return;
132
+ }
133
+ const node = item as Record<string, unknown>;
134
+ for (const key of ["id", ...TREE_ITEM_KEYS[component]]) {
135
+ if (typeof node[key] !== "string" || !node[key])
136
+ warnings.push({ field: `${at}.${key}`, message: "expected a string" });
137
+ }
138
+ if (!Array.isArray(node.children)) {
139
+ warnings.push({ field: `${at}.children`, message: "expected an array (use [] for a leaf)" });
140
+ return;
141
+ }
142
+ validateTree(`${at}.children`, component, node.children, warnings);
143
+ });
144
+ };
145
+
110
146
  const validateBlockList = (
111
147
  name: string,
112
148
  types: Record<string, Record<string, FieldConfig>>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kidecms/core",
3
- "version": "0.25.0",
3
+ "version": "0.25.1",
4
4
  "description": "Kide CMS runtime — core library, admin UI, routes, and middleware for Astro. Shipped as TypeScript source.",
5
5
  "keywords": [
6
6
  "cms",