@backmatter/core 0.0.35 → 0.0.36

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.35",
3
+ "version": "0.0.36",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/src/fields.ts CHANGED
@@ -380,7 +380,7 @@ function link<const R extends boolean = false>(
380
380
  ): FieldDef<"link", R>;
381
381
  function link(opts?: LinkOptsBase<boolean> & {
382
382
  models?: readonly (string | LinkModelInput)[];
383
- where?: RelationWhere;
383
+ where?: unknown;
384
384
  }): FieldDef<"link", boolean> {
385
385
  if (opts?.to === "external") {
386
386
  if (opts.models !== undefined || opts.where !== undefined) {
package/src/index.ts CHANGED
@@ -67,6 +67,7 @@ export { defineSource } from "./source";
67
67
 
68
68
  export type { DocFormat } from "./doc-format";
69
69
  export { modelFormat, parseDoc, serializeDoc } from "./doc-format";
70
+ export { reserializeDoc } from "./reserialize";
70
71
 
71
72
  export type { BackmatterConfig, BackmatterDirs, DevBarHostConfig, ResolvedConfig } from "./config";
72
73
  export {
@@ -132,6 +133,9 @@ export {
132
133
  validateBlocks,
133
134
  validateContentTree,
134
135
  } from "./validate";
136
+ export type { Problem } from "./problem";
137
+ export type { ValidationIssue } from "./validation-issue";
138
+ export { describeValidationError } from "./validation-issue";
135
139
  export { coerceFieldValue, coerceDocValues, imageUrlFromUnknown } from "./coerce";
136
140
  export { documentLabel } from "./document-label";
137
141
  export { matchesWhere } from "./relation-match";
package/src/problem.ts ADDED
@@ -0,0 +1,15 @@
1
+ // packages/core/src/problem.ts
2
+
3
+ /** One failure from a workspace action, written for the person or agent who repairs it. */
4
+ export type Problem = {
5
+ code: string;
6
+ message: string;
7
+ fix?: string;
8
+ doc?: { type: string; slug: string };
9
+ /** Dotted path, such as "cover" or "blocks[2].image". */
10
+ field?: string;
11
+ path?: string;
12
+ /** A step only a person can take, such as approving a GitHub install. */
13
+ needsPerson?: { url: string; reason: string };
14
+ retryable: boolean;
15
+ };
@@ -0,0 +1,54 @@
1
+ // packages/core/src/reserialize.ts
2
+ //
3
+ // Write a document's new values over its previous text. Fields whose values
4
+ // didn't change keep their exact lines, so an edit to one field doesn't
5
+ // reformat the rest of the file in git.
6
+ import { isMap, parseDocument } from "yaml";
7
+ import { serializeDoc, type DocFormat } from "./doc-format";
8
+ import { parseMdx, type Frontmatter } from "./mdx";
9
+
10
+ const FM_RE = /^(---\r?\n)([\s\S]*?)(\r?\n---\r?\n?)([\s\S]*)$/;
11
+
12
+ function sameValue(a: unknown, b: unknown): boolean {
13
+ return JSON.stringify(a) === JSON.stringify(b);
14
+ }
15
+
16
+ export function reserializeDoc(
17
+ previous: string | null,
18
+ format: DocFormat,
19
+ frontmatter: Frontmatter,
20
+ body: string,
21
+ ): string {
22
+ if (format === "page" || previous === null) return serializeDoc(format, frontmatter, body);
23
+ const match = FM_RE.exec(previous);
24
+ if (!match) return serializeDoc(format, frontmatter, body);
25
+
26
+ let before: Frontmatter;
27
+ try {
28
+ before = parseMdx(previous).frontmatter;
29
+ } catch {
30
+ return serializeDoc(format, frontmatter, body);
31
+ }
32
+ const doc = parseDocument(match[2] ?? "");
33
+ if (doc.errors.length > 0 || !isMap(doc.contents)) {
34
+ return serializeDoc(format, frontmatter, body);
35
+ }
36
+ if (Object.keys(frontmatter).length === 0) return serializeDoc(format, frontmatter, body);
37
+
38
+ let changed = false;
39
+ for (const key of Object.keys(before)) {
40
+ if (!(key in frontmatter) || frontmatter[key] === undefined) {
41
+ doc.delete(key);
42
+ changed = true;
43
+ }
44
+ }
45
+ for (const [key, value] of Object.entries(frontmatter)) {
46
+ if (value === undefined) continue;
47
+ if (key in before && sameValue(before[key], value)) continue;
48
+ doc.set(key, value);
49
+ changed = true;
50
+ }
51
+
52
+ const yaml = changed ? doc.toString({ lineWidth: 0 }).replace(/\r?\n$/, "") : match[2];
53
+ return `${match[1]}${yaml}${match[3]}${body}`;
54
+ }
@@ -0,0 +1,61 @@
1
+ // packages/core/src/validation-issue.ts
2
+
3
+ export type ValidationIssue = {
4
+ field: string | null;
5
+ message: string;
6
+ fix: string | undefined;
7
+ };
8
+
9
+ const FIELD_LINE = /^([^\s:]+) (is required|must .+|has .+)$/;
10
+
11
+ function sentence(text: string): string {
12
+ return /[.!?]$/.test(text) ? text : `${text}.`;
13
+ }
14
+
15
+ function fixFor(field: string, rest: string): string | undefined {
16
+ if (rest === "is required") return `Add a value for ${field}.`;
17
+ if (rest.includes("media URL")) return "Use a URL from the media library.";
18
+ const relation = rest.match(/^must name a (\S+) document$/);
19
+ if (relation) return `Pick an existing ${relation[1]} document.`;
20
+ return undefined;
21
+ }
22
+
23
+ /**
24
+ * Turn one line from document validation into its field, a sentence, and a
25
+ * repair. Validation keeps emitting lines, because the CLI prints them.
26
+ */
27
+ export function describeValidationError(line: string): ValidationIssue {
28
+ const parse = line.match(/^(frontmatter|JSON): (.+)/);
29
+ if (parse) {
30
+ // A YAML error continues with a code snippet. The first line says what's wrong.
31
+ const detail = parse[2]!.replace(/:$/, "");
32
+ return parse[1] === "JSON"
33
+ ? {
34
+ field: null,
35
+ message: sentence(`The file isn't valid JSON: ${detail}`),
36
+ fix: "Fix the JSON syntax.",
37
+ }
38
+ : {
39
+ field: null,
40
+ message: sentence(`The frontmatter can't be read: ${detail}`),
41
+ fix: "Fix the YAML at the top of the file.",
42
+ };
43
+ }
44
+ const unknownBlock = line.match(/^(\S+): unknown type "(.+)"$/);
45
+ if (unknownBlock) {
46
+ return {
47
+ field: unknownBlock[1]!,
48
+ message: `${unknownBlock[1]} uses an unknown block type "${unknownBlock[2]}".`,
49
+ fix: "Use a block type from the schema.",
50
+ };
51
+ }
52
+ const fieldLine = line.match(FIELD_LINE);
53
+ if (fieldLine) {
54
+ return {
55
+ field: fieldLine[1]!,
56
+ message: sentence(line),
57
+ fix: fixFor(fieldLine[1]!, fieldLine[2]!),
58
+ };
59
+ }
60
+ return { field: null, message: sentence(line), fix: undefined };
61
+ }