@backmatter/core 0.0.33 → 0.0.35
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 +448 -16
- package/src/index.ts +17 -1
- package/src/link-address.ts +141 -0
- package/src/link-problems.ts +274 -0
- package/src/media-fields.ts +6 -0
- package/src/model.ts +13 -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 +459 -78
- package/src/relation-match.ts +12 -0
- package/src/relation-problems.ts +189 -0
- package/src/schema-check.ts +260 -0
- package/src/sync-schema.ts +2 -0
- package/src/validate.ts +161 -22
- package/src/zod.ts +164 -16
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,11 @@ 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 { linkProblems } from "./link-problems";
|
|
140
|
+
export { isColorString } from "./color";
|
|
134
141
|
|
|
135
142
|
export type { FrontmatterJsonSchema } from "./zod";
|
|
136
143
|
export {
|
|
@@ -145,7 +152,7 @@ export {
|
|
|
145
152
|
// createBackmatter and the CLI live in `@backmatter/core/node`.
|
|
146
153
|
export type { BackmatterDoc, BackmatterDocSummary } from "./doc";
|
|
147
154
|
export { createQuery } from "./query";
|
|
148
|
-
export type { Query, ModelQuery,
|
|
155
|
+
export type { Query, ModelQuery, Where, DocLoader } from "./query";
|
|
149
156
|
export type { PreviewCheckout, PreviewSessionClaims, PreviewTicket } from "./preview-session";
|
|
150
157
|
export {
|
|
151
158
|
PREVIEW_COOKIE,
|
|
@@ -199,3 +206,12 @@ export {
|
|
|
199
206
|
type EnterRequest,
|
|
200
207
|
type EnterTarget,
|
|
201
208
|
} from "./enter";
|
|
209
|
+
|
|
210
|
+
export type { LinkSide } from "./link-address";
|
|
211
|
+
export {
|
|
212
|
+
externalLinkOk,
|
|
213
|
+
internalSuffixError,
|
|
214
|
+
joinInternalLink,
|
|
215
|
+
linkSide,
|
|
216
|
+
splitInternalLink,
|
|
217
|
+
} from "./link-address";
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// packages/core/src/link-address.ts
|
|
2
|
+
export type LinkSide = "internal" | "external" | "neither";
|
|
3
|
+
|
|
4
|
+
const SCHEME_RE = /^([a-z][a-z0-9+.-]*):/i;
|
|
5
|
+
const EXTERNAL_SCHEMES = new Set(["http", "https", "mailto", "tel"]);
|
|
6
|
+
|
|
7
|
+
export function linkSide(value: string): LinkSide {
|
|
8
|
+
const trimmed = value.trim();
|
|
9
|
+
const schemeMatch = trimmed.match(SCHEME_RE);
|
|
10
|
+
if (schemeMatch) {
|
|
11
|
+
const scheme = schemeMatch[1].toLowerCase();
|
|
12
|
+
if (EXTERNAL_SCHEMES.has(scheme)) {
|
|
13
|
+
return "external";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (trimmed.startsWith("/")) {
|
|
17
|
+
return "internal";
|
|
18
|
+
}
|
|
19
|
+
return "neither";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function splitInternalLink(value: string): {
|
|
23
|
+
path: string;
|
|
24
|
+
query: string;
|
|
25
|
+
anchor: string;
|
|
26
|
+
} {
|
|
27
|
+
const trimmed = value.trim();
|
|
28
|
+
const hashIndex = trimmed.indexOf("#");
|
|
29
|
+
let pathAndQuery: string;
|
|
30
|
+
let anchor: string;
|
|
31
|
+
if (hashIndex === -1) {
|
|
32
|
+
pathAndQuery = trimmed;
|
|
33
|
+
anchor = "";
|
|
34
|
+
} else {
|
|
35
|
+
pathAndQuery = trimmed.slice(0, hashIndex);
|
|
36
|
+
anchor = trimmed.slice(hashIndex + 1);
|
|
37
|
+
}
|
|
38
|
+
const queryIndex = pathAndQuery.indexOf("?");
|
|
39
|
+
if (queryIndex === -1) {
|
|
40
|
+
return { path: pathAndQuery, query: "", anchor };
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
path: pathAndQuery.slice(0, queryIndex),
|
|
44
|
+
query: pathAndQuery.slice(queryIndex + 1),
|
|
45
|
+
anchor,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function joinInternalLink(path: string, query: string, anchor: string): string {
|
|
50
|
+
let result = path;
|
|
51
|
+
if (query !== "") {
|
|
52
|
+
result += `?${query}`;
|
|
53
|
+
}
|
|
54
|
+
if (anchor !== "") {
|
|
55
|
+
result += `#${anchor}`;
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const QUERY_SUFFIX_RE = /^[^\s#]+$/;
|
|
61
|
+
const ANCHOR_SUFFIX_RE = /^[^\s#?]+$/;
|
|
62
|
+
|
|
63
|
+
export function internalSuffixError(
|
|
64
|
+
parts: { query: string; anchor: string },
|
|
65
|
+
present: { query: boolean; anchor: boolean },
|
|
66
|
+
): "query" | "anchor" | null {
|
|
67
|
+
if (present.query && !QUERY_SUFFIX_RE.test(parts.query)) {
|
|
68
|
+
return "query";
|
|
69
|
+
}
|
|
70
|
+
if (present.anchor && !ANCHOR_SUFFIX_RE.test(parts.anchor)) {
|
|
71
|
+
return "anchor";
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const MAILTO_RE = /^mailto:([^?#\s]*)(\?([^#\s]*))?$/i;
|
|
77
|
+
const TEL_BODY_RE = /^\+?[0-9 .()-]*$/;
|
|
78
|
+
|
|
79
|
+
function mailtoOk(trimmed: string): boolean {
|
|
80
|
+
const match = trimmed.match(MAILTO_RE);
|
|
81
|
+
if (!match) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const address = match[1];
|
|
85
|
+
const query = match[3];
|
|
86
|
+
if (match[2] !== undefined && (query === undefined || query === "")) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const atParts = address.split("@");
|
|
90
|
+
if (atParts.length !== 2 || atParts[0] === "" || atParts[1] === "") {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
const domain = atParts[1];
|
|
94
|
+
const dotIndex = domain.indexOf(".");
|
|
95
|
+
if (dotIndex <= 0 || dotIndex >= domain.length - 1) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function telOk(trimmed: string): boolean {
|
|
102
|
+
if (!trimmed.toLowerCase().startsWith("tel:")) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const body = trimmed.slice(trimmed.indexOf(":") + 1);
|
|
106
|
+
if (!TEL_BODY_RE.test(body) || !/\d/.test(body)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function httpOk(trimmed: string): boolean {
|
|
113
|
+
try {
|
|
114
|
+
const url = new URL(trimmed);
|
|
115
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
return url.hostname !== "";
|
|
119
|
+
} catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function externalLinkOk(value: string): boolean {
|
|
125
|
+
const trimmed = value.trim();
|
|
126
|
+
const schemeMatch = trimmed.match(SCHEME_RE);
|
|
127
|
+
if (!schemeMatch) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
const scheme = schemeMatch[1].toLowerCase();
|
|
131
|
+
if (scheme === "http" || scheme === "https") {
|
|
132
|
+
return httpOk(trimmed);
|
|
133
|
+
}
|
|
134
|
+
if (scheme === "mailto") {
|
|
135
|
+
return mailtoOk(trimmed);
|
|
136
|
+
}
|
|
137
|
+
if (scheme === "tel") {
|
|
138
|
+
return telOk(trimmed);
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// packages/core/src/link-problems.ts
|
|
2
|
+
import type { BlockInstance } from "./block";
|
|
3
|
+
import type { FieldSnapshot } from "./fields";
|
|
4
|
+
import {
|
|
5
|
+
externalLinkOk,
|
|
6
|
+
internalSuffixError,
|
|
7
|
+
linkSide,
|
|
8
|
+
splitInternalLink,
|
|
9
|
+
} from "./link-address";
|
|
10
|
+
import { matchesWhere } from "./relation-match";
|
|
11
|
+
import type { ModelSnapshot, SchemaSnapshot } from "./model";
|
|
12
|
+
import { routeFor } from "./routes";
|
|
13
|
+
|
|
14
|
+
type DocEntry = {
|
|
15
|
+
type: string;
|
|
16
|
+
slug: string;
|
|
17
|
+
frontmatter: Record<string, unknown>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function isBlankLinkValue(value: unknown): boolean {
|
|
21
|
+
return typeof value !== "string" || value.trim() === "";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function inlineFields(field: FieldSnapshot): Record<string, FieldSnapshot> | undefined {
|
|
25
|
+
if (!field.fields || Object.keys(field.fields).length === 0) return undefined;
|
|
26
|
+
return field.fields;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function objectFieldsFor(
|
|
30
|
+
schema: SchemaSnapshot,
|
|
31
|
+
field: FieldSnapshot,
|
|
32
|
+
): Record<string, FieldSnapshot> | undefined {
|
|
33
|
+
const inline = inlineFields(field);
|
|
34
|
+
if (inline) return inline;
|
|
35
|
+
if ((field.kind === "object" || field.kind === "array") && field.object) {
|
|
36
|
+
const def = schema.objects?.find((o) => o.name === field.object);
|
|
37
|
+
return def?.fields;
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function joinPath(prefix: string, segment: string): string {
|
|
43
|
+
return prefix ? `${prefix}.${segment}` : segment;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function modelsLabel(models: string[]): string {
|
|
47
|
+
return models.join(" or ");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function documentRouteMessage(path: string, models: string[] | undefined): string {
|
|
51
|
+
if (models && models.length > 0) {
|
|
52
|
+
return `${path} must be a route of ${modelsLabel(models)}`;
|
|
53
|
+
}
|
|
54
|
+
return `${path} must be a document route`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function buildRouteIndex(
|
|
58
|
+
schema: SchemaSnapshot,
|
|
59
|
+
docs: Array<{ type: string; slug: string; frontmatter: Record<string, unknown> }>,
|
|
60
|
+
modelsByType: Map<string, ModelSnapshot>,
|
|
61
|
+
): Map<string, DocEntry[]> {
|
|
62
|
+
const index = new Map<string, DocEntry[]>();
|
|
63
|
+
for (const doc of docs) {
|
|
64
|
+
const model = modelsByType.get(doc.type);
|
|
65
|
+
if (!model || model.external || model.route === false) continue;
|
|
66
|
+
const route = routeFor(model, doc.slug);
|
|
67
|
+
const row: DocEntry = {
|
|
68
|
+
type: doc.type,
|
|
69
|
+
slug: doc.slug,
|
|
70
|
+
frontmatter: doc.frontmatter,
|
|
71
|
+
};
|
|
72
|
+
const list = index.get(route);
|
|
73
|
+
if (list) {
|
|
74
|
+
list.push(row);
|
|
75
|
+
} else {
|
|
76
|
+
index.set(route, [row]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return index;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function validateInternalLink(
|
|
83
|
+
path: string,
|
|
84
|
+
field: FieldSnapshot,
|
|
85
|
+
value: string,
|
|
86
|
+
routeIndex: Map<string, DocEntry[]>,
|
|
87
|
+
errors: string[],
|
|
88
|
+
): void {
|
|
89
|
+
const parts = splitInternalLink(value);
|
|
90
|
+
const atPath = routeIndex.get(parts.path) ?? [];
|
|
91
|
+
const models = field.models;
|
|
92
|
+
let candidates = atPath;
|
|
93
|
+
if (models && models.length > 0) {
|
|
94
|
+
candidates = atPath.filter((d) => models.includes(d.type));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (candidates.length === 0) {
|
|
98
|
+
errors.push(documentRouteMessage(path, models));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const passing = candidates.filter((d) =>
|
|
103
|
+
matchesWhere({ slug: d.slug, frontmatter: d.frontmatter }, field.where),
|
|
104
|
+
);
|
|
105
|
+
if (passing.length === 0) {
|
|
106
|
+
errors.push(`${path} must match the field's filter`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const hashIndex = value.indexOf("#");
|
|
111
|
+
const preHash = hashIndex === -1 ? value : value.slice(0, hashIndex);
|
|
112
|
+
const suffixErr = internalSuffixError(
|
|
113
|
+
{ query: parts.query, anchor: parts.anchor },
|
|
114
|
+
{ query: preHash.includes("?"), anchor: hashIndex !== -1 },
|
|
115
|
+
);
|
|
116
|
+
if (suffixErr === "query") {
|
|
117
|
+
errors.push(`${path} has an invalid query`);
|
|
118
|
+
} else if (suffixErr === "anchor") {
|
|
119
|
+
errors.push(`${path} has an invalid anchor`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function checkLinkValue(
|
|
124
|
+
path: string,
|
|
125
|
+
field: FieldSnapshot,
|
|
126
|
+
value: unknown,
|
|
127
|
+
routeIndex: Map<string, DocEntry[]>,
|
|
128
|
+
errors: string[],
|
|
129
|
+
): void {
|
|
130
|
+
if (field.kind !== "link") return;
|
|
131
|
+
if (isBlankLinkValue(value)) return;
|
|
132
|
+
|
|
133
|
+
const side = linkSide(value as string);
|
|
134
|
+
const models = field.models;
|
|
135
|
+
|
|
136
|
+
if (field.to === "external" || (field.to === undefined && side === "external")) {
|
|
137
|
+
if (!externalLinkOk(value as string)) {
|
|
138
|
+
errors.push(`${path} must be an http(s) URL, email, or phone`);
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (field.to === "internal" || (field.to === undefined && side === "internal")) {
|
|
144
|
+
validateInternalLink(path, field, value as string, routeIndex, errors);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (field.to === undefined && side === "neither") {
|
|
149
|
+
errors.push(
|
|
150
|
+
`${path} must be a document route or an http(s) URL, email, or phone`,
|
|
151
|
+
);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (field.to === "internal" && side !== "internal") {
|
|
156
|
+
errors.push(documentRouteMessage(path, models));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function asBlockList(value: unknown): BlockInstance[] {
|
|
161
|
+
if (!Array.isArray(value)) return [];
|
|
162
|
+
return value.filter((item): item is BlockInstance => {
|
|
163
|
+
if (!item || typeof item !== "object") return false;
|
|
164
|
+
const row = item as Record<string, unknown>;
|
|
165
|
+
return typeof row.id === "string" && typeof row.type === "string";
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function walkFields(
|
|
170
|
+
schema: SchemaSnapshot,
|
|
171
|
+
pathPrefix: string,
|
|
172
|
+
fields: Record<string, FieldSnapshot>,
|
|
173
|
+
data: Record<string, unknown>,
|
|
174
|
+
routeIndex: Map<string, DocEntry[]>,
|
|
175
|
+
errors: string[],
|
|
176
|
+
): void {
|
|
177
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
178
|
+
const path = joinPath(pathPrefix, name);
|
|
179
|
+
const value = data[name];
|
|
180
|
+
|
|
181
|
+
if (field.kind === "link") {
|
|
182
|
+
checkLinkValue(path, field, value, routeIndex, errors);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (field.kind === "blocks") {
|
|
187
|
+
walkBlockInstances(schema, name, value, routeIndex, errors);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const nested = objectFieldsFor(schema, field);
|
|
192
|
+
if (!nested) continue;
|
|
193
|
+
|
|
194
|
+
if (field.kind === "array" && Array.isArray(value)) {
|
|
195
|
+
value.forEach((item, index) => {
|
|
196
|
+
if (!item || typeof item !== "object") return;
|
|
197
|
+
walkFields(
|
|
198
|
+
schema,
|
|
199
|
+
`${path}.${index}`,
|
|
200
|
+
nested,
|
|
201
|
+
item as Record<string, unknown>,
|
|
202
|
+
routeIndex,
|
|
203
|
+
errors,
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
} else if (field.kind === "object" && value && typeof value === "object") {
|
|
207
|
+
walkFields(
|
|
208
|
+
schema,
|
|
209
|
+
path,
|
|
210
|
+
nested,
|
|
211
|
+
value as Record<string, unknown>,
|
|
212
|
+
routeIndex,
|
|
213
|
+
errors,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function walkBlockInstances(
|
|
220
|
+
schema: SchemaSnapshot,
|
|
221
|
+
prefix: string,
|
|
222
|
+
blocks: unknown,
|
|
223
|
+
routeIndex: Map<string, DocEntry[]>,
|
|
224
|
+
errors: string[],
|
|
225
|
+
): void {
|
|
226
|
+
const catalog = schema.blocks ?? [];
|
|
227
|
+
const byName = new Map(catalog.map((b) => [b.name, b]));
|
|
228
|
+
asBlockList(blocks).forEach((block, index) => {
|
|
229
|
+
const def = byName.get(block.type);
|
|
230
|
+
if (!def) return;
|
|
231
|
+
const props =
|
|
232
|
+
block.props && typeof block.props === "object"
|
|
233
|
+
? (block.props as Record<string, unknown>)
|
|
234
|
+
: {};
|
|
235
|
+
walkFields(
|
|
236
|
+
schema,
|
|
237
|
+
`${prefix}[${index}]`,
|
|
238
|
+
def.fields,
|
|
239
|
+
props,
|
|
240
|
+
routeIndex,
|
|
241
|
+
errors,
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function walkDocument(
|
|
247
|
+
schema: SchemaSnapshot,
|
|
248
|
+
model: ModelSnapshot,
|
|
249
|
+
frontmatter: Record<string, unknown>,
|
|
250
|
+
routeIndex: Map<string, DocEntry[]>,
|
|
251
|
+
errors: string[],
|
|
252
|
+
): void {
|
|
253
|
+
walkFields(schema, "", model.fields, frontmatter, routeIndex, errors);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function linkProblems(input: {
|
|
257
|
+
schema: SchemaSnapshot;
|
|
258
|
+
docs: Array<{ type: string; slug: string; frontmatter: Record<string, unknown> }>;
|
|
259
|
+
}): Array<{ type: string; slug: string; errors: string[] }> {
|
|
260
|
+
const modelsByType = new Map(input.schema.models.map((m) => [m.type, m]));
|
|
261
|
+
const routeIndex = buildRouteIndex(input.schema, input.docs, modelsByType);
|
|
262
|
+
|
|
263
|
+
const rows: Array<{ type: string; slug: string; errors: string[] }> = [];
|
|
264
|
+
for (const doc of input.docs) {
|
|
265
|
+
const model = modelsByType.get(doc.type);
|
|
266
|
+
if (!model) continue;
|
|
267
|
+
const errors: string[] = [];
|
|
268
|
+
walkDocument(input.schema, model, doc.frontmatter, routeIndex, errors);
|
|
269
|
+
if (errors.length > 0) {
|
|
270
|
+
rows.push({ type: doc.type, slug: doc.slug, errors });
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return rows;
|
|
274
|
+
}
|
package/src/media-fields.ts
CHANGED
|
@@ -28,6 +28,12 @@ function collect(
|
|
|
28
28
|
if (typeof value === "string") {
|
|
29
29
|
const id = mediaIdFromUrl(value);
|
|
30
30
|
if (id) ids.push(id);
|
|
31
|
+
} else if (Array.isArray(value)) {
|
|
32
|
+
for (const entry of value) {
|
|
33
|
+
if (typeof entry !== "string") continue;
|
|
34
|
+
const id = mediaIdFromUrl(entry);
|
|
35
|
+
if (id) ids.push(id);
|
|
36
|
+
}
|
|
31
37
|
}
|
|
32
38
|
continue;
|
|
33
39
|
}
|
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,17 @@ 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
|
-
:
|
|
199
|
+
: F extends { kind: "media"; multiple: true }
|
|
200
|
+
? string[]
|
|
201
|
+
: F extends { options: infer O extends readonly [string, ...string[]] }
|
|
202
|
+
? O[number]
|
|
203
|
+
: string;
|
|
197
204
|
|
|
198
205
|
type IsRequired<F> = F extends { required?: infer R }
|
|
199
206
|
? [Exclude<R, undefined>] extends [true]
|
|
@@ -206,11 +213,11 @@ export type InferFrontmatter<M extends ModelDef> = Simplify<
|
|
|
206
213
|
{
|
|
207
214
|
[N in keyof M["fields"] as IsRequired<M["fields"][N]> extends true
|
|
208
215
|
? N
|
|
209
|
-
: never]:
|
|
216
|
+
: never]: ValueOfField<M["fields"][N]>;
|
|
210
217
|
} & {
|
|
211
218
|
[N in keyof M["fields"] as IsRequired<M["fields"][N]> extends true
|
|
212
219
|
? never
|
|
213
|
-
: N]?:
|
|
220
|
+
: N]?: ValueOfField<M["fields"][N]>;
|
|
214
221
|
}
|
|
215
222
|
>;
|
|
216
223
|
|
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
|
};
|