editlet 0.0.0-stage → 0.1.0-alpha.1

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 +140 -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,49 @@
1
+ import { PreviewRef } from "./previewCookie.js";
2
+ import { Resolved } from "../core/references.js";
3
+ import { Entry } from "../core/validate.js";
4
+ export type AnyEntry = {
5
+ collection: string;
6
+ slug: string;
7
+ data: object;
8
+ body: string;
9
+ ref?: PreviewRef;
10
+ };
11
+ export type RefKey<M> = { [C in keyof M & string]: `${C}/${string}`; }[keyof M & string];
12
+ export type CollectionOf<R> = R extends `${infer C}/${string}` ? C : never;
13
+ export interface ReaderOptions {
14
+ root?: string;
15
+ schemaPath?: string;
16
+ /** Include entries with `draft: true`. Defaults to true outside production. */
17
+ drafts?: boolean;
18
+ }
19
+ /**
20
+ * Per-call options. `ref` (from `previewRef()`) makes a draft render read that commit from GitHub, not the disk, and
21
+ * include `draft: true` entries (only a verified editor's render has a ref).
22
+ */
23
+ export interface ReadOptions {
24
+ ref?: PreviewRef;
25
+ /**
26
+ * Replace references with the entries they point at, this many levels deep (0–10, default 0: keep the
27
+ * `collection/slug` strings). A reference to an entry this read does not show (a draft in production) is dropped.
28
+ */
29
+ depth?: number;
30
+ }
31
+ /** `getReferrers` options: `from` keeps only entries of one collection. */
32
+ export interface ReferrerOptions<C extends string = string> extends ReadOptions {
33
+ from?: C;
34
+ }
35
+ export declare function createReader<M extends { [K in keyof M]: AnyEntry; } = Record<string, Entry>>(options?: ReaderOptions): {
36
+ getEntries: <C extends keyof M & string, D extends number = 0>(collection: C, read?: ReadOptions & {
37
+ depth?: D;
38
+ }) => Promise<Resolved<M, M[C], D>[]>;
39
+ getEntry: <C extends keyof M & string, D extends number = 0>(collection: C, slug?: string, read?: ReadOptions & {
40
+ depth?: D;
41
+ }) => Promise<Resolved<M, M[C], D> | undefined>;
42
+ getRef: <R extends RefKey<M>, D extends number = 0>(ref: R, read?: ReadOptions & {
43
+ depth?: D;
44
+ }) => Promise<Resolved<M, M[CollectionOf<R> & keyof M], D> | undefined>;
45
+ getReferrers: <C extends keyof M & string = keyof M & string, D extends number = 0>(ref: RefKey<M>, read?: ReferrerOptions<C> & {
46
+ depth?: D;
47
+ }) => Promise<Resolved<M, M[C], D>[]>;
48
+ };
49
+ export type { Resolved };
@@ -0,0 +1,161 @@
1
+ import { assetUrl } from "./assetUrl.js";
2
+ import { readPreviewEnv } from "./previewEnv.js";
3
+ import { EditletError } from "../core/issues.js";
4
+ import { refKey } from "../core/paths.js";
5
+ import { entriesFromFiles } from "../core/project.js";
6
+ import { checkReferenceDepth, createResolver, findReferrers } from "../core/references.js";
7
+ import { normalizeData, validateEntry } from "../core/validate.js";
8
+ import { mapFields } from "../core/walk.js";
9
+ import { listFiles } from "../node/fs.js";
10
+ import { readProject } from "../node/project.js";
11
+ import { cache } from "react";
12
+ // next/reader.ts
13
+ const production = /* @__PURE__ */ new Map();
14
+ const describeIssue = (i) => `${i.file ?? ""}${i.file ? ": " : ""}${i.path ? `${i.path} — ` : ""}${i.message}`;
15
+ function buildLoaded(schema, entries, baseIssues, fileExists, ref) {
16
+ const issues = [...baseIssues];
17
+ const refs = new Set(entries.map((e) => refKey(e.collection, e.slug)));
18
+ for (const entry of entries) issues.push(...validateEntry(schema, entry, {
19
+ entryExists: (r) => refs.has(r),
20
+ fileExists
21
+ }));
22
+ if (issues.length > 0) throw new EditletError(issues, `Invalid content:\n${issues.map(describeIssue).join("\n")}`);
23
+ const out = {
24
+ schema,
25
+ byCollection: /* @__PURE__ */ new Map(),
26
+ byRef: /* @__PURE__ */ new Map()
27
+ };
28
+ for (const entry of entries) {
29
+ const collection = schema.collections[entry.collection];
30
+ const data = mapFields(collection.fields, normalizeData(collection, entry.data), (field, value) => {
31
+ const image = value;
32
+ return field.type === "image" && image && typeof image.src === "string" ? {
33
+ ...image,
34
+ src: assetUrl(entry, image.src)
35
+ } : value;
36
+ });
37
+ const loaded = ref ? {
38
+ ...entry,
39
+ data,
40
+ ref
41
+ } : {
42
+ ...entry,
43
+ data
44
+ };
45
+ const list = out.byCollection.get(entry.collection) ?? [];
46
+ list.push(loaded);
47
+ out.byCollection.set(entry.collection, list);
48
+ out.byRef.set(refKey(entry.collection, entry.slug), loaded);
49
+ }
50
+ return out;
51
+ }
52
+ async function load(root, schemaPath) {
53
+ const project = await readProject(root, schemaPath);
54
+ const { schema, entries } = project;
55
+ const files = new Set(await listFiles(root, schema.contentDir));
56
+ return buildLoaded(schema, entries, project.issues, (p) => files.has(p));
57
+ }
58
+ const MAX_PREVIEW_SHAS = 20;
59
+ const previews = /* @__PURE__ */ new Map();
60
+ async function loadCommit(ref) {
61
+ const token = process.env.CMS_GITHUB_TOKEN;
62
+ if (!token) throw new Error("Draft render needs CMS_GITHUB_TOKEN in the server environment.");
63
+ const env = readPreviewEnv();
64
+ if (!env) throw new Error("Draft render needs the EDITLET_PREVIEW config; enable `admin.preview` in withEditlet.");
65
+ const { readCommit } = await import("../github/commitReader.js");
66
+ const { schema, files, paths } = await readCommit({
67
+ token,
68
+ owner: env.owner,
69
+ repo: env.repo,
70
+ schemaPath: env.schemaPath,
71
+ sha: ref.sha
72
+ });
73
+ const { entries, issues } = entriesFromFiles(schema, env.schemaPath, files);
74
+ return buildLoaded(schema, entries, issues, (p) => paths.has(p), ref);
75
+ }
76
+ function loadPreview(ref) {
77
+ let promise = previews.get(ref.sha);
78
+ if (promise) {
79
+ previews.delete(ref.sha);
80
+ previews.set(ref.sha, promise);
81
+ return promise;
82
+ }
83
+ promise = loadCommit(ref);
84
+ previews.set(ref.sha, promise);
85
+ promise.catch(() => {
86
+ if (previews.get(ref.sha) === promise) previews.delete(ref.sha);
87
+ });
88
+ while (previews.size > MAX_PREVIEW_SHAS) previews.delete(previews.keys().next().value);
89
+ return promise;
90
+ }
91
+ const loadCached = cache((root, schemaPath) => {
92
+ if (process.env.NODE_ENV !== "production") return load(root, schemaPath);
93
+ const key = `${root}|${schemaPath}`;
94
+ let promise = production.get(key);
95
+ if (!promise) {
96
+ promise = load(root, schemaPath);
97
+ production.set(key, promise);
98
+ }
99
+ return promise;
100
+ });
101
+ const bySlug = (a, b) => a.slug.localeCompare(b.slug);
102
+ function createReader(options = {}) {
103
+ const root = options.root ?? process.cwd();
104
+ const schemaPath = options.schemaPath ?? "cms/schema.json";
105
+ const drafts = options.drafts ?? process.env.NODE_ENV !== "production";
106
+ const visible = (e) => drafts || e.ref !== void 0 || e.data.draft !== true;
107
+ const resolvers = /* @__PURE__ */ new WeakMap();
108
+ function resolverOf(loaded) {
109
+ let resolve = resolvers.get(loaded);
110
+ if (!resolve) {
111
+ resolve = createResolver(loaded.schema, (r) => {
112
+ const found = loaded.byRef.get(r);
113
+ return found && visible(found) ? found : void 0;
114
+ });
115
+ resolvers.set(loaded, resolve);
116
+ }
117
+ return resolve;
118
+ }
119
+ /** The source for a read, and how to hand its entries out (`depth` checked before anything loads). */
120
+ async function open(read) {
121
+ const depth = read.depth ?? 0;
122
+ checkReferenceDepth(depth);
123
+ const loaded = await (read.ref ? loadPreview(read.ref) : loadCached(root, schemaPath));
124
+ const resolve = resolverOf(loaded);
125
+ return {
126
+ loaded,
127
+ out: (entry) => resolve(entry, depth)
128
+ };
129
+ }
130
+ async function getEntries(collection, read = {}) {
131
+ const { loaded, out } = await open(read);
132
+ return (loaded.byCollection.get(collection) ?? []).filter(visible).toSorted(bySlug).map(out);
133
+ }
134
+ async function getEntry(collection, slug = "index", read = {}) {
135
+ const { loaded, out } = await open(read);
136
+ const found = (loaded.byCollection.get(collection) ?? []).find((e) => e.slug === slug && visible(e));
137
+ return found && out(found);
138
+ }
139
+ async function getRef(ref, read = {}) {
140
+ const i = ref.indexOf("/");
141
+ return getEntry(ref.slice(0, i), ref.slice(i + 1), read);
142
+ }
143
+ /**
144
+ * Visible entries with a reference field pointing at `ref` ("used by"), sorted by collection, then slug. `from`
145
+ * keeps one collection; `depth` resolves the referrers like the other reads do.
146
+ */
147
+ async function getReferrers(ref, read = {}) {
148
+ const { loaded, out } = await open(read);
149
+ const pool = read.from === void 0 ? [...loaded.byCollection.values()].flat() : loaded.byCollection.get(read.from) ?? [];
150
+ const found = findReferrers(loaded.schema, pool.filter(visible), ref).map((r) => r.entry);
151
+ found.sort((a, b) => a.collection.localeCompare(b.collection) || bySlug(a, b));
152
+ return found.map(out);
153
+ }
154
+ return {
155
+ getEntries,
156
+ getEntry,
157
+ getRef,
158
+ getReferrers
159
+ };
160
+ }
161
+ export { createReader };
@@ -0,0 +1,56 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>Editlet admin</title>
7
+ <style>
8
+ body {
9
+ font:
10
+ 16px/1.5 system-ui,
11
+ sans-serif;
12
+ margin: 0;
13
+ padding: 2rem;
14
+ background: #fff;
15
+ color: #111;
16
+ }
17
+ @media (prefers-color-scheme: dark) {
18
+ body {
19
+ background: #111;
20
+ color: #eee;
21
+ }
22
+ }
23
+ button {
24
+ font: inherit;
25
+ padding: 0.4rem 0.9rem;
26
+ }
27
+ </style>
28
+ </head>
29
+ <body>
30
+ <h1>Editlet admin</h1>
31
+ <p>The Editlet admin is not built yet (P6).</p>
32
+ <p id="route">Waiting for the host page...</p>
33
+ <button id="close" type="button">Close</button>
34
+ <script>
35
+ var origin = null;
36
+ function send(type) {
37
+ var target = type === 'ready' ? '*' : origin;
38
+ if (target) window.parent.postMessage({ source: 'editlet', v: 1, type: type }, target);
39
+ }
40
+ window.addEventListener('message', function (event) {
41
+ var data = event.data;
42
+ if (event.source !== window.parent || !data || data.source !== 'editlet' || data.type !== 'init') return;
43
+ if (origin === null) origin = event.origin;
44
+ if (event.origin !== origin) return;
45
+ document.getElementById('route').textContent = 'Route: ' + (data.route || '/');
46
+ });
47
+ document.getElementById('close').addEventListener('click', function () {
48
+ send('close');
49
+ });
50
+ document.addEventListener('keydown', function (event) {
51
+ if (event.key === 'Escape') send('close');
52
+ });
53
+ send('ready');
54
+ </script>
55
+ </body>
56
+ </html>
@@ -0,0 +1,6 @@
1
+ export declare function createDevHandler(opts: {
2
+ root: string;
3
+ schemaPath?: string;
4
+ /** Extra hostnames (exact match) besides localhost, 127.0.0.1, [::1] and *.localhost. */
5
+ allowedHosts?: string[];
6
+ }): (req: Request) => Promise<Response>;