@backmatter/core 0.0.33 → 0.0.34
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 +1 -1
- package/src/block.ts +10 -8
- package/src/color.ts +40 -0
- package/src/doc.ts +2 -0
- package/src/document-label.ts +24 -0
- package/src/fields.ts +264 -15
- package/src/index.ts +7 -1
- package/src/model.ts +11 -6
- package/src/node/cli.ts +10 -2
- package/src/node/create-backmatter.ts +15 -9
- package/src/node/eval-model.ts +109 -25
- package/src/node/generate.ts +14 -5
- package/src/node/index.ts +7 -1
- package/src/query.ts +457 -78
- package/src/relation-match.ts +12 -0
- package/src/relation-problems.ts +189 -0
- package/src/schema-check.ts +199 -0
- package/src/sync-schema.ts +2 -0
- package/src/validate.ts +108 -21
- package/src/zod.ts +127 -10
package/package.json
CHANGED
package/src/block.ts
CHANGED
|
@@ -52,13 +52,15 @@ export const RESERVED_BLOCK_PROP = "data";
|
|
|
52
52
|
|
|
53
53
|
type Simplify<T> = { -readonly [K in keyof T]: T[K] } & {};
|
|
54
54
|
|
|
55
|
-
type
|
|
55
|
+
type ValueOfField<F> = F extends { kind: "number" }
|
|
56
56
|
? number
|
|
57
|
-
:
|
|
57
|
+
: F extends { kind: "boolean" }
|
|
58
58
|
? boolean
|
|
59
|
-
:
|
|
59
|
+
: F extends { kind: "blocks" }
|
|
60
60
|
? BlockInstance[]
|
|
61
|
-
: string
|
|
61
|
+
: F extends { options: infer O extends readonly [string, ...string[]] }
|
|
62
|
+
? O[number]
|
|
63
|
+
: string;
|
|
62
64
|
|
|
63
65
|
type IsRequired<F> = F extends { required?: infer R }
|
|
64
66
|
? [Exclude<R, undefined>] extends [true]
|
|
@@ -72,12 +74,12 @@ type IsRequired<F> = F extends { required?: infer R }
|
|
|
72
74
|
*/
|
|
73
75
|
export type InferBlockProps<Fields extends Record<string, FieldDef>, Data = undefined> = Simplify<
|
|
74
76
|
{
|
|
75
|
-
[N in keyof Fields as IsRequired<Fields[N]> extends true ? N : never]:
|
|
76
|
-
Fields[N]
|
|
77
|
+
[N in keyof Fields as IsRequired<Fields[N]> extends true ? N : never]: ValueOfField<
|
|
78
|
+
Fields[N]
|
|
77
79
|
>;
|
|
78
80
|
} & {
|
|
79
|
-
[N in keyof Fields as IsRequired<Fields[N]> extends true ? never : N]?:
|
|
80
|
-
Fields[N]
|
|
81
|
+
[N in keyof Fields as IsRequired<Fields[N]> extends true ? never : N]?: ValueOfField<
|
|
82
|
+
Fields[N]
|
|
81
83
|
>;
|
|
82
84
|
} & ([Data] extends [undefined] ? {} : { data: Data })
|
|
83
85
|
>;
|
package/src/color.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// packages/core/src/color.ts
|
|
2
|
+
|
|
3
|
+
function isHex(value: string): boolean {
|
|
4
|
+
const match = /^#([0-9a-fA-F]+)$/.exec(value);
|
|
5
|
+
if (!match) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
const digits = match[1];
|
|
9
|
+
const len = digits.length;
|
|
10
|
+
if (len !== 3 && len !== 4 && len !== 6 && len !== 8) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const NUM = String.raw`(?:[+-]?(?:\d+\.?\d*|\.\d+))`;
|
|
17
|
+
const PCT = String.raw`${NUM}%`;
|
|
18
|
+
const CHANNEL = String.raw`(?:${NUM}|${PCT})`;
|
|
19
|
+
const HUE = String.raw`${NUM}(?:deg|rad|grad|turn)?`;
|
|
20
|
+
|
|
21
|
+
const FUNCTIONAL = [
|
|
22
|
+
String.raw`^rgb\(\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*(?:,\s*${CHANNEL})?\s*\)$`,
|
|
23
|
+
String.raw`^rgba\(\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*\)$`,
|
|
24
|
+
String.raw`^rgb\(\s*${CHANNEL}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
|
|
25
|
+
String.raw`^rgba\(\s*${CHANNEL}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
|
|
26
|
+
String.raw`^hsl\(\s*${HUE}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*(?:,\s*${CHANNEL})?\s*\)$`,
|
|
27
|
+
String.raw`^hsla\(\s*${HUE}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*\)$`,
|
|
28
|
+
String.raw`^hsl\(\s*${HUE}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
|
|
29
|
+
String.raw`^hsla\(\s*${HUE}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
|
|
30
|
+
String.raw`^hsla\(\s*${HUE}\s+${CHANNEL}\s+${CHANNEL}\s*/\s*${CHANNEL}\s*\)$`,
|
|
31
|
+
String.raw`^oklch\(\s*${CHANNEL}\s+${CHANNEL}\s+${HUE}(?:\s*/\s*${CHANNEL})?\s*\)$`,
|
|
32
|
+
String.raw`^oklch\(\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${HUE}(?:\s*/\s*${CHANNEL})?\s*\)$`,
|
|
33
|
+
].map((source) => new RegExp(source, "i"));
|
|
34
|
+
|
|
35
|
+
export function isColorString(value: string): boolean {
|
|
36
|
+
if (isHex(value)) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return FUNCTIONAL.some((pattern) => pattern.test(value));
|
|
40
|
+
}
|
package/src/doc.ts
CHANGED
|
@@ -5,6 +5,8 @@ export type BackmatterDocSummary = {
|
|
|
5
5
|
slug: string;
|
|
6
6
|
/** Card-level fields when the loader already parsed the file. */
|
|
7
7
|
frontmatter?: Record<string, unknown>;
|
|
8
|
+
/** Markdown when the loader already read the file. */
|
|
9
|
+
body?: string;
|
|
8
10
|
};
|
|
9
11
|
|
|
10
12
|
export type BackmatterDoc = {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// packages/core/src/document-label.ts
|
|
2
|
+
const TITLE_KEYS = ["title", "name", "headline", "heading", "label"] as const;
|
|
3
|
+
const TITLE_KEY_SET = new Set<string>(TITLE_KEYS);
|
|
4
|
+
|
|
5
|
+
export function documentLabel(
|
|
6
|
+
frontmatter: Record<string, unknown>,
|
|
7
|
+
model: { fields?: Record<string, { kind: string }> } | null | undefined,
|
|
8
|
+
): string | null {
|
|
9
|
+
const pick = (key: string) => {
|
|
10
|
+
const v = frontmatter[key];
|
|
11
|
+
return typeof v === "string" && v.trim() ? v.trim() : null;
|
|
12
|
+
};
|
|
13
|
+
for (const key of TITLE_KEYS) {
|
|
14
|
+
const v = pick(key);
|
|
15
|
+
if (v) return v;
|
|
16
|
+
}
|
|
17
|
+
for (const [key, field] of Object.entries(model?.fields ?? {})) {
|
|
18
|
+
if (TITLE_KEY_SET.has(key)) continue;
|
|
19
|
+
if (field.kind !== "text") continue;
|
|
20
|
+
const v = pick(key);
|
|
21
|
+
if (v) return v;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
package/src/fields.ts
CHANGED
|
@@ -11,6 +11,9 @@ export type FieldKind =
|
|
|
11
11
|
| "object"
|
|
12
12
|
| "array";
|
|
13
13
|
|
|
14
|
+
/** Equality filter on scalar frontmatter keys for a file-backed relation target. */
|
|
15
|
+
export type RelationWhere = Record<string, string | number | boolean>;
|
|
16
|
+
|
|
14
17
|
/**
|
|
15
18
|
* Field definition. Generic parameters only exist for type inference
|
|
16
19
|
* (`InferFrontmatter`); the runtime object is `{ kind, required?, relationTo? }`.
|
|
@@ -34,6 +37,12 @@ export type FieldDef<
|
|
|
34
37
|
* back to the default input for the kind.
|
|
35
38
|
*/
|
|
36
39
|
input?: string;
|
|
40
|
+
/** Allowed values for a text field stored as a string. */
|
|
41
|
+
options?: readonly string[];
|
|
42
|
+
min?: number;
|
|
43
|
+
max?: number;
|
|
44
|
+
step?: number;
|
|
45
|
+
where?: RelationWhere;
|
|
37
46
|
};
|
|
38
47
|
|
|
39
48
|
export type FieldSnapshot = {
|
|
@@ -43,30 +52,159 @@ export type FieldSnapshot = {
|
|
|
43
52
|
object?: string;
|
|
44
53
|
fields?: Record<string, FieldSnapshot>;
|
|
45
54
|
input?: string;
|
|
55
|
+
options?: string[];
|
|
56
|
+
min?: number;
|
|
57
|
+
max?: number;
|
|
58
|
+
step?: number;
|
|
59
|
+
where?: RelationWhere;
|
|
46
60
|
};
|
|
47
61
|
|
|
48
62
|
type ObjectLike = { name: string; fields: Record<string, FieldDef> };
|
|
49
63
|
|
|
50
64
|
type Opts<R extends boolean> = { required?: R; input?: string };
|
|
51
65
|
|
|
66
|
+
type TextOpts<R extends boolean> = Opts<R> & { options?: string[] };
|
|
67
|
+
|
|
68
|
+
type NumberOpts<R extends boolean> = Opts<R> & {
|
|
69
|
+
min?: number;
|
|
70
|
+
max?: number;
|
|
71
|
+
step?: number;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
type RelationModelInput = {
|
|
75
|
+
type: string;
|
|
76
|
+
fields: Record<string, FieldDef>;
|
|
77
|
+
external?: boolean;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
type ScalarFieldKind = "text" | "number" | "boolean";
|
|
81
|
+
|
|
82
|
+
type ScalarValueOfField<F extends FieldDef> = F extends { kind: "number" }
|
|
83
|
+
? number
|
|
84
|
+
: F extends { kind: "boolean" }
|
|
85
|
+
? boolean
|
|
86
|
+
: F extends { options: infer O extends readonly [string, ...string[]] }
|
|
87
|
+
? O[number]
|
|
88
|
+
: F extends { kind: "text" }
|
|
89
|
+
? string
|
|
90
|
+
: never;
|
|
91
|
+
|
|
92
|
+
/** Equality filters on slug and scalar fields of a file-backed relation target. */
|
|
93
|
+
export type RelationWhereForModel<M extends RelationModelInput> = Partial<
|
|
94
|
+
{ slug: string } & {
|
|
95
|
+
[K in keyof M["fields"] as M["fields"][K]["kind"] extends ScalarFieldKind
|
|
96
|
+
? K
|
|
97
|
+
: never]: ScalarValueOfField<M["fields"][K]>;
|
|
98
|
+
}
|
|
99
|
+
>;
|
|
100
|
+
|
|
101
|
+
type RelationModelOpts<M extends RelationModelInput, R extends boolean> = Opts<R> & {
|
|
102
|
+
where: RelationWhereForModel<M>;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
function isBlankString(value: string): boolean {
|
|
106
|
+
return value.trim() === "";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function assertTextOptions(options: string[]): string[] {
|
|
110
|
+
if (options.length === 0) {
|
|
111
|
+
throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
|
|
112
|
+
}
|
|
113
|
+
const seen = new Set<string>();
|
|
114
|
+
for (const option of options) {
|
|
115
|
+
if (typeof option !== "string" || isBlankString(option)) {
|
|
116
|
+
throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
|
|
117
|
+
}
|
|
118
|
+
if (seen.has(option)) {
|
|
119
|
+
throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
|
|
120
|
+
}
|
|
121
|
+
seen.add(option);
|
|
122
|
+
}
|
|
123
|
+
return options;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function assertNumberConstraints(
|
|
127
|
+
opts: NumberOpts<boolean>,
|
|
128
|
+
): { min: number; max: number; step?: number } | undefined {
|
|
129
|
+
const { min, max, step } = opts;
|
|
130
|
+
if (min === undefined && max === undefined && step === undefined) {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
if (min === undefined || max === undefined) {
|
|
134
|
+
throw new Error("field.number min and max must be provided together");
|
|
135
|
+
}
|
|
136
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
|
137
|
+
throw new Error("field.number min and max must be finite");
|
|
138
|
+
}
|
|
139
|
+
if (min >= max) {
|
|
140
|
+
throw new Error("field.number min must be less than max");
|
|
141
|
+
}
|
|
142
|
+
if (step !== undefined) {
|
|
143
|
+
if (!Number.isFinite(step) || step <= 0 || step > max - min) {
|
|
144
|
+
throw new Error("field.number step must be finite, greater than 0, and at most max - min");
|
|
145
|
+
}
|
|
146
|
+
return { min, max, step };
|
|
147
|
+
}
|
|
148
|
+
return { min, max };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function parseRelationWhere(value: unknown): RelationWhere | undefined {
|
|
152
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
const where: RelationWhere = {};
|
|
156
|
+
for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
|
|
157
|
+
if (typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean") {
|
|
158
|
+
where[key] = entry;
|
|
159
|
+
} else {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return where;
|
|
164
|
+
}
|
|
165
|
+
|
|
52
166
|
function base<K extends FieldKind, R extends boolean, To extends string>(
|
|
53
167
|
kind: K,
|
|
54
|
-
opts?: {
|
|
168
|
+
opts?: {
|
|
169
|
+
required?: R;
|
|
170
|
+
relationTo?: To;
|
|
171
|
+
input?: string;
|
|
172
|
+
options?: readonly string[];
|
|
173
|
+
min?: number;
|
|
174
|
+
max?: number;
|
|
175
|
+
step?: number;
|
|
176
|
+
where?: RelationWhere;
|
|
177
|
+
},
|
|
55
178
|
): FieldDef<K, R, To> {
|
|
56
179
|
return {
|
|
57
180
|
kind,
|
|
58
181
|
...(opts?.required ? { required: opts.required } : {}),
|
|
59
182
|
...(opts?.relationTo ? { relationTo: opts.relationTo } : {}),
|
|
60
183
|
...(opts?.input ? { input: opts.input } : {}),
|
|
184
|
+
...(opts?.options ? { options: opts.options } : {}),
|
|
185
|
+
...(opts?.min !== undefined ? { min: opts.min } : {}),
|
|
186
|
+
...(opts?.max !== undefined ? { max: opts.max } : {}),
|
|
187
|
+
...(opts?.step !== undefined ? { step: opts.step } : {}),
|
|
188
|
+
...(opts?.where ? { where: opts.where } : {}),
|
|
61
189
|
} as FieldDef<K, R, To>;
|
|
62
190
|
}
|
|
63
191
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
192
|
+
type InlineFields = Record<string, FieldDef>;
|
|
193
|
+
|
|
194
|
+
type NamedComposite<K extends "object" | "array", R extends boolean> = FieldDef<K, R> & {
|
|
195
|
+
object: string;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
type InlineComposite<K extends "object" | "array", R extends boolean, F> = {
|
|
199
|
+
kind: K;
|
|
200
|
+
object: string;
|
|
201
|
+
required?: R;
|
|
202
|
+
input?: string;
|
|
203
|
+
fields: F;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
function buildComposite(kind: "object" | "array", def: string | ObjectLike, opts?: Opts<boolean>) {
|
|
207
|
+
const required = opts?.required ? { required: opts.required } : {};
|
|
70
208
|
const input = opts?.input ? { input: opts.input } : {};
|
|
71
209
|
if (typeof def === "string") {
|
|
72
210
|
return { kind, object: def, ...required, ...input };
|
|
@@ -74,19 +212,101 @@ function composite<K extends "object" | "array", R extends boolean>(
|
|
|
74
212
|
return { kind, object: def.name, fields: def.fields, ...required, ...input };
|
|
75
213
|
}
|
|
76
214
|
|
|
215
|
+
function objectField<const F extends InlineFields, const R extends boolean = false>(
|
|
216
|
+
def: { name: string; fields: F },
|
|
217
|
+
opts?: Opts<R>,
|
|
218
|
+
): InlineComposite<"object", R, F>;
|
|
219
|
+
function objectField<const R extends boolean = false>(
|
|
220
|
+
def: string,
|
|
221
|
+
opts?: Opts<R>,
|
|
222
|
+
): NamedComposite<"object", R>;
|
|
223
|
+
function objectField(def: string | ObjectLike, opts?: Opts<boolean>): InlineComposite<"object", boolean, InlineFields> {
|
|
224
|
+
return buildComposite("object", def, opts) as InlineComposite<"object", boolean, InlineFields>;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function arrayField<const F extends InlineFields, const R extends boolean = false>(
|
|
228
|
+
def: { name: string; fields: F },
|
|
229
|
+
opts?: Opts<R>,
|
|
230
|
+
): InlineComposite<"array", R, F>;
|
|
231
|
+
function arrayField<const R extends boolean = false>(
|
|
232
|
+
def: string,
|
|
233
|
+
opts?: Opts<R>,
|
|
234
|
+
): NamedComposite<"array", R>;
|
|
235
|
+
function arrayField(def: string | ObjectLike, opts?: Opts<boolean>): InlineComposite<"array", boolean, InlineFields> {
|
|
236
|
+
return buildComposite("array", def, opts) as InlineComposite<"array", boolean, InlineFields>;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function relation<To extends string, const R extends boolean = false>(
|
|
240
|
+
to: To,
|
|
241
|
+
opts?: Opts<R>,
|
|
242
|
+
): FieldDef<"relation", R, To>;
|
|
243
|
+
function relation<M extends RelationModelInput, const R extends boolean = false>(
|
|
244
|
+
model: M,
|
|
245
|
+
opts: RelationModelOpts<M, R>,
|
|
246
|
+
): FieldDef<"relation", R, M["type"]>;
|
|
247
|
+
function relation(
|
|
248
|
+
to: string | RelationModelInput,
|
|
249
|
+
opts?: Opts<boolean> | RelationModelOpts<RelationModelInput, boolean>,
|
|
250
|
+
): FieldDef<"relation", boolean, string> {
|
|
251
|
+
if (typeof to === "string") {
|
|
252
|
+
if (opts && "where" in opts && opts.where !== undefined) {
|
|
253
|
+
throw new Error("field.relation where requires a model target");
|
|
254
|
+
}
|
|
255
|
+
return base("relation", { ...opts, relationTo: to });
|
|
256
|
+
}
|
|
257
|
+
if (to.external) {
|
|
258
|
+
throw new Error("field.relation where cannot target an external model");
|
|
259
|
+
}
|
|
260
|
+
const where = parseRelationWhere(
|
|
261
|
+
opts !== undefined && "where" in opts ? opts.where : undefined,
|
|
262
|
+
);
|
|
263
|
+
if (!where) {
|
|
264
|
+
throw new Error("field.relation where must be a plain object");
|
|
265
|
+
}
|
|
266
|
+
return base("relation", {
|
|
267
|
+
relationTo: to.type,
|
|
268
|
+
where,
|
|
269
|
+
required: opts?.required,
|
|
270
|
+
input: opts?.input,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
type TextFieldDef<R extends boolean, O extends readonly string[] | undefined = undefined> =
|
|
275
|
+
FieldDef<"text", R> & (O extends readonly string[] ? { readonly options: O } : {});
|
|
276
|
+
|
|
77
277
|
export const field = {
|
|
78
|
-
text: <
|
|
278
|
+
text: <
|
|
279
|
+
const R extends boolean = false,
|
|
280
|
+
const O extends readonly string[] | undefined = undefined,
|
|
281
|
+
>(
|
|
282
|
+
opts?: Opts<R> & { options?: O },
|
|
283
|
+
): TextFieldDef<R, O> => {
|
|
284
|
+
if (opts?.options !== undefined) {
|
|
285
|
+
if (opts.input !== undefined) {
|
|
286
|
+
throw new Error("field.text options cannot be combined with input");
|
|
287
|
+
}
|
|
288
|
+
const options = assertTextOptions([...opts.options]);
|
|
289
|
+
return base("text", { required: opts.required, options }) as TextFieldDef<R, O>;
|
|
290
|
+
}
|
|
291
|
+
return base("text", {
|
|
292
|
+
required: opts?.required,
|
|
293
|
+
...(opts?.input ? { input: opts.input } : {}),
|
|
294
|
+
}) as TextFieldDef<R, O>;
|
|
295
|
+
},
|
|
79
296
|
image: <const R extends boolean = false>(opts?: Opts<R>) => base("image", opts),
|
|
80
297
|
media: <const R extends boolean = false>(opts?: Opts<R>) => base("media", opts),
|
|
81
|
-
number: <const R extends boolean = false>(opts?:
|
|
298
|
+
number: <const R extends boolean = false>(opts?: NumberOpts<R>) => {
|
|
299
|
+
const bounds = opts ? assertNumberConstraints(opts) : undefined;
|
|
300
|
+
if (bounds) {
|
|
301
|
+
return base("number", { ...opts, ...bounds });
|
|
302
|
+
}
|
|
303
|
+
return base("number", opts);
|
|
304
|
+
},
|
|
82
305
|
boolean: <const R extends boolean = false>(opts?: Opts<R>) => base("boolean", opts),
|
|
83
|
-
relation
|
|
84
|
-
base("relation", { ...opts, relationTo: to }),
|
|
306
|
+
relation,
|
|
85
307
|
blocks: <const R extends boolean = false>(opts?: Opts<R>) => base("blocks", opts),
|
|
86
|
-
object:
|
|
87
|
-
|
|
88
|
-
array: <const R extends boolean = false>(def: string | ObjectLike, opts?: Opts<R>) =>
|
|
89
|
-
composite("array", def, opts),
|
|
308
|
+
object: objectField,
|
|
309
|
+
array: arrayField,
|
|
90
310
|
};
|
|
91
311
|
|
|
92
312
|
export function serializeField(def: FieldDef): FieldSnapshot {
|
|
@@ -96,6 +316,11 @@ export function serializeField(def: FieldDef): FieldSnapshot {
|
|
|
96
316
|
...(def.relationTo ? { relationTo: def.relationTo } : {}),
|
|
97
317
|
...(def.object ? { object: def.object } : {}),
|
|
98
318
|
...(def.input ? { input: def.input } : {}),
|
|
319
|
+
...(def.options ? { options: [...def.options] } : {}),
|
|
320
|
+
...(def.min !== undefined ? { min: def.min } : {}),
|
|
321
|
+
...(def.max !== undefined ? { max: def.max } : {}),
|
|
322
|
+
...(def.step !== undefined ? { step: def.step } : {}),
|
|
323
|
+
...(def.where ? { where: def.where } : {}),
|
|
99
324
|
};
|
|
100
325
|
if (def.fields) {
|
|
101
326
|
const fields: Record<string, FieldSnapshot> = {};
|
|
@@ -138,6 +363,30 @@ export function parseFieldSnapshot(input: unknown): FieldSnapshot {
|
|
|
138
363
|
if (typeof value.input === "string" && value.input.length > 0) {
|
|
139
364
|
snap.input = value.input;
|
|
140
365
|
}
|
|
366
|
+
if (Array.isArray(value.options)) {
|
|
367
|
+
const options: string[] = [];
|
|
368
|
+
for (const item of value.options) {
|
|
369
|
+
if (typeof item === "string" && !isBlankString(item)) {
|
|
370
|
+
options.push(item);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (options.length > 0) {
|
|
374
|
+
snap.options = options;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (typeof value.min === "number" && Number.isFinite(value.min)) {
|
|
378
|
+
snap.min = value.min;
|
|
379
|
+
}
|
|
380
|
+
if (typeof value.max === "number" && Number.isFinite(value.max)) {
|
|
381
|
+
snap.max = value.max;
|
|
382
|
+
}
|
|
383
|
+
if (typeof value.step === "number" && Number.isFinite(value.step)) {
|
|
384
|
+
snap.step = value.step;
|
|
385
|
+
}
|
|
386
|
+
const where = parseRelationWhere(value.where);
|
|
387
|
+
if (where) {
|
|
388
|
+
snap.where = where;
|
|
389
|
+
}
|
|
141
390
|
if (value.fields && typeof value.fields === "object" && !Array.isArray(value.fields)) {
|
|
142
391
|
const fields: Record<string, FieldSnapshot> = {};
|
|
143
392
|
for (const [name, nested] of Object.entries(value.fields as Record<string, unknown>)) {
|
package/src/index.ts
CHANGED
|
@@ -47,6 +47,8 @@ export {
|
|
|
47
47
|
parseSchemaSnapshot,
|
|
48
48
|
} from "./model";
|
|
49
49
|
|
|
50
|
+
export { assertSchema } from "./schema-check";
|
|
51
|
+
|
|
50
52
|
export type {
|
|
51
53
|
BlockInstance,
|
|
52
54
|
BlockDef,
|
|
@@ -131,6 +133,10 @@ export {
|
|
|
131
133
|
validateContentTree,
|
|
132
134
|
} from "./validate";
|
|
133
135
|
export { coerceFieldValue, coerceDocValues, imageUrlFromUnknown } from "./coerce";
|
|
136
|
+
export { documentLabel } from "./document-label";
|
|
137
|
+
export { matchesWhere } from "./relation-match";
|
|
138
|
+
export { relationProblems } from "./relation-problems";
|
|
139
|
+
export { isColorString } from "./color";
|
|
134
140
|
|
|
135
141
|
export type { FrontmatterJsonSchema } from "./zod";
|
|
136
142
|
export {
|
|
@@ -145,7 +151,7 @@ export {
|
|
|
145
151
|
// createBackmatter and the CLI live in `@backmatter/core/node`.
|
|
146
152
|
export type { BackmatterDoc, BackmatterDocSummary } from "./doc";
|
|
147
153
|
export { createQuery } from "./query";
|
|
148
|
-
export type { Query, ModelQuery,
|
|
154
|
+
export type { Query, ModelQuery, Where, DocLoader } from "./query";
|
|
149
155
|
export type { PreviewCheckout, PreviewSessionClaims, PreviewTicket } from "./preview-session";
|
|
150
156
|
export {
|
|
151
157
|
PREVIEW_COOKIE,
|
package/src/model.ts
CHANGED
|
@@ -174,6 +174,9 @@ export function defineModel<const D extends ModelInput>(def: D): WithTemplate<Wi
|
|
|
174
174
|
if (!def.fields || typeof def.fields !== "object") {
|
|
175
175
|
throw new Error("model.fields is required");
|
|
176
176
|
}
|
|
177
|
+
for (const name of ["slug", "body"] as const) {
|
|
178
|
+
if (name in def.fields) throw new Error(`model.fields.${name} is reserved`);
|
|
179
|
+
}
|
|
177
180
|
const template = resolveTemplateSlug(def);
|
|
178
181
|
const withPath = (def.path === undefined ? { ...def, path } : { ...def }) as WithTemplate<
|
|
179
182
|
WithPath<D>
|
|
@@ -187,13 +190,15 @@ export function defineModel<const D extends ModelInput>(def: D): WithTemplate<Wi
|
|
|
187
190
|
|
|
188
191
|
type Simplify<T> = { -readonly [K in keyof T]: T[K] } & {};
|
|
189
192
|
|
|
190
|
-
type
|
|
193
|
+
type ValueOfField<F> = F extends { kind: "number" }
|
|
191
194
|
? number
|
|
192
|
-
:
|
|
195
|
+
: F extends { kind: "boolean" }
|
|
193
196
|
? boolean
|
|
194
|
-
:
|
|
197
|
+
: F extends { kind: "blocks" }
|
|
195
198
|
? BlockInstance[]
|
|
196
|
-
: string
|
|
199
|
+
: F extends { options: infer O extends readonly [string, ...string[]] }
|
|
200
|
+
? O[number]
|
|
201
|
+
: string;
|
|
197
202
|
|
|
198
203
|
type IsRequired<F> = F extends { required?: infer R }
|
|
199
204
|
? [Exclude<R, undefined>] extends [true]
|
|
@@ -206,11 +211,11 @@ export type InferFrontmatter<M extends ModelDef> = Simplify<
|
|
|
206
211
|
{
|
|
207
212
|
[N in keyof M["fields"] as IsRequired<M["fields"][N]> extends true
|
|
208
213
|
? N
|
|
209
|
-
: never]:
|
|
214
|
+
: never]: ValueOfField<M["fields"][N]>;
|
|
210
215
|
} & {
|
|
211
216
|
[N in keyof M["fields"] as IsRequired<M["fields"][N]> extends true
|
|
212
217
|
? never
|
|
213
|
-
: N]?:
|
|
218
|
+
: N]?: ValueOfField<M["fields"][N]>;
|
|
214
219
|
}
|
|
215
220
|
>;
|
|
216
221
|
|
package/src/node/cli.ts
CHANGED
|
@@ -8,13 +8,14 @@ import {
|
|
|
8
8
|
frontmatterJsonSchema,
|
|
9
9
|
serializeModel,
|
|
10
10
|
serializeBlock,
|
|
11
|
+
serializeObject,
|
|
11
12
|
validateContentTree,
|
|
12
13
|
type ContentFileResult,
|
|
13
14
|
type ModelDef,
|
|
14
15
|
type ModelSnapshot,
|
|
15
16
|
} from "../index";
|
|
16
17
|
import { resolveConfig, type BackmatterConfig, type ResolvedConfig } from "../config";
|
|
17
|
-
import { discoverModels, discoverBlocks } from "./discover";
|
|
18
|
+
import { discoverModels, discoverBlocks, discoverObjects } from "./discover";
|
|
18
19
|
import { walkContentFiles } from "./fs-loader";
|
|
19
20
|
import { detectSdk, findConfigFile, generate, type GenerateResult } from "./generate";
|
|
20
21
|
import { ensureRegistryTsconfig, REGISTRY_SPECIFIER } from "./registry-import";
|
|
@@ -177,6 +178,13 @@ export async function runValidate(
|
|
|
177
178
|
} catch {
|
|
178
179
|
catalog = [];
|
|
179
180
|
}
|
|
181
|
+
let objects: ReturnType<typeof serializeObject>[] = [];
|
|
182
|
+
try {
|
|
183
|
+
const defs = await discoverObjects(root, config.dirs.objects);
|
|
184
|
+
objects = defs.map((def) => serializeObject(def));
|
|
185
|
+
} catch {
|
|
186
|
+
objects = [];
|
|
187
|
+
}
|
|
180
188
|
const roots = contentRoots(models);
|
|
181
189
|
const files: string[] = [];
|
|
182
190
|
for (const contentRoot of roots) {
|
|
@@ -192,7 +200,7 @@ export async function runValidate(
|
|
|
192
200
|
raw: await readFile(full, "utf-8"),
|
|
193
201
|
})),
|
|
194
202
|
);
|
|
195
|
-
const results = validateContentTree({ models, files: inputs, catalog });
|
|
203
|
+
const results = validateContentTree({ models, files: inputs, catalog, objects });
|
|
196
204
|
return { results, roots, exitCode: results.some((r) => !r.ok) ? 1 : 0 };
|
|
197
205
|
}
|
|
198
206
|
|
|
@@ -14,10 +14,12 @@ import { contentCacheTags, listCacheTags } from "../content-committed";
|
|
|
14
14
|
* `BackmatterConfig` (usually spread from `backmatter.config.ts`) plus the discovered
|
|
15
15
|
* models. Unset fields fall back to `BACKMATTER_*` env vars.
|
|
16
16
|
*/
|
|
17
|
-
export type CreateBackmatterOptions<
|
|
18
|
-
|
|
17
|
+
export type CreateBackmatterOptions<
|
|
18
|
+
Models extends readonly ModelDef[] = readonly ModelDef[],
|
|
19
|
+
Blocks extends readonly BlockDef[] = readonly BlockDef[],
|
|
20
|
+
> = BackmatterConfig & {
|
|
19
21
|
models: Models;
|
|
20
|
-
blocks?:
|
|
22
|
+
blocks?: Blocks;
|
|
21
23
|
/** In-content blocks (`richtext/*.tsx`). Synced separately from page blocks. */
|
|
22
24
|
richtext?: readonly BlockDef[];
|
|
23
25
|
objects?: readonly ObjectDef[];
|
|
@@ -33,13 +35,16 @@ export type CreateBackmatterOptions<Models extends readonly ModelDef[] = readonl
|
|
|
33
35
|
sync?: boolean;
|
|
34
36
|
};
|
|
35
37
|
|
|
36
|
-
export type BackmatterClient<
|
|
38
|
+
export type BackmatterClient<
|
|
39
|
+
Models extends readonly ModelDef[] = readonly ModelDef[],
|
|
40
|
+
Blocks extends readonly BlockDef[] = readonly BlockDef[],
|
|
41
|
+
> = {
|
|
37
42
|
listDocs: (filter?: {
|
|
38
43
|
type?: string;
|
|
39
44
|
}) => Promise<BackmatterDocSummary[]>;
|
|
40
45
|
getDoc: (type: string, slug: string) => Promise<BackmatterDoc | null>;
|
|
41
46
|
/** Typed access keyed by model.type: cms.query.blog.get / findMany */
|
|
42
|
-
query: Query<Models>;
|
|
47
|
+
query: Query<Models, Blocks>;
|
|
43
48
|
/** The registered models, e.g. for route matching. */
|
|
44
49
|
models: Models;
|
|
45
50
|
/** Effective config after env fallbacks. */
|
|
@@ -61,9 +66,10 @@ function toSnapshots(models: readonly ModelDef[]): ModelSnapshot[] {
|
|
|
61
66
|
});
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
export function createBackmatter<
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
export function createBackmatter<
|
|
70
|
+
const Models extends readonly ModelDef[],
|
|
71
|
+
const Blocks extends readonly BlockDef[] = [],
|
|
72
|
+
>(options: CreateBackmatterOptions<Models, Blocks>): BackmatterClient<Models, Blocks> {
|
|
67
73
|
const { models: modelDefs, blocks: blockDefs, richtext: richtextDefs, objects: objectDefs, taggedApiFetch, sync = true, ...rest } = options;
|
|
68
74
|
const config = resolveConfig(rest);
|
|
69
75
|
const models = toSnapshots(modelDefs);
|
|
@@ -128,7 +134,7 @@ export function createBackmatter<const Models extends readonly ModelDef[]>(
|
|
|
128
134
|
return {
|
|
129
135
|
listDocs: loader.listDocs,
|
|
130
136
|
getDoc: loader.getDoc,
|
|
131
|
-
query: createQuery(modelDefs, loader),
|
|
137
|
+
query: createQuery<Models, Blocks>(modelDefs, loader, blockDefs),
|
|
132
138
|
models: modelDefs,
|
|
133
139
|
config,
|
|
134
140
|
};
|