editlet 0.0.0-stage → 0.1.0-alpha.0

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.
Files changed (146) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +113 -2
  3. package/dist/admin/config.d.ts +22 -0
  4. package/dist/admin/config.js +75 -0
  5. package/dist/admin/dist/assets/RichEditor-BlaCRrEX.css +1 -0
  6. package/dist/admin/dist/assets/RichEditor-IZKdt6MK.js +42 -0
  7. package/dist/admin/dist/assets/blank-line-CbGzlqBI.js +1 -0
  8. package/dist/admin/dist/assets/channel-CljeMIwA.js +1 -0
  9. package/dist/admin/dist/assets/db-CCMyPgQa.js +1 -0
  10. package/dist/admin/dist/assets/index-CWoJh4H8.js +257 -0
  11. package/dist/admin/dist/assets/index-DkRMnD_M.css +1 -0
  12. package/dist/admin/dist/assets/mdast-util-from-markdown-C79q1_d8.js +3 -0
  13. package/dist/admin/dist/assets/mdast-util-gfm-cM-_xFJE.js +19 -0
  14. package/dist/admin/dist/assets/micromark-extension-gfm-Wz6-oszS.js +1 -0
  15. package/dist/admin/dist/assets/micromark-util-classify-character-BgXaGAja.js +1 -0
  16. package/dist/admin/dist/assets/micromark-util-html-tag-name-B61T18yC.js +1 -0
  17. package/dist/admin/dist/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  18. package/dist/admin/dist/index.html +16 -0
  19. package/dist/admin/dist/preview.js +1 -0
  20. package/dist/admin/dist/window.js +1 -0
  21. package/dist/astro/content.d.ts +21 -0
  22. package/dist/astro/content.js +235 -0
  23. package/dist/astro/devServer.js +89 -0
  24. package/dist/astro/index.d.ts +15 -0
  25. package/dist/astro/index.js +98 -0
  26. package/dist/astro/nodeBridge.js +36 -0
  27. package/dist/astro/shared.js +10 -0
  28. package/dist/astro/stub/index.html +71 -0
  29. package/dist/auth/client.d.ts +24 -0
  30. package/dist/auth/client.js +120 -0
  31. package/dist/auth/handler.d.ts +16 -0
  32. package/dist/auth/handler.js +281 -0
  33. package/dist/auth/protocol.d.ts +25 -0
  34. package/dist/auth/protocol.js +3 -0
  35. package/dist/core/assets.d.ts +73 -0
  36. package/dist/core/assets.js +405 -0
  37. package/dist/core/devApi.d.ts +28 -0
  38. package/dist/core/devApi.js +3 -0
  39. package/dist/core/entryFile.d.ts +13 -0
  40. package/dist/core/entryFile.js +81 -0
  41. package/dist/core/fields.js +147 -0
  42. package/dist/core/generate.d.ts +3 -0
  43. package/dist/core/generate.js +70 -0
  44. package/dist/core/gitStyle.d.ts +92 -0
  45. package/dist/core/gitStyle.js +206 -0
  46. package/dist/core/index.d.ts +16 -0
  47. package/dist/core/index.js +16 -0
  48. package/dist/core/issues.d.ts +10 -0
  49. package/dist/core/issues.js +10 -0
  50. package/dist/core/paths.d.ts +31 -0
  51. package/dist/core/paths.js +114 -0
  52. package/dist/core/project.js +56 -0
  53. package/dist/core/references.d.ts +68 -0
  54. package/dist/core/references.js +103 -0
  55. package/dist/core/rename.d.ts +60 -0
  56. package/dist/core/rename.js +133 -0
  57. package/dist/core/schema.d.ts +117 -0
  58. package/dist/core/schema.js +299 -0
  59. package/dist/core/sha.d.ts +2 -0
  60. package/dist/core/sha.js +13 -0
  61. package/dist/core/source.d.ts +48 -0
  62. package/dist/core/source.js +57 -0
  63. package/dist/core/validate.d.ts +22 -0
  64. package/dist/core/validate.js +148 -0
  65. package/dist/core/walk.d.ts +23 -0
  66. package/dist/core/walk.js +94 -0
  67. package/dist/core/writes.d.ts +37 -0
  68. package/dist/core/writes.js +166 -0
  69. package/dist/github/base64.js +16 -0
  70. package/dist/github/cache.d.ts +34 -0
  71. package/dist/github/cache.js +126 -0
  72. package/dist/github/client.d.ts +38 -0
  73. package/dist/github/client.js +228 -0
  74. package/dist/github/codeowners.d.ts +23 -0
  75. package/dist/github/codeowners.js +96 -0
  76. package/dist/github/commitReader.d.ts +25 -0
  77. package/dist/github/commitReader.js +38 -0
  78. package/dist/github/deploy.d.ts +15 -0
  79. package/dist/github/deploy.js +33 -0
  80. package/dist/github/index.d.ts +9 -0
  81. package/dist/github/index.js +8 -0
  82. package/dist/github/repo.d.ts +95 -0
  83. package/dist/github/repo.js +375 -0
  84. package/dist/github/source.d.ts +115 -0
  85. package/dist/github/source.js +558 -0
  86. package/dist/loader/index.d.ts +27 -0
  87. package/dist/loader/index.js +151 -0
  88. package/dist/loader/protocol.d.ts +95 -0
  89. package/dist/loader/protocol.js +120 -0
  90. package/dist/loader/window/geometry.js +9 -0
  91. package/dist/next/EditletLoader.d.ts +5 -0
  92. package/dist/next/EditletLoader.js +33 -0
  93. package/dist/next/assetUrl.d.ts +9 -0
  94. package/dist/next/assetUrl.js +13 -0
  95. package/dist/next/buildGuard.js +26 -0
  96. package/dist/next/config.d.ts +18 -0
  97. package/dist/next/config.js +255 -0
  98. package/dist/next/devServer.js +70 -0
  99. package/dist/next/editletField.d.ts +7 -0
  100. package/dist/next/editletField.js +7 -0
  101. package/dist/next/index.d.ts +8 -0
  102. package/dist/next/index.js +6 -0
  103. package/dist/next/preview.d.ts +18 -0
  104. package/dist/next/preview.js +100 -0
  105. package/dist/next/previewCookie.d.ts +5 -0
  106. package/dist/next/previewCookie.js +72 -0
  107. package/dist/next/previewEnv.js +25 -0
  108. package/dist/next/previewRef.d.ts +8 -0
  109. package/dist/next/previewRef.js +17 -0
  110. package/dist/next/publicDir.js +65 -0
  111. package/dist/next/reader.d.ts +49 -0
  112. package/dist/next/reader.js +161 -0
  113. package/dist/next/stub/index.html +56 -0
  114. package/dist/node/devHandler.d.ts +6 -0
  115. package/dist/node/devHandler.js +430 -0
  116. package/dist/node/fs.js +37 -0
  117. package/dist/node/index.d.ts +3 -0
  118. package/dist/node/index.js +3 -0
  119. package/dist/node/project.d.ts +12 -0
  120. package/dist/node/project.js +43 -0
  121. package/package.json +187 -4
  122. package/skills/README.md +84 -0
  123. package/skills/editlet/SKILL.md +119 -0
  124. package/skills/editlet/references/content-format.md +191 -0
  125. package/skills/editlet/references/preview.md +75 -0
  126. package/skills/editlet/references/publishing.md +137 -0
  127. package/skills/editlet/references/schema-changes.md +127 -0
  128. package/skills/editlet/scripts/guard.mjs +1061 -0
  129. package/skills/editlet/scripts/link.mjs +193 -0
  130. package/skills/editlet/scripts/refs.mjs +1480 -0
  131. package/skills/editlet/scripts/schema.mjs +1340 -0
  132. package/skills/editlet/scripts/style.mjs +657 -0
  133. package/skills/editlet/scripts/types.mjs +525 -0
  134. package/skills/editlet/scripts/validate.mjs +724 -0
  135. package/skills/editlet-migrate/SKILL.md +92 -0
  136. package/skills/editlet-migrate/references/mapping.md +73 -0
  137. package/skills/editlet-migrate/references/sources.md +70 -0
  138. package/skills/editlet-setup/SKILL.md +108 -0
  139. package/skills/editlet-setup/references/astro.md +83 -0
  140. package/skills/editlet-setup/references/doctor.md +89 -0
  141. package/skills/editlet-setup/references/next.md +140 -0
  142. package/skills/editlet-setup/references/preview.md +54 -0
  143. package/skills/editlet-setup/references/production.md +153 -0
  144. package/skills/editlet-setup/references/roles.md +238 -0
  145. package/skills/editlet-setup/references/skills.md +99 -0
  146. package/skills/editlet-setup/scripts/doctor.mjs +1378 -0
@@ -0,0 +1,68 @@
1
+ /**
2
+ * References between entries: which fields hold them, who points at an entry, and replacing `collection/slug`
3
+ * values with the entries themselves, a bounded number of levels deep.
4
+ */
5
+ import { Collection, Field, Schema } from "./schema.js";
6
+ import { Entry } from "./validate.js";
7
+ export type ReferenceField = Extract<Field, {
8
+ type: 'reference';
9
+ }>;
10
+ /** Deepest `depth` the readers accept. */
11
+ export declare const MAX_REFERENCE_DEPTH = 10;
12
+ /**
13
+ * The `reference` fields of a collection, nested ones inside `object` and `blocks` too, in schema order. The key is
14
+ * the field's path in the schema tree joined by dots (`content.survey.survey` for a block kind's field).
15
+ */
16
+ export declare function referenceFields(collection: Collection): [string, ReferenceField][];
17
+ /** True when some reference field of `collection` (nested ones too) may point at `target`. */
18
+ export declare function canReference(collection: Collection, target: string): boolean;
19
+ /**
20
+ * Every `collection/slug` value in `data`, in field order and list order, nested ones too. `field` is the value's dot
21
+ * path in the data (`author`, `content.2.survey`). Non-string values are skipped.
22
+ */
23
+ export declare function entryReferences(collection: Collection, data: Record<string, unknown>): {
24
+ field: string;
25
+ ref: string;
26
+ }[];
27
+ /**
28
+ * `data` with every reference to `from` replaced by `to` (rename), or removed when `to` is `null`: a list loses the
29
+ * item (and the key when it is left empty), a single field loses its key. At any depth (inside `object` and `blocks`). `null` when nothing changed.
30
+ */
31
+ export declare function replaceReference(collection: Collection, data: Record<string, unknown>, from: string, to: string | null): Record<string, unknown> | null;
32
+ export interface Referrer<E extends Entry = Entry> {
33
+ entry: E;
34
+ /** The fields of `entry` that hold the reference, in field order: dot paths for nested ones (`content.2.survey`). */
35
+ fields: string[];
36
+ }
37
+ /** Entries with a reference to `ref`, in the order of `entries`. An entry pointing at itself counts. */
38
+ export declare function findReferrers<E extends Entry>(schema: Schema, entries: Iterable<E>, ref: string): Referrer<E>[];
39
+ /** Throws `RangeError` unless `depth` is a whole number from 0 to `MAX_REFERENCE_DEPTH`. */
40
+ export declare function checkReferenceDepth(depth: number): void;
41
+ /**
42
+ * A function that replaces reference values with the entries `lookup` finds, `depth` levels deep: at `depth` 1 the
43
+ * entry's own references become entries (whose references stay strings), and so on. A reference `lookup` does not
44
+ * find (a hidden draft) is dropped: a list loses the item, a single field loses the key. A cycle (A → B → A) just
45
+ * repeats until the depth runs out, so the result is always a finite tree. Results are cached by entry and depth,
46
+ * so one resolver shares subtrees between calls; build a new one when the entries change.
47
+ */
48
+ export declare function createResolver<E extends Entry>(schema: Schema, lookup: (ref: string) => E | undefined): (entry: E, depth: number) => E;
49
+ type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
50
+ /**
51
+ * The collection a value type refers to: `` `section/${string}` `` gives `'section'`. Only the whole template
52
+ * counts, so a select option value like `'post/x'` is not a reference.
53
+ */
54
+ type TargetOf<M, V> = V extends `${infer C}/${string}` ? C extends keyof M & string ? `${C}/${string}` extends V ? C : never : never : never;
55
+ type ResolveRef<M, V, D extends number> = V extends unknown ? [TargetOf<M, V>] extends [never] ? V : number extends D ? V | Resolved<M, M[TargetOf<M, V>], D> : Resolved<M, M[TargetOf<M, V>], Prev[D]> : never;
56
+ /** A list item: a reference becomes an entry, an object (an `object` item or a block) is resolved inside. */
57
+ type ResolveItem<M, V, D extends number> = V extends string ? ResolveRef<M, V, D> : V extends readonly unknown[] ? ResolveValue<M, V, D> : V extends object ? ResolvedData<M, V, D> : V;
58
+ type ResolveValue<M, V, D extends number> = V extends readonly (infer I)[] ? ResolveItem<M, I, D>[] : V extends string // A single reference to a hidden draft is dropped, so a resolved single reference may be missing.
59
+ ? ResolveRef<M, V, D> | ([TargetOf<M, V>] extends [never] ? never : undefined) : V extends object ? ResolvedData<M, V, D> : V;
60
+ /**
61
+ * The entry type `E` (an `EntryMap` value) after reading it with `depth: D`: every reference field becomes the
62
+ * referenced entry type, resolved with `D - 1`; at `D` 0 the type is `E` itself. With a non-literal `number` depth
63
+ * each level is "entry or `collection/slug`".
64
+ */
65
+ export type Resolved<M, E, D extends number = 1> = D extends 0 ? E : { [K in keyof E]: K extends 'data' ? ResolvedData<M, E[K], D> : E[K]; };
66
+ /** `data` of a resolved entry; keeps each field's `?`. */
67
+ export type ResolvedData<M, T, D extends number> = { [F in keyof T]: ResolveValue<M, T[F], D>; };
68
+ export {};
@@ -0,0 +1,103 @@
1
+ import { allFields, mapFields, visitFields } from "./walk.js";
2
+ // core/references.ts
3
+ /** Deepest `depth` the readers accept. */
4
+ const MAX_REFERENCE_DEPTH = 10;
5
+ /**
6
+ * The `reference` fields of a collection, nested ones inside `object` and `blocks` too, in schema order. The key is
7
+ * the field's path in the schema tree joined by dots (`content.survey.survey` for a block kind's field).
8
+ */
9
+ function referenceFields(collection) {
10
+ return allFields(collection.fields).filter((e) => e[1].type === "reference").map(([path, field]) => [path.join("."), field]);
11
+ }
12
+ /** True when some reference field of `collection` (nested ones too) may point at `target`. */
13
+ function canReference(collection, target) {
14
+ return referenceFields(collection).some(([, f]) => f.collections.includes(target));
15
+ }
16
+ /**
17
+ * Every `collection/slug` value in `data`, in field order and list order, nested ones too. `field` is the value's dot
18
+ * path in the data (`author`, `content.2.survey`). Non-string values are skipped.
19
+ */
20
+ function entryReferences(collection, data) {
21
+ const out = [];
22
+ visitFields(collection.fields, data, (field, value, at) => {
23
+ if (field.type !== "reference") return;
24
+ for (const ref of Array.isArray(value) ? value : [value]) if (typeof ref === "string" && ref !== "") out.push({
25
+ field: at.join("."),
26
+ ref
27
+ });
28
+ });
29
+ return out;
30
+ }
31
+ /**
32
+ * `data` with every reference to `from` replaced by `to` (rename), or removed when `to` is `null`: a list loses the
33
+ * item (and the key when it is left empty), a single field loses its key. At any depth (inside `object` and `blocks`). `null` when nothing changed.
34
+ */
35
+ function replaceReference(collection, data, from, to) {
36
+ let changed = false;
37
+ const out = mapFields(collection.fields, data, (field, value) => {
38
+ if (field.type !== "reference") return value;
39
+ if (Array.isArray(value)) {
40
+ if (!value.includes(from)) return value;
41
+ changed = true;
42
+ if (to !== null) return value.map((r) => r === from ? to : r);
43
+ const rest = value.filter((r) => r !== from);
44
+ return rest.length > 0 ? rest : void 0;
45
+ }
46
+ if (value !== from) return value;
47
+ changed = true;
48
+ return to ?? void 0;
49
+ });
50
+ return changed ? out : null;
51
+ }
52
+ /** Entries with a reference to `ref`, in the order of `entries`. An entry pointing at itself counts. */
53
+ function findReferrers(schema, entries, ref) {
54
+ const out = [];
55
+ for (const entry of entries) {
56
+ if (!Object.hasOwn(schema.collections, entry.collection)) continue;
57
+ const fields = entryReferences(schema.collections[entry.collection], entry.data).filter((r) => r.ref === ref).map((r) => r.field);
58
+ if (fields.length > 0) out.push({
59
+ entry,
60
+ fields: [...new Set(fields)]
61
+ });
62
+ }
63
+ return out;
64
+ }
65
+ /** Throws `RangeError` unless `depth` is a whole number from 0 to `MAX_REFERENCE_DEPTH`. */
66
+ function checkReferenceDepth(depth) {
67
+ if (!Number.isInteger(depth) || depth < 0 || depth > 10) throw new RangeError(`depth must be a whole number from 0 to 10, got ${depth}`);
68
+ }
69
+ /**
70
+ * A function that replaces reference values with the entries `lookup` finds, `depth` levels deep: at `depth` 1 the
71
+ * entry's own references become entries (whose references stay strings), and so on. A reference `lookup` does not
72
+ * find (a hidden draft) is dropped: a list loses the item, a single field loses the key. A cycle (A → B → A) just
73
+ * repeats until the depth runs out, so the result is always a finite tree. Results are cached by entry and depth,
74
+ * so one resolver shares subtrees between calls; build a new one when the entries change.
75
+ */
76
+ function createResolver(schema, lookup) {
77
+ const memo = /* @__PURE__ */ new Map();
78
+ const resolve = (entry, depth) => {
79
+ if (depth === 0 || !Object.hasOwn(schema.collections, entry.collection)) return entry;
80
+ const key = `${depth}:${entry.collection}/${entry.slug}`;
81
+ const cached = memo.get(key);
82
+ if (cached) return cached;
83
+ const target = (ref) => {
84
+ const found = typeof ref === "string" ? lookup(ref) : void 0;
85
+ return found === void 0 ? void 0 : resolve(found, depth - 1);
86
+ };
87
+ const data = mapFields(schema.collections[entry.collection].fields, entry.data, (field, value) => {
88
+ if (field.type !== "reference") return value;
89
+ return Array.isArray(value) ? value.flatMap((ref) => target(ref) ?? []) : target(value);
90
+ });
91
+ const out = {
92
+ ...entry,
93
+ data
94
+ };
95
+ memo.set(key, out);
96
+ return out;
97
+ };
98
+ return (entry, depth) => {
99
+ checkReferenceDepth(depth);
100
+ return resolve(entry, depth);
101
+ };
102
+ }
103
+ export { MAX_REFERENCE_DEPTH, canReference, checkReferenceDepth, createResolver, entryReferences, findReferrers, referenceFields, replaceReference };
@@ -0,0 +1,60 @@
1
+ import { Schema } from "./schema.js";
2
+ /** One entry file as the planner needs it; `E` carries the caller's own fields (sha, key order, text) through. */
3
+ export interface PlanEntry {
4
+ collection: string;
5
+ slug: string;
6
+ data: Record<string, unknown>;
7
+ body: string;
8
+ }
9
+ /** An entry to write: `entry` is the input it comes from; `path` is where it goes (new for the renamed entry). */
10
+ export interface EntryRewrite<E extends PlanEntry> {
11
+ entry: E;
12
+ path: string;
13
+ data: Record<string, unknown>;
14
+ body: string;
15
+ /** Why it changes: `renamed` (the entry itself), `references` (a reference to it), `images` (an image `src`). */
16
+ reasons: ('renamed' | 'references' | 'images')[];
17
+ /**
18
+ * The values that change, as text: a reference (`question/role` → `question/team-role`, or `to: null` when it is
19
+ * removed) and image sources (`role/a.webp` → `team-role/a.webp`). A text editor can try exactly these edits and
20
+ * check the result against `data` and `body`.
21
+ */
22
+ replacements: {
23
+ from: string;
24
+ to: string | null;
25
+ }[];
26
+ }
27
+ export interface RenamePlan<E extends PlanEntry> {
28
+ from: string;
29
+ to: string;
30
+ /** The renamed entry at its new path first, then every other entry that changes, in input order. */
31
+ write: EntryRewrite<E>[];
32
+ /** Files to delete: the old entry file. Moved assets' old paths are in `move`. */
33
+ remove: string[];
34
+ /** Files of the entry's folder to move (read `from`, write `to`, delete `from`). */
35
+ move: {
36
+ from: string;
37
+ to: string;
38
+ }[];
39
+ }
40
+ export interface RemovePlan<E extends PlanEntry> {
41
+ ref: string;
42
+ /** Entries whose references to `ref` are removed. */
43
+ write: EntryRewrite<E>[];
44
+ /** The entry file. Its images stay, as with a plain delete. */
45
+ remove: string[];
46
+ }
47
+ /**
48
+ * Plans renaming `ref` (`collection/slug`) to the slug `to`: the entry moves to `<to>.md`, the files of its folder
49
+ * `<slug>/` move to `<to>/`, every reference to it (in `entries`, at any depth, the entry's own included) points at
50
+ * the new ref, and every image `src` (in `entries`) that pointed into the moved folder points at the new path.
51
+ * `files` are the repository paths that exist (to find the folder's files and to check the new slug is free).
52
+ * Throws `EditletError` for a singleton, an invalid or taken slug, or an unknown entry.
53
+ */
54
+ export declare function planRename<E extends PlanEntry>(schema: Schema, entries: E[], files: Iterable<string>, ref: string, to: string): RenamePlan<E>;
55
+ /**
56
+ * Plans deleting `ref` with every reference to it removed from `entries` (a list loses the item, a single field its
57
+ * key; at any depth). The rewritten entries may now break the schema (a required reference, `min`): validate them
58
+ * before committing. Throws `EditletError` for an unknown entry.
59
+ */
60
+ export declare function planRemove<E extends PlanEntry>(schema: Schema, entries: E[], ref: string): RemovePlan<E>;
@@ -0,0 +1,133 @@
1
+ import { applyImageEdits, findImageRefs } from "./assets.js";
2
+ import { EditletError } from "./issues.js";
3
+ import { entryFilePath, isValidSlug, refKey, relativeAssetSrc } from "./paths.js";
4
+ import { replaceReference } from "./references.js";
5
+ // core/rename.ts
6
+ /**
7
+ * Renaming an entry's slug and deleting an entry, with every reference to it rewritten in the same change (P21).
8
+ * Pure planning: the caller reads files, serializes the rewritten entries and commits (admin) or writes (skill).
9
+ */
10
+ const fail = (message) => {
11
+ throw new EditletError([{
12
+ path: "",
13
+ message
14
+ }]);
15
+ };
16
+ function entryOf(schema, entries, ref) {
17
+ const i = ref.indexOf("/");
18
+ const collection = ref.slice(0, i);
19
+ const slug = ref.slice(i + 1);
20
+ if (!Object.hasOwn(schema.collections, collection)) fail(`Unknown collection "${collection}"`);
21
+ return entries.find((e) => e.collection === collection && e.slug === slug) ?? fail(`Entry not found: ${ref}`);
22
+ }
23
+ /**
24
+ * Plans renaming `ref` (`collection/slug`) to the slug `to`: the entry moves to `<to>.md`, the files of its folder
25
+ * `<slug>/` move to `<to>/`, every reference to it (in `entries`, at any depth, the entry's own included) points at
26
+ * the new ref, and every image `src` (in `entries`) that pointed into the moved folder points at the new path.
27
+ * `files` are the repository paths that exist (to find the folder's files and to check the new slug is free).
28
+ * Throws `EditletError` for a singleton, an invalid or taken slug, or an unknown entry.
29
+ */
30
+ function planRename(schema, entries, files, ref, to) {
31
+ const entry = entryOf(schema, entries, ref);
32
+ const { collection, slug } = entry;
33
+ if (schema.collections[collection].singleton) fail("A singleton cannot be renamed: its slug is always \"index\"");
34
+ if (!isValidSlug(to)) fail(`Invalid slug "${to}": use lowercase letters, digits and single hyphens`);
35
+ if (to === slug) fail("The new slug is the same as the old one");
36
+ const oldPath = entryFilePath(schema, collection, slug);
37
+ const newPath = entryFilePath(schema, collection, to);
38
+ const oldDir = `${schema.contentDir}/${collection}/${slug}/`;
39
+ const newDir = `${schema.contentDir}/${collection}/${to}/`;
40
+ const paths = [...files];
41
+ if (paths.some((p) => p === newPath || p.startsWith(newDir))) fail(`${collection}/${to} is taken`);
42
+ const move = paths.filter((p) => p.startsWith(oldDir)).map((p) => ({
43
+ from: p,
44
+ to: newDir + p.slice(oldDir.length)
45
+ }));
46
+ const moved = new Map(move.map((m) => [m.from, m.to]));
47
+ const newRef = refKey(collection, to);
48
+ const write = [];
49
+ for (const e of entries) {
50
+ const isSelf = e === entry;
51
+ const path = isSelf ? newPath : entryFilePath(schema, e.collection, e.slug);
52
+ const def = schema.collections[e.collection];
53
+ if (!def) continue;
54
+ const reasons = isSelf ? ["renamed"] : [];
55
+ const replacements = [];
56
+ let data = e.data;
57
+ const replaced = replaceReference(def, data, ref, newRef);
58
+ if (replaced) {
59
+ data = replaced;
60
+ reasons.push("references");
61
+ replacements.push({
62
+ from: ref,
63
+ to: newRef
64
+ });
65
+ }
66
+ const edits = findImageRefs(schema, def, isSelf ? oldPath : path, data, e.body).flatMap((use) => {
67
+ const target = use.path === null ? void 0 : moved.get(use.path);
68
+ return target === void 0 ? [] : [{
69
+ use,
70
+ src: relativeAssetSrc(path, target)
71
+ }];
72
+ });
73
+ let body = e.body;
74
+ if (edits.length > 0) {
75
+ ({data, body} = applyImageEdits(data, body, edits));
76
+ reasons.push("images");
77
+ for (const { use, src } of edits) if (!replacements.some((r) => r.from === use.src)) replacements.push({
78
+ from: use.src,
79
+ to: src
80
+ });
81
+ }
82
+ if (reasons.length > 0) write.push({
83
+ entry: e,
84
+ path,
85
+ data,
86
+ body,
87
+ reasons,
88
+ replacements
89
+ });
90
+ }
91
+ write.sort((a, b) => Number(b.entry === entry) - Number(a.entry === entry));
92
+ return {
93
+ from: ref,
94
+ to: newRef,
95
+ write,
96
+ remove: [oldPath],
97
+ move
98
+ };
99
+ }
100
+ /**
101
+ * Plans deleting `ref` with every reference to it removed from `entries` (a list loses the item, a single field its
102
+ * key; at any depth). The rewritten entries may now break the schema (a required reference, `min`): validate them
103
+ * before committing. Throws `EditletError` for an unknown entry.
104
+ */
105
+ function planRemove(schema, entries, ref) {
106
+ const entry = entryOf(schema, entries, ref);
107
+ const write = [];
108
+ for (const e of entries) {
109
+ if (e === entry) continue;
110
+ const def = schema.collections[e.collection];
111
+ const data = def ? replaceReference(def, e.data, ref, null) : null;
112
+ if (data) {
113
+ const path = entryFilePath(schema, e.collection, e.slug);
114
+ write.push({
115
+ entry: e,
116
+ path,
117
+ data,
118
+ body: e.body,
119
+ reasons: ["references"],
120
+ replacements: [{
121
+ from: ref,
122
+ to: null
123
+ }]
124
+ });
125
+ }
126
+ }
127
+ return {
128
+ ref,
129
+ write,
130
+ remove: [entryFilePath(schema, entry.collection, entry.slug)]
131
+ };
132
+ }
133
+ export { planRemove, planRename };
@@ -0,0 +1,117 @@
1
+ export type FieldType = 'string' | 'text' | 'markdown' | 'boolean' | 'select' | 'number' | 'datetime' | 'json' | 'url' | 'color' | 'image' | 'reference' | 'object' | 'blocks';
2
+ interface Base {
3
+ label: string;
4
+ hint?: string;
5
+ required?: boolean;
6
+ }
7
+ export type Field = (Base & {
8
+ type: 'string';
9
+ list?: boolean;
10
+ default?: string | string[];
11
+ }) | (Base & {
12
+ type: 'text';
13
+ rows?: number;
14
+ default?: string;
15
+ }) | (Base & {
16
+ type: 'markdown';
17
+ }) | (Base & {
18
+ type: 'boolean';
19
+ default?: boolean;
20
+ labels?: {
21
+ true: string;
22
+ false: string;
23
+ };
24
+ }) | (Base & {
25
+ type: 'select';
26
+ options: {
27
+ label: string;
28
+ value: string;
29
+ }[];
30
+ multiple?: boolean;
31
+ default?: string | string[];
32
+ }) | (Base & {
33
+ type: 'number';
34
+ min?: number;
35
+ max?: number;
36
+ step?: number;
37
+ integer?: boolean;
38
+ default?: number;
39
+ }) | (Base & {
40
+ type: 'datetime';
41
+ dateOnly?: boolean;
42
+ defaultNow?: boolean;
43
+ }) | (Base & {
44
+ type: 'json';
45
+ }) | (Base & {
46
+ type: 'url';
47
+ }) | (Base & {
48
+ type: 'color';
49
+ default?: string;
50
+ }) | (Base & {
51
+ type: 'image';
52
+ }) | (Base & {
53
+ type: 'reference';
54
+ collections: string[];
55
+ multiple?: boolean;
56
+ min?: number;
57
+ max?: number;
58
+ }) | (Base & {
59
+ type: 'object';
60
+ fields: Record<string, Field>;
61
+ list?: boolean;
62
+ min?: number;
63
+ max?: number;
64
+ }) | (Base & {
65
+ type: 'blocks';
66
+ blocks: Record<string, BlockDef>;
67
+ min?: number;
68
+ max?: number;
69
+ });
70
+ /** One kind of block of a `blocks` field: its items carry `block: <key>` plus these fields. */
71
+ export interface BlockDef {
72
+ label: string;
73
+ fields: Record<string, Field>;
74
+ }
75
+ /** Key that names the kind of a `blocks` item; reserved as a field key inside blocks. */
76
+ export declare const BLOCK_KEY = "block";
77
+ export interface Collection {
78
+ label: string;
79
+ singleton?: boolean;
80
+ titleField?: string;
81
+ slugFrom?: string;
82
+ fields: Record<string, Field>;
83
+ }
84
+ /** How the admin and the agent name branches, commits and pull requests. Every key but `baseBranch` is optional. */
85
+ export interface GitConfig {
86
+ /** The branch the admin edits and opens pull requests against. */
87
+ baseBranch: string;
88
+ /** Prefix of every branch Editlet creates and lists, ending in `/`. Default `editlet/`. */
89
+ branchPrefix?: string;
90
+ /** Commit message template, one line; placeholders `COMMIT_PLACEHOLDERS`. Default `{Action} {target}`. */
91
+ commitMessage?: string;
92
+ pullRequest?: {
93
+ /** Title template; placeholders `TITLE_PLACEHOLDERS`. Default `Update {branch}`. */
94
+ title?: string;
95
+ /** Labels added to a new pull request. Default `["editlet"]`; `[]` adds none. */
96
+ labels?: string[];
97
+ /** Open new pull requests as drafts. Default `false`. */
98
+ draft?: boolean;
99
+ };
100
+ }
101
+ export interface Schema {
102
+ /** Editor hint (JSON Schema URL); ignored by Editlet. */
103
+ $schema?: string;
104
+ contentDir: string;
105
+ git: GitConfig;
106
+ collections: Record<string, Collection>;
107
+ }
108
+ /** Placeholders of `git.commitMessage`. */
109
+ export declare const COMMIT_PLACEHOLDERS: readonly ['action', 'Action', 'collection', 'slug', 'target'];
110
+ /** Placeholders of `git.pullRequest.title`. */
111
+ export declare const TITLE_PLACEHOLDERS: readonly ['branch', 'name', 'login'];
112
+ export declare function isRefPath(value: string): boolean;
113
+ export declare function loadSchema(input: unknown): Schema;
114
+ /** Parses schema JSON text; issues (bad JSON or bad schema) carry `file`. */
115
+ export declare function parseSchemaText(text: string, file: string): Schema;
116
+ export declare function markdownFieldKey(collection: Collection): string | undefined;
117
+ export {};