@backmatter/next 0.0.42 → 0.0.44

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/dist/config.cjs CHANGED
@@ -81,6 +81,244 @@ var import_node_path4 = __toESM(require("node:path"));
81
81
  var import_node_fs2 = require("node:fs");
82
82
  var import_node_path2 = __toESM(require("node:path"));
83
83
 
84
+ // ../core/src/fields.ts
85
+ function isBlankString(value) {
86
+ return value.trim() === "";
87
+ }
88
+ function assertTextOptions(options) {
89
+ if (options.length === 0) {
90
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
91
+ }
92
+ const seen = new Set;
93
+ for (const option of options) {
94
+ if (typeof option !== "string" || isBlankString(option)) {
95
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
96
+ }
97
+ if (seen.has(option)) {
98
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
99
+ }
100
+ seen.add(option);
101
+ }
102
+ return options;
103
+ }
104
+ function assertNumberConstraints(opts) {
105
+ const { min, max, step } = opts;
106
+ if (min === undefined && max === undefined && step === undefined) {
107
+ return;
108
+ }
109
+ if (min === undefined || max === undefined) {
110
+ throw new Error("field.number min and max must be provided together");
111
+ }
112
+ if (!Number.isFinite(min) || !Number.isFinite(max)) {
113
+ throw new Error("field.number min and max must be finite");
114
+ }
115
+ if (min >= max) {
116
+ throw new Error("field.number min must be less than max");
117
+ }
118
+ if (step !== undefined) {
119
+ if (!Number.isFinite(step) || step <= 0 || step > max - min) {
120
+ throw new Error("field.number step must be finite, greater than 0, and at most max - min");
121
+ }
122
+ return { min, max, step };
123
+ }
124
+ return { min, max };
125
+ }
126
+ function assertLinkModelTypes(models) {
127
+ if (models.length === 0) {
128
+ throw new Error("field.link models must include at least one model");
129
+ }
130
+ const seen = new Set;
131
+ const types = [];
132
+ for (const entry of models) {
133
+ if (typeof entry === "string") {
134
+ if (isBlankString(entry)) {
135
+ throw new Error("field.link models must include at least one distinct model type");
136
+ }
137
+ if (seen.has(entry)) {
138
+ throw new Error("field.link models must be distinct model type names");
139
+ }
140
+ seen.add(entry);
141
+ types.push(entry);
142
+ continue;
143
+ }
144
+ if (entry.external) {
145
+ throw new Error("field.link cannot include an external model");
146
+ }
147
+ if (entry.route === false) {
148
+ throw new Error("field.link model must have a route");
149
+ }
150
+ if (seen.has(entry.type)) {
151
+ throw new Error("field.link models must be distinct model type names");
152
+ }
153
+ seen.add(entry.type);
154
+ types.push(entry.type);
155
+ }
156
+ return types;
157
+ }
158
+ function parseRelationWhere(value) {
159
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
160
+ return;
161
+ }
162
+ const where = {};
163
+ for (const [key, entry] of Object.entries(value)) {
164
+ if (typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean") {
165
+ where[key] = entry;
166
+ } else {
167
+ return;
168
+ }
169
+ }
170
+ return where;
171
+ }
172
+ function base(kind, opts) {
173
+ return {
174
+ kind,
175
+ ...opts?.required ? { required: opts.required } : {},
176
+ ...opts?.relationTo ? { relationTo: opts.relationTo } : {},
177
+ ...opts?.input ? { input: opts.input } : {},
178
+ ...opts?.options ? { options: opts.options } : {},
179
+ ...opts?.to ? { to: opts.to } : {},
180
+ ...opts?.models ? { models: opts.models } : {},
181
+ ...opts?.min !== undefined ? { min: opts.min } : {},
182
+ ...opts?.max !== undefined ? { max: opts.max } : {},
183
+ ...opts?.step !== undefined ? { step: opts.step } : {},
184
+ ...opts?.where ? { where: opts.where } : {},
185
+ ...opts?.multiple === true ? { multiple: true } : {}
186
+ };
187
+ }
188
+ function buildComposite(kind, def, opts) {
189
+ const required = opts?.required ? { required: opts.required } : {};
190
+ const input = opts?.input ? { input: opts.input } : {};
191
+ if (typeof def === "string") {
192
+ return { kind, object: def, ...required, ...input };
193
+ }
194
+ return { kind, object: def.name, fields: def.fields, ...required, ...input };
195
+ }
196
+ function objectField(def, opts) {
197
+ return buildComposite("object", def, opts);
198
+ }
199
+ function arrayField(def, opts) {
200
+ return buildComposite("array", def, opts);
201
+ }
202
+ function relation(to, opts) {
203
+ if (typeof to === "string") {
204
+ if (opts && "where" in opts && opts.where !== undefined) {
205
+ throw new Error("field.relation where requires a model target");
206
+ }
207
+ return base("relation", { ...opts, relationTo: to });
208
+ }
209
+ if (to.external) {
210
+ throw new Error("field.relation where cannot target an external model");
211
+ }
212
+ const where = parseRelationWhere(opts !== undefined && "where" in opts ? opts.where : undefined);
213
+ if (!where) {
214
+ throw new Error("field.relation where must be a plain object");
215
+ }
216
+ return base("relation", {
217
+ relationTo: to.type,
218
+ where,
219
+ required: opts?.required,
220
+ input: opts?.input
221
+ });
222
+ }
223
+ function link(opts) {
224
+ if (opts?.to === "external") {
225
+ if (opts.models !== undefined || opts.where !== undefined) {
226
+ throw new Error("field.link external cannot include models or where");
227
+ }
228
+ return base("link", {
229
+ to: "external",
230
+ required: opts.required,
231
+ input: opts.input
232
+ });
233
+ }
234
+ if (opts?.where !== undefined) {
235
+ const models = opts.models;
236
+ if (!models || models.length !== 1 || typeof models[0] === "string") {
237
+ throw new Error("field.link where requires a model");
238
+ }
239
+ const where = parseRelationWhere(opts.where);
240
+ if (!where) {
241
+ throw new Error("field.link where must be a plain object");
242
+ }
243
+ const modelTypes = assertLinkModelTypes(models);
244
+ return base("link", {
245
+ to: opts.to,
246
+ models: modelTypes,
247
+ where,
248
+ required: opts.required,
249
+ input: opts.input
250
+ });
251
+ }
252
+ if (opts?.models !== undefined) {
253
+ const modelTypes = assertLinkModelTypes(opts.models);
254
+ return base("link", {
255
+ to: opts.to,
256
+ models: modelTypes,
257
+ required: opts.required,
258
+ input: opts.input
259
+ });
260
+ }
261
+ return base("link", {
262
+ to: opts?.to,
263
+ required: opts?.required,
264
+ input: opts?.input
265
+ });
266
+ }
267
+ function described(make, optsIndex) {
268
+ return (...args) => {
269
+ const def = make(...args);
270
+ const opts = args[optsIndex];
271
+ const raw = opts && typeof opts === "object" ? opts.description : undefined;
272
+ if (raw === undefined)
273
+ return def;
274
+ if (typeof raw !== "string")
275
+ throw new Error("field description must be a string");
276
+ return raw.trim() ? { ...def, description: raw.trim() } : def;
277
+ };
278
+ }
279
+ var constructors = {
280
+ text: (opts) => {
281
+ if (opts?.options !== undefined) {
282
+ if (opts.input !== undefined) {
283
+ throw new Error("field.text options cannot be combined with input");
284
+ }
285
+ const options = assertTextOptions([...opts.options]);
286
+ return base("text", { required: opts.required, options });
287
+ }
288
+ return base("text", {
289
+ required: opts?.required,
290
+ ...opts?.input ? { input: opts.input } : {}
291
+ });
292
+ },
293
+ image: (opts) => base("image", opts),
294
+ media: (opts) => base("media", opts),
295
+ number: (opts) => {
296
+ const bounds = opts ? assertNumberConstraints(opts) : undefined;
297
+ if (bounds) {
298
+ return base("number", { ...opts, ...bounds });
299
+ }
300
+ return base("number", opts);
301
+ },
302
+ boolean: (opts) => base("boolean", opts),
303
+ relation,
304
+ link,
305
+ blocks: (opts) => base("blocks", opts),
306
+ object: objectField,
307
+ array: arrayField
308
+ };
309
+ var field = {
310
+ text: described(constructors.text, 0),
311
+ image: described(constructors.image, 0),
312
+ media: described(constructors.media, 0),
313
+ number: described(constructors.number, 0),
314
+ boolean: described(constructors.boolean, 0),
315
+ relation: described(constructors.relation, 1),
316
+ link: described(constructors.link, 0),
317
+ blocks: described(constructors.blocks, 0),
318
+ object: described(constructors.object, 1),
319
+ array: described(constructors.array, 1)
320
+ };
321
+
84
322
  // ../core/src/config.ts
85
323
  var DEFAULT_DIRS = {
86
324
  models: "models",
@@ -485,10 +723,10 @@ function registryAliases() {
485
723
  }
486
724
  function registryTurbopackAlias(appRoot, outDir = DEFAULT_OUT_DIR) {
487
725
  const alias = {};
488
- const base = import_node_path3.default.resolve(appRoot);
726
+ const base2 = import_node_path3.default.resolve(appRoot);
489
727
  for (const { specifier, file } of registryAliases()) {
490
728
  const abs = import_node_path3.default.resolve(appRoot, outDir, file);
491
- alias[specifier] = asRelative(import_node_path3.default.relative(base, abs).split(import_node_path3.default.sep).join("/"));
729
+ alias[specifier] = asRelative(import_node_path3.default.relative(base2, abs).split(import_node_path3.default.sep).join("/"));
492
730
  }
493
731
  return alias;
494
732
  }
@@ -500,8 +738,8 @@ function registryWebpackAlias(root, outDir = DEFAULT_OUT_DIR) {
500
738
  return alias;
501
739
  }
502
740
  function registryImportPaths(outDir = DEFAULT_OUT_DIR, baseUrl) {
503
- const base = !baseUrl || baseUrl === "." ? "." : baseUrl;
504
- const from = import_node_path3.default.resolve("/registry-root", base);
741
+ const base2 = !baseUrl || baseUrl === "." ? "." : baseUrl;
742
+ const from = import_node_path3.default.resolve("/registry-root", base2);
505
743
  const to = import_node_path3.default.resolve("/registry-root", outDir);
506
744
  const folder = asRelative(import_node_path3.default.relative(from, to).split(import_node_path3.default.sep).join("/"));
507
745
  const paths = {};
@@ -593,8 +831,8 @@ function innerIndent(source, open) {
593
831
  }
594
832
  const lineStart = source.lastIndexOf(`
595
833
  `, open) + 1;
596
- const base = source.slice(lineStart, open).match(/^[ \t]*/)?.[0] ?? "";
597
- return `${base} `;
834
+ const base2 = source.slice(lineStart, open).match(/^[ \t]*/)?.[0] ?? "";
835
+ return `${base2} `;
598
836
  }
599
837
  function looseParse(source) {
600
838
  const stripped = stripJsonComments(source).replace(/,\s*([}\]])/g, "$1");
package/dist/loader.cjs CHANGED
@@ -74,6 +74,244 @@ var import_node_path3 = __toESM(require("node:path"));
74
74
  var import_node_fs2 = require("node:fs");
75
75
  var import_node_path2 = __toESM(require("node:path"));
76
76
 
77
+ // ../core/src/fields.ts
78
+ function isBlankString(value) {
79
+ return value.trim() === "";
80
+ }
81
+ function assertTextOptions(options) {
82
+ if (options.length === 0) {
83
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
84
+ }
85
+ const seen = new Set;
86
+ for (const option of options) {
87
+ if (typeof option !== "string" || isBlankString(option)) {
88
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
89
+ }
90
+ if (seen.has(option)) {
91
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
92
+ }
93
+ seen.add(option);
94
+ }
95
+ return options;
96
+ }
97
+ function assertNumberConstraints(opts) {
98
+ const { min, max, step } = opts;
99
+ if (min === undefined && max === undefined && step === undefined) {
100
+ return;
101
+ }
102
+ if (min === undefined || max === undefined) {
103
+ throw new Error("field.number min and max must be provided together");
104
+ }
105
+ if (!Number.isFinite(min) || !Number.isFinite(max)) {
106
+ throw new Error("field.number min and max must be finite");
107
+ }
108
+ if (min >= max) {
109
+ throw new Error("field.number min must be less than max");
110
+ }
111
+ if (step !== undefined) {
112
+ if (!Number.isFinite(step) || step <= 0 || step > max - min) {
113
+ throw new Error("field.number step must be finite, greater than 0, and at most max - min");
114
+ }
115
+ return { min, max, step };
116
+ }
117
+ return { min, max };
118
+ }
119
+ function assertLinkModelTypes(models) {
120
+ if (models.length === 0) {
121
+ throw new Error("field.link models must include at least one model");
122
+ }
123
+ const seen = new Set;
124
+ const types = [];
125
+ for (const entry of models) {
126
+ if (typeof entry === "string") {
127
+ if (isBlankString(entry)) {
128
+ throw new Error("field.link models must include at least one distinct model type");
129
+ }
130
+ if (seen.has(entry)) {
131
+ throw new Error("field.link models must be distinct model type names");
132
+ }
133
+ seen.add(entry);
134
+ types.push(entry);
135
+ continue;
136
+ }
137
+ if (entry.external) {
138
+ throw new Error("field.link cannot include an external model");
139
+ }
140
+ if (entry.route === false) {
141
+ throw new Error("field.link model must have a route");
142
+ }
143
+ if (seen.has(entry.type)) {
144
+ throw new Error("field.link models must be distinct model type names");
145
+ }
146
+ seen.add(entry.type);
147
+ types.push(entry.type);
148
+ }
149
+ return types;
150
+ }
151
+ function parseRelationWhere(value) {
152
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
153
+ return;
154
+ }
155
+ const where = {};
156
+ for (const [key, entry] of Object.entries(value)) {
157
+ if (typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean") {
158
+ where[key] = entry;
159
+ } else {
160
+ return;
161
+ }
162
+ }
163
+ return where;
164
+ }
165
+ function base(kind, opts) {
166
+ return {
167
+ kind,
168
+ ...opts?.required ? { required: opts.required } : {},
169
+ ...opts?.relationTo ? { relationTo: opts.relationTo } : {},
170
+ ...opts?.input ? { input: opts.input } : {},
171
+ ...opts?.options ? { options: opts.options } : {},
172
+ ...opts?.to ? { to: opts.to } : {},
173
+ ...opts?.models ? { models: opts.models } : {},
174
+ ...opts?.min !== undefined ? { min: opts.min } : {},
175
+ ...opts?.max !== undefined ? { max: opts.max } : {},
176
+ ...opts?.step !== undefined ? { step: opts.step } : {},
177
+ ...opts?.where ? { where: opts.where } : {},
178
+ ...opts?.multiple === true ? { multiple: true } : {}
179
+ };
180
+ }
181
+ function buildComposite(kind, def, opts) {
182
+ const required = opts?.required ? { required: opts.required } : {};
183
+ const input = opts?.input ? { input: opts.input } : {};
184
+ if (typeof def === "string") {
185
+ return { kind, object: def, ...required, ...input };
186
+ }
187
+ return { kind, object: def.name, fields: def.fields, ...required, ...input };
188
+ }
189
+ function objectField(def, opts) {
190
+ return buildComposite("object", def, opts);
191
+ }
192
+ function arrayField(def, opts) {
193
+ return buildComposite("array", def, opts);
194
+ }
195
+ function relation(to, opts) {
196
+ if (typeof to === "string") {
197
+ if (opts && "where" in opts && opts.where !== undefined) {
198
+ throw new Error("field.relation where requires a model target");
199
+ }
200
+ return base("relation", { ...opts, relationTo: to });
201
+ }
202
+ if (to.external) {
203
+ throw new Error("field.relation where cannot target an external model");
204
+ }
205
+ const where = parseRelationWhere(opts !== undefined && "where" in opts ? opts.where : undefined);
206
+ if (!where) {
207
+ throw new Error("field.relation where must be a plain object");
208
+ }
209
+ return base("relation", {
210
+ relationTo: to.type,
211
+ where,
212
+ required: opts?.required,
213
+ input: opts?.input
214
+ });
215
+ }
216
+ function link(opts) {
217
+ if (opts?.to === "external") {
218
+ if (opts.models !== undefined || opts.where !== undefined) {
219
+ throw new Error("field.link external cannot include models or where");
220
+ }
221
+ return base("link", {
222
+ to: "external",
223
+ required: opts.required,
224
+ input: opts.input
225
+ });
226
+ }
227
+ if (opts?.where !== undefined) {
228
+ const models = opts.models;
229
+ if (!models || models.length !== 1 || typeof models[0] === "string") {
230
+ throw new Error("field.link where requires a model");
231
+ }
232
+ const where = parseRelationWhere(opts.where);
233
+ if (!where) {
234
+ throw new Error("field.link where must be a plain object");
235
+ }
236
+ const modelTypes = assertLinkModelTypes(models);
237
+ return base("link", {
238
+ to: opts.to,
239
+ models: modelTypes,
240
+ where,
241
+ required: opts.required,
242
+ input: opts.input
243
+ });
244
+ }
245
+ if (opts?.models !== undefined) {
246
+ const modelTypes = assertLinkModelTypes(opts.models);
247
+ return base("link", {
248
+ to: opts.to,
249
+ models: modelTypes,
250
+ required: opts.required,
251
+ input: opts.input
252
+ });
253
+ }
254
+ return base("link", {
255
+ to: opts?.to,
256
+ required: opts?.required,
257
+ input: opts?.input
258
+ });
259
+ }
260
+ function described(make, optsIndex) {
261
+ return (...args) => {
262
+ const def = make(...args);
263
+ const opts = args[optsIndex];
264
+ const raw = opts && typeof opts === "object" ? opts.description : undefined;
265
+ if (raw === undefined)
266
+ return def;
267
+ if (typeof raw !== "string")
268
+ throw new Error("field description must be a string");
269
+ return raw.trim() ? { ...def, description: raw.trim() } : def;
270
+ };
271
+ }
272
+ var constructors = {
273
+ text: (opts) => {
274
+ if (opts?.options !== undefined) {
275
+ if (opts.input !== undefined) {
276
+ throw new Error("field.text options cannot be combined with input");
277
+ }
278
+ const options = assertTextOptions([...opts.options]);
279
+ return base("text", { required: opts.required, options });
280
+ }
281
+ return base("text", {
282
+ required: opts?.required,
283
+ ...opts?.input ? { input: opts.input } : {}
284
+ });
285
+ },
286
+ image: (opts) => base("image", opts),
287
+ media: (opts) => base("media", opts),
288
+ number: (opts) => {
289
+ const bounds = opts ? assertNumberConstraints(opts) : undefined;
290
+ if (bounds) {
291
+ return base("number", { ...opts, ...bounds });
292
+ }
293
+ return base("number", opts);
294
+ },
295
+ boolean: (opts) => base("boolean", opts),
296
+ relation,
297
+ link,
298
+ blocks: (opts) => base("blocks", opts),
299
+ object: objectField,
300
+ array: arrayField
301
+ };
302
+ var field = {
303
+ text: described(constructors.text, 0),
304
+ image: described(constructors.image, 0),
305
+ media: described(constructors.media, 0),
306
+ number: described(constructors.number, 0),
307
+ boolean: described(constructors.boolean, 0),
308
+ relation: described(constructors.relation, 1),
309
+ link: described(constructors.link, 0),
310
+ blocks: described(constructors.blocks, 0),
311
+ object: described(constructors.object, 1),
312
+ array: described(constructors.array, 1)
313
+ };
314
+
77
315
  // ../core/src/config.ts
78
316
  var DEFAULT_DIRS = {
79
317
  models: "models",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backmatter/next",
3
- "version": "0.0.42",
3
+ "version": "0.0.44",
4
4
  "description": "Experimental Next.js adapter for Backmatter. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -47,10 +47,10 @@
47
47
  "test": "bun test"
48
48
  },
49
49
  "dependencies": {
50
- "@backmatter/core": "^0.0.36",
50
+ "@backmatter/core": "^0.0.38",
51
51
  "@mdx-js/mdx": "^3.1.1",
52
52
  "remark-gfm": "^4.0.1",
53
- "@backmatter/studio": "^0.0.19"
53
+ "@backmatter/studio": "^0.0.21"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "next": ">=16.3.0",
package/src/cms-proxy.ts CHANGED
@@ -43,10 +43,20 @@ const ALLOWED: Array<{ method: string; pattern: RegExp }> = [
43
43
  { method: "POST", pattern: new RegExp(`^${WS}/ai/threads/[^/]+/messages$`) },
44
44
  { method: "POST", pattern: new RegExp(`^${WS}/ai/threads/[^/]+/messages/[^/]+/changes/(keep-all|undo-all)$`) },
45
45
  { method: "POST", pattern: new RegExp(`^${WS}/ai/changes/[^/]+/(keep|undo)$`) },
46
+ { method: "POST", pattern: new RegExp(`^${WS}/ai/chat-files$`) },
47
+ { method: "GET", pattern: new RegExp(`^${WS}/ai/chat-files/[^/]+$`) },
48
+ { method: "GET", pattern: new RegExp(`^${WS}/ai/threads/[^/]+/files$`) },
46
49
  { method: "GET", pattern: /^\/preview\/session$/ },
47
50
  { method: "POST", pattern: /^\/preview\/session\/renew$/ },
48
51
  ];
49
52
 
53
+ const PASSED_RESPONSE_HEADERS = [
54
+ "content-type",
55
+ "content-disposition",
56
+ "content-security-policy",
57
+ "x-content-type-options",
58
+ ];
59
+
50
60
  export function cmsProxyAllowed(method: string, path: string): boolean {
51
61
  return ALLOWED.some((rule) => rule.method === method && rule.pattern.test(path));
52
62
  }
@@ -158,8 +168,11 @@ export function createCmsProxy(
158
168
  body: hasBody ? await req.arrayBuffer() : undefined,
159
169
  });
160
170
  const responseHeaders = new Headers();
161
- const type = res.headers.get("content-type");
162
- if (type) responseHeaders.set("Content-Type", type);
171
+ // A chat file is served on the site's origin, so its sandboxing must survive.
172
+ for (const name of PASSED_RESPONSE_HEADERS) {
173
+ const value = res.headers.get(name);
174
+ if (value) responseHeaders.set(name, value);
175
+ }
163
176
  responseHeaders.set("Cache-Control", "no-store");
164
177
  return new Response(res.body, { status: res.status, headers: responseHeaders });
165
178
  };
@@ -193,7 +193,19 @@ export function PreviewClient({
193
193
  if (target.closest("[data-backmatter-panel]")) return true;
194
194
  return target.closest("iframe") !== null;
195
195
  },
196
+ // A click outside a marked block still reaches the editor as a
197
+ // document-level pointer with its text.
198
+ includeUnmarked: true,
196
199
  onEditorGrab: (grab) => session.grab(grab),
200
+ onUnmarkedGrab: (grab) =>
201
+ session.grab({
202
+ blockId: null,
203
+ blockType: null,
204
+ blockIndex: null,
205
+ quote: grab.quote,
206
+ shift: grab.shift,
207
+ ...(grab.field ? { field: grab.field } : {}),
208
+ }),
197
209
  onDevbarGrab: (grab) => {
198
210
  window.dispatchEvent(new CustomEvent("backmatter:devbar-grab", { detail: grab }));
199
211
  },