@backmatter/core 0.0.36 → 0.0.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backmatter/core",
3
- "version": "0.0.36",
3
+ "version": "0.0.37",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -0,0 +1,112 @@
1
+ // packages/core/src/block-groups.ts
2
+ //
3
+ // Built-in block groups. A block opts in with `group: "header"` etc. The
4
+ // description tells editors and the AI what blocks in the group are for and
5
+ // where they belong on a page; `placement`/`maxPerPage` are defaults a block
6
+ // can override. Custom group names are allowed and simply have no built-in text.
7
+
8
+ export type BlockPlacement = "first" | "last" | "any";
9
+
10
+ export type BlockGroupInfo = {
11
+ label: string;
12
+ description: string;
13
+ placement?: BlockPlacement;
14
+ maxPerPage?: number;
15
+ };
16
+
17
+ export const BLOCK_GROUPS = {
18
+ header: {
19
+ label: "Page header",
20
+ description:
21
+ "Opens the page: hero, page title, intro banner. Says what the page is about in one glance. Exactly one, always the first block.",
22
+ placement: "first",
23
+ maxPerPage: 1,
24
+ },
25
+ content: {
26
+ label: "Content",
27
+ description:
28
+ "The body of the page: text sections, text with image, features, steps, FAQ. Alternate different content blocks instead of repeating one.",
29
+ },
30
+ media: {
31
+ label: "Media",
32
+ description:
33
+ "Visual sections: full-width images, galleries, video. Use them to break up runs of text; never two in a row.",
34
+ },
35
+ people: {
36
+ label: "People",
37
+ description: "Team members, board, contact persons, profiles. Group all people of one kind together.",
38
+ },
39
+ proof: {
40
+ label: "Social proof",
41
+ description:
42
+ "Testimonials, quotes, partner logos, numbers. Builds trust: place after the offer is explained and before the call to action.",
43
+ },
44
+ cta: {
45
+ label: "Call to action",
46
+ description:
47
+ "Asks the visitor to act: contact, apply, sign up, read more. Usually near the end of the page; at most two per page.",
48
+ maxPerPage: 2,
49
+ },
50
+ footer: {
51
+ label: "Footer",
52
+ description: "Closes the page. At most one, always the last block.",
53
+ placement: "last",
54
+ maxPerPage: 1,
55
+ },
56
+ } as const satisfies Record<string, BlockGroupInfo>;
57
+
58
+ export type BuiltinBlockGroup = keyof typeof BLOCK_GROUPS;
59
+
60
+ export function blockGroupInfo(group: string | undefined): BlockGroupInfo | null {
61
+ if (!group) return null;
62
+ return (BLOCK_GROUPS as Record<string, BlockGroupInfo>)[group] ?? null;
63
+ }
64
+
65
+ /** Placement and per-page limit for a block: its own values, else its group's defaults. */
66
+ export function blockRules(block: {
67
+ group?: string;
68
+ placement?: BlockPlacement;
69
+ maxPerPage?: number;
70
+ }): { placement: BlockPlacement; maxPerPage?: number } {
71
+ const group = blockGroupInfo(block.group);
72
+ const maxPerPage = block.maxPerPage ?? group?.maxPerPage;
73
+ return {
74
+ placement: block.placement ?? group?.placement ?? "any",
75
+ ...(maxPerPage !== undefined ? { maxPerPage } : {}),
76
+ };
77
+ }
78
+
79
+ /**
80
+ * Placement and per-page-limit problems in a page's block list, as short
81
+ * sentences. Advisory: commits do not enforce these rules.
82
+ */
83
+ export function blockLayoutWarnings(
84
+ blocks: unknown,
85
+ catalog: ReadonlyArray<{ name: string; group?: string; placement?: BlockPlacement; maxPerPage?: number }>,
86
+ prefix = "blocks",
87
+ ): string[] {
88
+ if (!Array.isArray(blocks)) return [];
89
+ const byName = new Map(catalog.map((b) => [b.name, b]));
90
+ const warnings: string[] = [];
91
+ const counts = new Map<string, number>();
92
+ blocks.forEach((item, i) => {
93
+ const type = item && typeof item === "object" ? (item as { type?: unknown }).type : undefined;
94
+ const def = typeof type === "string" ? byName.get(type) : undefined;
95
+ if (!def) return;
96
+ counts.set(def.name, (counts.get(def.name) ?? 0) + 1);
97
+ const { placement } = blockRules(def);
98
+ if (placement === "first" && i !== 0) {
99
+ warnings.push(`${prefix}[${i}]: ${def.name} should be the first block on the page`);
100
+ }
101
+ if (placement === "last" && i !== blocks.length - 1) {
102
+ warnings.push(`${prefix}[${i}]: ${def.name} should be the last block on the page`);
103
+ }
104
+ });
105
+ for (const [name, count] of counts) {
106
+ const { maxPerPage } = blockRules(byName.get(name)!);
107
+ if (maxPerPage !== undefined && count > maxPerPage) {
108
+ warnings.push(`${prefix}: ${count} ${name} blocks, but at most ${maxPerPage} per page`);
109
+ }
110
+ }
111
+ return warnings;
112
+ }
package/src/block.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  parseFieldSnapshot,
7
7
  serializeField,
8
8
  } from "./fields";
9
+ import type { BlockPlacement } from "./block-groups";
9
10
 
10
11
  export type BlockInstance = {
11
12
  id: string;
@@ -13,17 +14,67 @@ export type BlockInstance = {
13
14
  props: Record<string, unknown>;
14
15
  };
15
16
 
16
- export type BlockDef<C = unknown> = {
17
+ /**
18
+ * Guidance for editors and the AI. Never rendered on the site. Shared by
19
+ * block definitions and snapshots.
20
+ */
21
+ export type BlockMeta = {
22
+ /** What the block is for and when to use it. */
23
+ description?: string;
24
+ /** A built-in group from `BLOCK_GROUPS` ("header", "content", …) or a custom kebab-case name. */
25
+ group?: string;
26
+ /** "first" / "last" pin the block to the start or end of a page. Defaults from the group. */
27
+ placement?: BlockPlacement;
28
+ /** Most instances one page may hold. Defaults from the group. */
29
+ maxPerPage?: number;
30
+ };
31
+
32
+ export type BlockDef<C = unknown> = BlockMeta & {
17
33
  name: string;
18
34
  fields: Record<string, FieldDef>;
19
35
  component: C;
20
36
  };
21
37
 
22
- export type BlockSnapshot = {
38
+ export type BlockSnapshot = BlockMeta & {
23
39
  name: string;
24
40
  fields: Record<string, FieldSnapshot>;
25
41
  };
26
42
 
43
+ const PLACEMENTS: readonly BlockPlacement[] = ["first", "last", "any"];
44
+ const GROUP_NAME = /^[a-z][a-z0-9-]*$/;
45
+
46
+ /**
47
+ * Read guidance keys. `strict` throws on a bad value (defineBlock); otherwise a
48
+ * bad value is dropped so a stored snapshot still loads.
49
+ */
50
+ function readBlockMeta(value: Record<string, unknown>, strict: boolean): BlockMeta {
51
+ const meta: BlockMeta = {};
52
+ const bad = (message: string) => {
53
+ if (strict) throw new Error(message);
54
+ };
55
+ if (value.description !== undefined) {
56
+ if (typeof value.description !== "string") bad("block.description must be a string");
57
+ else if (value.description.trim()) meta.description = value.description.trim();
58
+ }
59
+ if (value.group !== undefined) {
60
+ if (typeof value.group !== "string" || !GROUP_NAME.test(value.group)) {
61
+ bad('block.group must be a lowercase name like "header" or "content"');
62
+ } else meta.group = value.group;
63
+ }
64
+ if (value.placement !== undefined) {
65
+ if (!PLACEMENTS.includes(value.placement as BlockPlacement)) {
66
+ bad('block.placement must be "first", "last" or "any"');
67
+ } else meta.placement = value.placement as BlockPlacement;
68
+ }
69
+ if (value.maxPerPage !== undefined) {
70
+ const n = value.maxPerPage;
71
+ if (typeof n !== "number" || !Number.isInteger(n) || n < 1) {
72
+ bad("block.maxPerPage must be a positive integer");
73
+ } else meta.maxPerPage = n;
74
+ }
75
+ return meta;
76
+ }
77
+
27
78
  const PASCAL = /^[A-Z][A-Za-z0-9]*$/;
28
79
 
29
80
  export function defineBlock<const D extends BlockDef>(def: D): D {
@@ -44,6 +95,7 @@ export function defineBlock<const D extends BlockDef>(def: D): D {
44
95
  `block.fields.${RESERVED_BLOCK_PROP} is reserved: it carries the external source record`,
45
96
  );
46
97
  }
98
+ readBlockMeta(def as unknown as Record<string, unknown>, true);
47
99
  return def;
48
100
  }
49
101
 
@@ -89,7 +141,11 @@ export function serializeBlock(block: BlockDef): BlockSnapshot {
89
141
  for (const [name, def] of Object.entries(block.fields)) {
90
142
  fields[name] = serializeField(def);
91
143
  }
92
- return { name: block.name, fields };
144
+ return {
145
+ name: block.name,
146
+ ...readBlockMeta(block as unknown as Record<string, unknown>, true),
147
+ fields,
148
+ };
93
149
  }
94
150
 
95
151
  export function parseBlockSnapshot(input: unknown): BlockSnapshot {
@@ -111,5 +167,5 @@ export function parseBlockSnapshot(input: unknown): BlockSnapshot {
111
167
  // Drop unknown kinds so a stored snapshot still loads.
112
168
  }
113
169
  }
114
- return { name: value.name, fields };
170
+ return { name: value.name, ...readBlockMeta(value, false), fields };
115
171
  }
package/src/fields.ts CHANGED
@@ -49,6 +49,8 @@ export type FieldDef<
49
49
  step?: number;
50
50
  where?: RelationWhere;
51
51
  multiple?: true;
52
+ /** Guidance for editors and the AI: what goes here, length, tone. Never rendered. */
53
+ description?: string;
52
54
  };
53
55
 
54
56
  export type FieldSnapshot = {
@@ -66,11 +68,17 @@ export type FieldSnapshot = {
66
68
  step?: number;
67
69
  where?: RelationWhere;
68
70
  multiple?: true;
71
+ description?: string;
69
72
  };
70
73
 
71
74
  type ObjectLike = { name: string; fields: Record<string, FieldDef> };
72
75
 
73
- type Opts<R extends boolean> = { required?: R; input?: string };
76
+ type Opts<R extends boolean> = {
77
+ required?: R;
78
+ input?: string;
79
+ /** Guidance for editors and the AI: what goes here, length, tone. Never rendered. */
80
+ description?: string;
81
+ };
74
82
 
75
83
  type TextOpts<R extends boolean> = Opts<R> & { options?: string[] };
76
84
 
@@ -435,7 +443,22 @@ type TextFieldDef<R extends boolean, O extends readonly string[] | undefined = u
435
443
  type MediaFieldDef<R extends boolean, M extends boolean = false> = FieldDef<"media", R> &
436
444
  (M extends true ? { readonly multiple: true } : {});
437
445
 
438
- export const field = {
446
+ /** Copy `opts.description` (trimmed, non-blank) onto the definition a constructor built. */
447
+ function described<A extends unknown[], D extends FieldDef>(
448
+ make: (...args: A) => D,
449
+ optsIndex: number,
450
+ ): (...args: A) => D {
451
+ return (...args: A) => {
452
+ const def = make(...args);
453
+ const opts = args[optsIndex] as { description?: unknown } | undefined;
454
+ const raw = opts && typeof opts === "object" ? opts.description : undefined;
455
+ if (raw === undefined) return def;
456
+ if (typeof raw !== "string") throw new Error("field description must be a string");
457
+ return raw.trim() ? { ...def, description: raw.trim() } : def;
458
+ };
459
+ }
460
+
461
+ const constructors = {
439
462
  text: <
440
463
  const R extends boolean = false,
441
464
  const O extends readonly string[] | undefined = undefined,
@@ -473,6 +496,19 @@ export const field = {
473
496
  array: arrayField,
474
497
  };
475
498
 
499
+ export const field = {
500
+ text: described(constructors.text, 0),
501
+ image: described(constructors.image, 0),
502
+ media: described(constructors.media, 0),
503
+ number: described(constructors.number, 0),
504
+ boolean: described(constructors.boolean, 0),
505
+ relation: described(constructors.relation, 1) as typeof relation,
506
+ link: described(constructors.link, 0) as typeof link,
507
+ blocks: described(constructors.blocks, 0),
508
+ object: described(constructors.object, 1) as typeof objectField,
509
+ array: described(constructors.array, 1) as typeof arrayField,
510
+ };
511
+
476
512
  export function serializeField(def: FieldDef): FieldSnapshot {
477
513
  const snap: FieldSnapshot = {
478
514
  kind: def.kind,
@@ -488,6 +524,7 @@ export function serializeField(def: FieldDef): FieldSnapshot {
488
524
  ...(def.models ? { models: [...def.models] } : {}),
489
525
  ...(def.where ? { where: def.where } : {}),
490
526
  ...(def.multiple ? { multiple: true as const } : {}),
527
+ ...(def.description?.trim() ? { description: def.description.trim() } : {}),
491
528
  };
492
529
  if (def.fields) {
493
530
  const fields: Record<string, FieldSnapshot> = {};
@@ -570,6 +607,9 @@ export function parseFieldSnapshot(input: unknown): FieldSnapshot {
570
607
  if (where) {
571
608
  snap.where = where;
572
609
  }
610
+ if (typeof value.description === "string" && value.description.trim()) {
611
+ snap.description = value.description.trim();
612
+ }
573
613
  if (value.fields && typeof value.fields === "object" && !Array.isArray(value.fields)) {
574
614
  const fields: Record<string, FieldSnapshot> = {};
575
615
  for (const [name, nested] of Object.entries(value.fields as Record<string, unknown>)) {
package/src/index.ts CHANGED
@@ -54,7 +54,10 @@ export type {
54
54
  BlockDef,
55
55
  BlockSnapshot,
56
56
  InferBlockProps,
57
+ BlockMeta,
57
58
  } from "./block";
59
+ export type { BlockGroupInfo, BlockPlacement, BuiltinBlockGroup } from "./block-groups";
60
+ export { BLOCK_GROUPS, blockGroupInfo, blockLayoutWarnings, blockRules } from "./block-groups";
58
61
  export {
59
62
  defineBlock,
60
63
  serializeBlock,