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.
- package/LICENSE +21 -0
- package/README.md +140 -2
- package/dist/admin/config.d.ts +22 -0
- package/dist/admin/config.js +75 -0
- package/dist/admin/dist/assets/RichEditor-BlaCRrEX.css +1 -0
- package/dist/admin/dist/assets/RichEditor-IZKdt6MK.js +42 -0
- package/dist/admin/dist/assets/blank-line-CbGzlqBI.js +1 -0
- package/dist/admin/dist/assets/channel-CljeMIwA.js +1 -0
- package/dist/admin/dist/assets/db-CCMyPgQa.js +1 -0
- package/dist/admin/dist/assets/index-CWoJh4H8.js +257 -0
- package/dist/admin/dist/assets/index-DkRMnD_M.css +1 -0
- package/dist/admin/dist/assets/mdast-util-from-markdown-C79q1_d8.js +3 -0
- package/dist/admin/dist/assets/mdast-util-gfm-cM-_xFJE.js +19 -0
- package/dist/admin/dist/assets/micromark-extension-gfm-Wz6-oszS.js +1 -0
- package/dist/admin/dist/assets/micromark-util-classify-character-BgXaGAja.js +1 -0
- package/dist/admin/dist/assets/micromark-util-html-tag-name-B61T18yC.js +1 -0
- package/dist/admin/dist/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/admin/dist/index.html +16 -0
- package/dist/admin/dist/preview.js +1 -0
- package/dist/admin/dist/window.js +1 -0
- package/dist/astro/content.d.ts +21 -0
- package/dist/astro/content.js +235 -0
- package/dist/astro/devServer.js +89 -0
- package/dist/astro/index.d.ts +15 -0
- package/dist/astro/index.js +98 -0
- package/dist/astro/nodeBridge.js +36 -0
- package/dist/astro/shared.js +10 -0
- package/dist/astro/stub/index.html +71 -0
- package/dist/auth/client.d.ts +24 -0
- package/dist/auth/client.js +120 -0
- package/dist/auth/handler.d.ts +16 -0
- package/dist/auth/handler.js +281 -0
- package/dist/auth/protocol.d.ts +25 -0
- package/dist/auth/protocol.js +3 -0
- package/dist/core/assets.d.ts +73 -0
- package/dist/core/assets.js +405 -0
- package/dist/core/devApi.d.ts +28 -0
- package/dist/core/devApi.js +3 -0
- package/dist/core/entryFile.d.ts +13 -0
- package/dist/core/entryFile.js +81 -0
- package/dist/core/fields.js +147 -0
- package/dist/core/generate.d.ts +3 -0
- package/dist/core/generate.js +70 -0
- package/dist/core/gitStyle.d.ts +92 -0
- package/dist/core/gitStyle.js +206 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +16 -0
- package/dist/core/issues.d.ts +10 -0
- package/dist/core/issues.js +10 -0
- package/dist/core/paths.d.ts +31 -0
- package/dist/core/paths.js +114 -0
- package/dist/core/project.js +56 -0
- package/dist/core/references.d.ts +68 -0
- package/dist/core/references.js +103 -0
- package/dist/core/rename.d.ts +60 -0
- package/dist/core/rename.js +133 -0
- package/dist/core/schema.d.ts +117 -0
- package/dist/core/schema.js +299 -0
- package/dist/core/sha.d.ts +2 -0
- package/dist/core/sha.js +13 -0
- package/dist/core/source.d.ts +48 -0
- package/dist/core/source.js +57 -0
- package/dist/core/validate.d.ts +22 -0
- package/dist/core/validate.js +148 -0
- package/dist/core/walk.d.ts +23 -0
- package/dist/core/walk.js +94 -0
- package/dist/core/writes.d.ts +37 -0
- package/dist/core/writes.js +166 -0
- package/dist/github/base64.js +16 -0
- package/dist/github/cache.d.ts +34 -0
- package/dist/github/cache.js +126 -0
- package/dist/github/client.d.ts +38 -0
- package/dist/github/client.js +228 -0
- package/dist/github/codeowners.d.ts +23 -0
- package/dist/github/codeowners.js +96 -0
- package/dist/github/commitReader.d.ts +25 -0
- package/dist/github/commitReader.js +38 -0
- package/dist/github/deploy.d.ts +15 -0
- package/dist/github/deploy.js +33 -0
- package/dist/github/index.d.ts +9 -0
- package/dist/github/index.js +8 -0
- package/dist/github/repo.d.ts +95 -0
- package/dist/github/repo.js +375 -0
- package/dist/github/source.d.ts +115 -0
- package/dist/github/source.js +558 -0
- package/dist/loader/index.d.ts +27 -0
- package/dist/loader/index.js +151 -0
- package/dist/loader/protocol.d.ts +95 -0
- package/dist/loader/protocol.js +120 -0
- package/dist/loader/window/geometry.js +9 -0
- package/dist/next/EditletLoader.d.ts +5 -0
- package/dist/next/EditletLoader.js +33 -0
- package/dist/next/assetUrl.d.ts +9 -0
- package/dist/next/assetUrl.js +13 -0
- package/dist/next/buildGuard.js +26 -0
- package/dist/next/config.d.ts +18 -0
- package/dist/next/config.js +255 -0
- package/dist/next/devServer.js +70 -0
- package/dist/next/editletField.d.ts +7 -0
- package/dist/next/editletField.js +7 -0
- package/dist/next/index.d.ts +8 -0
- package/dist/next/index.js +6 -0
- package/dist/next/preview.d.ts +18 -0
- package/dist/next/preview.js +100 -0
- package/dist/next/previewCookie.d.ts +5 -0
- package/dist/next/previewCookie.js +72 -0
- package/dist/next/previewEnv.js +25 -0
- package/dist/next/previewRef.d.ts +8 -0
- package/dist/next/previewRef.js +17 -0
- package/dist/next/publicDir.js +65 -0
- package/dist/next/reader.d.ts +49 -0
- package/dist/next/reader.js +161 -0
- package/dist/next/stub/index.html +56 -0
- package/dist/node/devHandler.d.ts +6 -0
- package/dist/node/devHandler.js +430 -0
- package/dist/node/fs.js +37 -0
- package/dist/node/index.d.ts +3 -0
- package/dist/node/index.js +3 -0
- package/dist/node/project.d.ts +12 -0
- package/dist/node/project.js +43 -0
- package/package.json +187 -4
- package/skills/README.md +84 -0
- package/skills/editlet/SKILL.md +119 -0
- package/skills/editlet/references/content-format.md +191 -0
- package/skills/editlet/references/preview.md +75 -0
- package/skills/editlet/references/publishing.md +137 -0
- package/skills/editlet/references/schema-changes.md +127 -0
- package/skills/editlet/scripts/guard.mjs +1061 -0
- package/skills/editlet/scripts/link.mjs +193 -0
- package/skills/editlet/scripts/refs.mjs +1480 -0
- package/skills/editlet/scripts/schema.mjs +1340 -0
- package/skills/editlet/scripts/style.mjs +657 -0
- package/skills/editlet/scripts/types.mjs +525 -0
- package/skills/editlet/scripts/validate.mjs +724 -0
- package/skills/editlet-migrate/SKILL.md +92 -0
- package/skills/editlet-migrate/references/mapping.md +73 -0
- package/skills/editlet-migrate/references/sources.md +70 -0
- package/skills/editlet-setup/SKILL.md +108 -0
- package/skills/editlet-setup/references/astro.md +83 -0
- package/skills/editlet-setup/references/doctor.md +89 -0
- package/skills/editlet-setup/references/next.md +140 -0
- package/skills/editlet-setup/references/preview.md +54 -0
- package/skills/editlet-setup/references/production.md +153 -0
- package/skills/editlet-setup/references/roles.md +238 -0
- package/skills/editlet-setup/references/skills.md +99 -0
- package/skills/editlet-setup/scripts/doctor.mjs +1378 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import { resolveAssetPath } from "./paths.js";
|
|
2
|
+
import { markdownFieldKey } from "./schema.js";
|
|
3
|
+
import { visitFields } from "./walk.js";
|
|
4
|
+
// core/assets.ts
|
|
5
|
+
/** Fenced code blocks, indented code blocks, inline code spans and HTML comments, as ranges to skip. */
|
|
6
|
+
function codeRanges(text) {
|
|
7
|
+
const out = [];
|
|
8
|
+
const lines = text.split("\n");
|
|
9
|
+
let pos = 0;
|
|
10
|
+
let fence = null;
|
|
11
|
+
let indented = null;
|
|
12
|
+
let previousBlank = true;
|
|
13
|
+
for (const line of lines) {
|
|
14
|
+
const end = pos + line.length;
|
|
15
|
+
const blank = line.trim() === "";
|
|
16
|
+
if (fence) {
|
|
17
|
+
const close = /^ {0,3}(`{3,}|~{3,})\s*$/.exec(line);
|
|
18
|
+
if (close && close[1][0] === fence.char && close[1].length >= fence.length) {
|
|
19
|
+
out.push([fence.start, end]);
|
|
20
|
+
fence = null;
|
|
21
|
+
}
|
|
22
|
+
} else {
|
|
23
|
+
const open = /^ {0,3}(`{3,}|~{3,})/.exec(line);
|
|
24
|
+
const isIndented = /^(?: {4}|\t)/.test(line) && !blank;
|
|
25
|
+
if (indented !== null && !isIndented && !blank) {
|
|
26
|
+
out.push([indented, pos]);
|
|
27
|
+
indented = null;
|
|
28
|
+
}
|
|
29
|
+
if (open && indented === null) fence = {
|
|
30
|
+
char: open[1][0],
|
|
31
|
+
length: open[1].length,
|
|
32
|
+
start: pos
|
|
33
|
+
};
|
|
34
|
+
else if (isIndented && indented === null && previousBlank) indented = pos;
|
|
35
|
+
}
|
|
36
|
+
previousBlank = blank;
|
|
37
|
+
pos = end + 1;
|
|
38
|
+
}
|
|
39
|
+
if (fence) out.push([fence.start, text.length]);
|
|
40
|
+
if (indented !== null) out.push([indented, text.length]);
|
|
41
|
+
const inside = (i) => out.some(([s, e]) => i >= s && i < e);
|
|
42
|
+
const inline = [];
|
|
43
|
+
const spans = /(`+)|<!--/g;
|
|
44
|
+
let m;
|
|
45
|
+
while (m = spans.exec(text)) {
|
|
46
|
+
if (inside(m.index)) continue;
|
|
47
|
+
if (m[0] === "<!--") {
|
|
48
|
+
const close = text.indexOf("-->", m.index + 4);
|
|
49
|
+
const stop = close === -1 ? text.length : close + 3;
|
|
50
|
+
inline.push([m.index, stop]);
|
|
51
|
+
spans.lastIndex = stop;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const run = m[1];
|
|
55
|
+
const closer = new RegExp(`(?<!\`)${run}(?!\`)`, "g");
|
|
56
|
+
closer.lastIndex = m.index + run.length;
|
|
57
|
+
const close = closer.exec(text);
|
|
58
|
+
if (!close) continue;
|
|
59
|
+
inline.push([m.index, close.index + run.length]);
|
|
60
|
+
spans.lastIndex = close.index + run.length;
|
|
61
|
+
}
|
|
62
|
+
return [...out, ...inline];
|
|
63
|
+
}
|
|
64
|
+
/** The text with every code range replaced by spaces, so offsets stay the same and nothing inside matches. */
|
|
65
|
+
function maskCode(text) {
|
|
66
|
+
let masked = text;
|
|
67
|
+
for (const [s, e] of codeRanges(text)) masked = masked.slice(0, s) + masked.slice(s, e).replace(/[^\n]/g, " ") + masked.slice(e);
|
|
68
|
+
return masked;
|
|
69
|
+
}
|
|
70
|
+
const isEscaped = (text, i) => {
|
|
71
|
+
let n = 0;
|
|
72
|
+
while (i - 1 - n >= 0 && text[i - 1 - n] === "\\") n++;
|
|
73
|
+
return n % 2 === 1;
|
|
74
|
+
};
|
|
75
|
+
const unescapeMarkdown = (s) => s.replace(/\\([!-/:-@[-`{-~])/g, "$1");
|
|
76
|
+
const decodeEntities = (s) => s.replace(/&(quot|amp|lt|gt|#39|apos);/g, (_, e) => e === "quot" ? "\"" : e === "amp" ? "&" : e === "lt" ? "<" : e === "gt" ? ">" : "'");
|
|
77
|
+
/** Reference labels match case-insensitively with whitespace collapsed. */
|
|
78
|
+
const normalizeLabel = (s) => s.trim().replace(/\s+/g, " ").toLowerCase();
|
|
79
|
+
/** Index of the `]` closing the `[` at `open` (nested brackets allowed), or -1. */
|
|
80
|
+
function closingBracket(text, open) {
|
|
81
|
+
let depth = 0;
|
|
82
|
+
for (let i = open; i < text.length; i++) {
|
|
83
|
+
const c = text[i];
|
|
84
|
+
if (c === "\n" && text[i + 1] === "\n") return -1;
|
|
85
|
+
if (isEscaped(text, i)) continue;
|
|
86
|
+
if (c === "[") depth++;
|
|
87
|
+
else if (c === "]" && --depth === 0) return i;
|
|
88
|
+
}
|
|
89
|
+
return -1;
|
|
90
|
+
}
|
|
91
|
+
/** `(dest "title")` starting at `open`: the destination's range and text, and the index after `)`; null if not one. */
|
|
92
|
+
function inlineDestination(text, open) {
|
|
93
|
+
let i = open + 1;
|
|
94
|
+
while (text[i] === " " || text[i] === " " || text[i] === "\n") i++;
|
|
95
|
+
let start = i;
|
|
96
|
+
let stop;
|
|
97
|
+
if (text[i] === "<") {
|
|
98
|
+
const close = text.indexOf(">", i);
|
|
99
|
+
if (close === -1 || text.slice(i, close).includes("\n")) return null;
|
|
100
|
+
start = i + 1;
|
|
101
|
+
stop = close;
|
|
102
|
+
i = close + 1;
|
|
103
|
+
} else {
|
|
104
|
+
let depth = 0;
|
|
105
|
+
while (i < text.length && !/\s/.test(text[i])) {
|
|
106
|
+
if (text[i] === "(" && !isEscaped(text, i)) depth++;
|
|
107
|
+
else if (text[i] === ")" && !isEscaped(text, i)) {
|
|
108
|
+
if (depth === 0) break;
|
|
109
|
+
depth--;
|
|
110
|
+
}
|
|
111
|
+
i++;
|
|
112
|
+
}
|
|
113
|
+
stop = i;
|
|
114
|
+
}
|
|
115
|
+
while (text[i] === " " || text[i] === " " || text[i] === "\n") i++;
|
|
116
|
+
const quote = text[i];
|
|
117
|
+
if (quote === "\"" || quote === "'" || quote === "(") {
|
|
118
|
+
const closeChar = quote === "(" ? ")" : quote;
|
|
119
|
+
let j = i + 1;
|
|
120
|
+
while (j < text.length && (text[j] !== closeChar || isEscaped(text, j))) j++;
|
|
121
|
+
if (j >= text.length) return null;
|
|
122
|
+
i = j + 1;
|
|
123
|
+
while (text[i] === " " || text[i] === " " || text[i] === "\n") i++;
|
|
124
|
+
}
|
|
125
|
+
if (text[i] !== ")") return null;
|
|
126
|
+
return {
|
|
127
|
+
range: [start, stop],
|
|
128
|
+
src: unescapeMarkdown(text.slice(start, stop)),
|
|
129
|
+
end: i + 1
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** `[label]: dest "title"` lines, first definition of a label wins. */
|
|
133
|
+
function definitions(masked) {
|
|
134
|
+
const out = /* @__PURE__ */ new Map();
|
|
135
|
+
const re = /^ {0,3}\[((?:\\.|[^\]\\])+)\]:[ \t]*(?:\n[ \t]*)?(<[^>\n]*>|\S+)/gm;
|
|
136
|
+
let m;
|
|
137
|
+
while (m = re.exec(masked)) {
|
|
138
|
+
const label = normalizeLabel(m[1]);
|
|
139
|
+
if (label === "" || out.has(label)) continue;
|
|
140
|
+
const raw = m[2];
|
|
141
|
+
const destStart = m.index + m[0].length - raw.length;
|
|
142
|
+
const angle = raw.startsWith("<");
|
|
143
|
+
const start = angle ? destStart + 1 : destStart;
|
|
144
|
+
const stop = angle ? destStart + raw.length - 1 : destStart + raw.length;
|
|
145
|
+
out.set(label, {
|
|
146
|
+
range: [start, stop],
|
|
147
|
+
src: unescapeMarkdown(masked.slice(start, stop))
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
function markdownImages(masked, defs) {
|
|
153
|
+
const out = [];
|
|
154
|
+
let from = 0;
|
|
155
|
+
for (;;) {
|
|
156
|
+
const at = masked.indexOf("![", from);
|
|
157
|
+
if (at === -1) return out;
|
|
158
|
+
from = at + 2;
|
|
159
|
+
if (isEscaped(masked, at)) continue;
|
|
160
|
+
const close = closingBracket(masked, at + 1);
|
|
161
|
+
if (close === -1) continue;
|
|
162
|
+
const altRange = [at + 2, close];
|
|
163
|
+
const altText = masked.slice(at + 2, close);
|
|
164
|
+
const alt = unescapeMarkdown(altText);
|
|
165
|
+
const next = masked[close + 1];
|
|
166
|
+
if (next === "(") {
|
|
167
|
+
const dest = inlineDestination(masked, close + 1);
|
|
168
|
+
if (dest) {
|
|
169
|
+
out.push({
|
|
170
|
+
kind: "inline",
|
|
171
|
+
src: dest.src,
|
|
172
|
+
alt,
|
|
173
|
+
range: [at, dest.end],
|
|
174
|
+
altRange,
|
|
175
|
+
srcRange: dest.range
|
|
176
|
+
});
|
|
177
|
+
from = dest.end;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
let label;
|
|
182
|
+
let end = close + 1;
|
|
183
|
+
if (next === "[") {
|
|
184
|
+
const labelClose = closingBracket(masked, close + 1);
|
|
185
|
+
if (labelClose !== -1) {
|
|
186
|
+
const text = masked.slice(close + 2, labelClose);
|
|
187
|
+
label = text.trim() === "" ? {
|
|
188
|
+
text: altText,
|
|
189
|
+
form: "collapsed"
|
|
190
|
+
} : {
|
|
191
|
+
text,
|
|
192
|
+
form: "full"
|
|
193
|
+
};
|
|
194
|
+
end = labelClose + 1;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
label ??= {
|
|
198
|
+
text: altText,
|
|
199
|
+
form: "shortcut"
|
|
200
|
+
};
|
|
201
|
+
const def = defs.get(normalizeLabel(label.text));
|
|
202
|
+
if (!def) continue;
|
|
203
|
+
out.push({
|
|
204
|
+
kind: "reference",
|
|
205
|
+
src: def.src,
|
|
206
|
+
alt,
|
|
207
|
+
range: [at, end],
|
|
208
|
+
altRange,
|
|
209
|
+
srcRange: def.range,
|
|
210
|
+
label
|
|
211
|
+
});
|
|
212
|
+
from = end;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function htmlImages(masked) {
|
|
216
|
+
const out = [];
|
|
217
|
+
const tags = /<img\b[^>]*>/gi;
|
|
218
|
+
let m;
|
|
219
|
+
while (m = tags.exec(masked)) {
|
|
220
|
+
const tag = m[0];
|
|
221
|
+
const attrs = /\s(src|alt)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+))/gi;
|
|
222
|
+
let src;
|
|
223
|
+
let alt;
|
|
224
|
+
let a;
|
|
225
|
+
while (a = attrs.exec(tag)) {
|
|
226
|
+
const value = a[2] ?? a[3] ?? a[4] ?? "";
|
|
227
|
+
const quoted = a[2] !== void 0 || a[3] !== void 0;
|
|
228
|
+
const valueStart = m.index + a.index + a[0].length - value.length - (quoted ? 1 : 0);
|
|
229
|
+
const entry = {
|
|
230
|
+
range: [valueStart, valueStart + value.length],
|
|
231
|
+
value: decodeEntities(value)
|
|
232
|
+
};
|
|
233
|
+
if (a[1].toLowerCase() === "src") src ??= entry;
|
|
234
|
+
else alt ??= entry;
|
|
235
|
+
}
|
|
236
|
+
if (!src) continue;
|
|
237
|
+
out.push({
|
|
238
|
+
kind: "html",
|
|
239
|
+
src: src.value,
|
|
240
|
+
alt: alt?.value ?? "",
|
|
241
|
+
range: [m.index, m.index + tag.length],
|
|
242
|
+
altRange: alt?.range ?? null,
|
|
243
|
+
srcRange: src.range
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
/** Every image in a markdown body (code blocks, inline code and HTML comments skipped), in document order. */
|
|
249
|
+
function findBodyImages(body) {
|
|
250
|
+
const masked = maskCode(body);
|
|
251
|
+
return [...markdownImages(masked, definitions(masked)), ...htmlImages(masked)].toSorted((a, b) => a.range[0] - b.range[0]);
|
|
252
|
+
}
|
|
253
|
+
/** Images an entry uses: its `image` fields (with a `src`), then the images in its body. */
|
|
254
|
+
function findImageRefs(schema, collection, entryPath, data, body) {
|
|
255
|
+
const out = [];
|
|
256
|
+
visitFields(collection.fields, data, (field, v, at) => {
|
|
257
|
+
const nested = at.length > 1 ? {
|
|
258
|
+
at,
|
|
259
|
+
label: `${field.label} (${at.slice(0, -1).join(".")})`
|
|
260
|
+
} : {};
|
|
261
|
+
if (field.type === "markdown" && typeof v === "string") {
|
|
262
|
+
for (const image of findBodyImages(v)) {
|
|
263
|
+
const use = {
|
|
264
|
+
src: image.src,
|
|
265
|
+
alt: image.alt,
|
|
266
|
+
path: resolveAssetPath(schema, entryPath, image.src),
|
|
267
|
+
body: image
|
|
268
|
+
};
|
|
269
|
+
out.push({
|
|
270
|
+
field: at.join("."),
|
|
271
|
+
...nested,
|
|
272
|
+
at,
|
|
273
|
+
...use
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (field.type !== "image" || typeof v !== "object" || v === null) return;
|
|
279
|
+
const { src, alt } = v;
|
|
280
|
+
if (typeof src !== "string" || src === "") return;
|
|
281
|
+
out.push({
|
|
282
|
+
field: at.join("."),
|
|
283
|
+
...nested,
|
|
284
|
+
src,
|
|
285
|
+
alt: typeof alt === "string" ? alt : "",
|
|
286
|
+
path: resolveAssetPath(schema, entryPath, src)
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
const bodyField = markdownFieldKey(collection) ?? "body";
|
|
290
|
+
for (const image of findBodyImages(body)) out.push({
|
|
291
|
+
field: bodyField,
|
|
292
|
+
src: image.src,
|
|
293
|
+
alt: image.alt,
|
|
294
|
+
path: resolveAssetPath(schema, entryPath, image.src),
|
|
295
|
+
body: image
|
|
296
|
+
});
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
const escapeAlt = (alt) => alt.replace(/[\\[\]]/g, "\\$&");
|
|
300
|
+
const escapeAttr = (s) => s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
301
|
+
/** A markdown destination: wrapped in `<…>` when it has spaces, parentheses or angle brackets. */
|
|
302
|
+
const markdownDestination = (src) => /[\s()<>]/.test(src) ? `<${src.replace(/[<>]/g, "\\$&")}>` : src;
|
|
303
|
+
function altSplice(image, alt) {
|
|
304
|
+
if (image.kind === "html") {
|
|
305
|
+
if (image.altRange) return {
|
|
306
|
+
range: image.altRange,
|
|
307
|
+
text: escapeAttr(alt)
|
|
308
|
+
};
|
|
309
|
+
const at = image.range[0] + 4;
|
|
310
|
+
return {
|
|
311
|
+
range: [at, at],
|
|
312
|
+
text: ` alt="${escapeAttr(alt)}"`
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
if (image.kind === "reference" && image.label && image.label.form !== "full") return {
|
|
316
|
+
range: image.range,
|
|
317
|
+
text: `![${escapeAlt(alt)}][${image.label.text}]`
|
|
318
|
+
};
|
|
319
|
+
return {
|
|
320
|
+
range: image.altRange,
|
|
321
|
+
text: escapeAlt(alt)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function srcSplice(body, image, src) {
|
|
325
|
+
if (image.kind === "html") return {
|
|
326
|
+
range: image.srcRange,
|
|
327
|
+
text: escapeAttr(src)
|
|
328
|
+
};
|
|
329
|
+
const [s, e] = image.srcRange;
|
|
330
|
+
const text = body[s - 1] === "<" && body[e] === ">" ? src.replace(/[<>]/g, "\\$&") : markdownDestination(src);
|
|
331
|
+
return {
|
|
332
|
+
range: image.srcRange,
|
|
333
|
+
text
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* The body with the given images' alt and/or src replaced; every other byte stays. Edits that hit the same range
|
|
338
|
+
* (several images sharing one reference definition) are applied once. Ranges come from `findBodyImages(body)`.
|
|
339
|
+
*/
|
|
340
|
+
function editBodyImages(body, edits) {
|
|
341
|
+
const splices = /* @__PURE__ */ new Map();
|
|
342
|
+
for (const { image, alt, src } of edits) for (const splice of [alt === void 0 ? null : altSplice(image, alt), src === void 0 ? null : srcSplice(body, image, src)]) if (splice) splices.set(`${splice.range[0]}:${splice.range[1]}`, splice);
|
|
343
|
+
const ordered = [...splices.values()].toSorted((a, b) => b.range[0] - a.range[0] || b.range[1] - a.range[1]);
|
|
344
|
+
let out = body;
|
|
345
|
+
let floor = 1e400;
|
|
346
|
+
for (const { range, text } of ordered) {
|
|
347
|
+
if (range[1] > floor) continue;
|
|
348
|
+
out = out.slice(0, range[0]) + text + out.slice(range[1]);
|
|
349
|
+
floor = range[0];
|
|
350
|
+
}
|
|
351
|
+
return out;
|
|
352
|
+
}
|
|
353
|
+
function getAt(data, path) {
|
|
354
|
+
return path.reduce((v, k) => v !== null && typeof v === "object" ? v[k] : void 0, data);
|
|
355
|
+
}
|
|
356
|
+
/** A copy of `data` with `path` set to `value`; the objects and arrays along the path are copied, nothing else. */
|
|
357
|
+
function setAt(data, path, value) {
|
|
358
|
+
if (path.length === 0) return value;
|
|
359
|
+
const [head, ...rest] = path;
|
|
360
|
+
const copy = Array.isArray(data) ? [...data] : { ...data };
|
|
361
|
+
copy[head] = setAt(copy[head], rest, value);
|
|
362
|
+
return copy;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* One entry's data and body with `edits` applied (all uses of the same entry): image fields get the new `alt`/`src`
|
|
366
|
+
* at any depth, images in the body or in a nested Markdown string are changed in place (`editBodyImages`, every other
|
|
367
|
+
* byte kept). The inputs are not changed.
|
|
368
|
+
*/
|
|
369
|
+
function applyImageEdits(data, body, edits) {
|
|
370
|
+
let out = { ...data };
|
|
371
|
+
const bodyEdits = [];
|
|
372
|
+
const textEdits = /* @__PURE__ */ new Map();
|
|
373
|
+
for (const { use, alt, src } of edits) if (use.body && use.at) {
|
|
374
|
+
const key = use.at.join(".");
|
|
375
|
+
const group = textEdits.get(key) ?? {
|
|
376
|
+
at: use.at,
|
|
377
|
+
edits: []
|
|
378
|
+
};
|
|
379
|
+
group.edits.push({
|
|
380
|
+
image: use.body,
|
|
381
|
+
alt,
|
|
382
|
+
src
|
|
383
|
+
});
|
|
384
|
+
textEdits.set(key, group);
|
|
385
|
+
} else if (use.body) bodyEdits.push({
|
|
386
|
+
image: use.body,
|
|
387
|
+
alt,
|
|
388
|
+
src
|
|
389
|
+
});
|
|
390
|
+
else {
|
|
391
|
+
const at = use.at ?? [use.field];
|
|
392
|
+
const next = {
|
|
393
|
+
...getAt(out, at),
|
|
394
|
+
...src === void 0 ? {} : { src },
|
|
395
|
+
...alt === void 0 ? {} : { alt }
|
|
396
|
+
};
|
|
397
|
+
out = setAt(out, at, next);
|
|
398
|
+
}
|
|
399
|
+
for (const { at, edits: group } of textEdits.values()) out = setAt(out, at, editBodyImages(String(getAt(out, at)), group));
|
|
400
|
+
return {
|
|
401
|
+
data: out,
|
|
402
|
+
body: bodyEdits.length > 0 ? editBodyImages(body, bodyEdits) : body
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
export { applyImageEdits, editBodyImages, findBodyImages, findImageRefs };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Issue } from "./issues.js";
|
|
2
|
+
import { FileRef } from "./source.js";
|
|
3
|
+
export declare const DEV_API_BASE = "/__editlet";
|
|
4
|
+
export interface DevFilesResponse {
|
|
5
|
+
files: FileRef[];
|
|
6
|
+
}
|
|
7
|
+
export interface DevGitResponse {
|
|
8
|
+
/** The checked-out branch; `null` when `root` is not a git checkout, git is missing, or HEAD is detached. */
|
|
9
|
+
branch: string | null;
|
|
10
|
+
/** Files under `contentDir` with uncommitted changes (modified, added, deleted or untracked). */
|
|
11
|
+
changes: number;
|
|
12
|
+
}
|
|
13
|
+
export interface DevErrorResponse {
|
|
14
|
+
issues: Issue[];
|
|
15
|
+
}
|
|
16
|
+
export interface DevCommitChange {
|
|
17
|
+
path: string;
|
|
18
|
+
/** New content; `null` deletes the file. */
|
|
19
|
+
content: string | null;
|
|
20
|
+
/** How `content` is encoded; default `'utf8'`. Use `'base64'` for binary files. */
|
|
21
|
+
encoding?: 'utf8' | 'base64';
|
|
22
|
+
/** Expected current git blob sha; `null` means the file must not exist yet. Omit to skip the check. */
|
|
23
|
+
sha?: string | null;
|
|
24
|
+
}
|
|
25
|
+
export interface DevCommitRequest {
|
|
26
|
+
message: string;
|
|
27
|
+
changes: DevCommitChange[];
|
|
28
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Issue } from "./issues.js";
|
|
2
|
+
export interface EntryFile {
|
|
3
|
+
data: Record<string, unknown>;
|
|
4
|
+
body: string;
|
|
5
|
+
}
|
|
6
|
+
/** Decodes file content as UTF-8 (strings pass through); on invalid bytes returns an issue for `file` instead. */
|
|
7
|
+
export declare function decodeUtf8(content: Uint8Array | string, file: string): {
|
|
8
|
+
text: string;
|
|
9
|
+
} | {
|
|
10
|
+
issue: Issue;
|
|
11
|
+
};
|
|
12
|
+
export declare function parseEntryFile(source: string): EntryFile;
|
|
13
|
+
export declare function serializeEntryFile(file: EntryFile, fieldOrder?: string[]): string;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { EditletError } from "./issues.js";
|
|
2
|
+
import { Schema, parse, stringify } from "yaml";
|
|
3
|
+
// core/entryFile.ts
|
|
4
|
+
/**
|
|
5
|
+
* Scalar tags of YAML 1.1 (`yes` / `no` / `on`, timestamps, `12:30:00` sexagesimals, `1_000`, ...). Stringifying
|
|
6
|
+
* with the core schema plus these quotes every string either a YAML 1.2 core reader or a YAML 1.1 reader would read
|
|
7
|
+
* as another type; `version: '1.1'` alone would leave e.g. `0o17` plain, which core reads as a number.
|
|
8
|
+
*/
|
|
9
|
+
const YAML_11_SCALARS = new Schema({ schema: "yaml-1.1" }).tags.filter((tag) => !tag.collection);
|
|
10
|
+
function fail(message) {
|
|
11
|
+
throw new EditletError([{
|
|
12
|
+
path: "",
|
|
13
|
+
message
|
|
14
|
+
}]);
|
|
15
|
+
}
|
|
16
|
+
const utf8 = new TextDecoder("utf-8", { fatal: true });
|
|
17
|
+
/** Decodes file content as UTF-8 (strings pass through); on invalid bytes returns an issue for `file` instead. */
|
|
18
|
+
function decodeUtf8(content, file) {
|
|
19
|
+
if (typeof content === "string") return { text: content };
|
|
20
|
+
try {
|
|
21
|
+
return { text: utf8.decode(content) };
|
|
22
|
+
} catch {
|
|
23
|
+
return { issue: {
|
|
24
|
+
file,
|
|
25
|
+
path: "",
|
|
26
|
+
message: "File is not valid UTF-8"
|
|
27
|
+
} };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function parseEntryFile(source) {
|
|
31
|
+
let text = source.startsWith("") ? source.slice(1) : source;
|
|
32
|
+
text = text.replace(/\r\n/g, "\n");
|
|
33
|
+
if (!text.startsWith("---\n")) return {
|
|
34
|
+
data: {},
|
|
35
|
+
body: text
|
|
36
|
+
};
|
|
37
|
+
const lines = text.slice(4).split("\n");
|
|
38
|
+
const close = lines.indexOf("---");
|
|
39
|
+
if (close === -1) fail("Frontmatter is not closed with a \"---\" line");
|
|
40
|
+
const yamlSrc = lines.slice(0, close).join("\n");
|
|
41
|
+
let body = lines.slice(close + 1).join("\n");
|
|
42
|
+
if (body.startsWith("\n")) body = body.slice(1);
|
|
43
|
+
let data;
|
|
44
|
+
try {
|
|
45
|
+
data = parse(yamlSrc, {
|
|
46
|
+
schema: "core",
|
|
47
|
+
logLevel: "error"
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
fail(`Invalid YAML frontmatter: ${error instanceof Error ? error.message : String(error)}`);
|
|
51
|
+
}
|
|
52
|
+
if (data === null || data === void 0) data = {};
|
|
53
|
+
if (typeof data !== "object" || Array.isArray(data)) fail("Frontmatter must be a YAML mapping");
|
|
54
|
+
return {
|
|
55
|
+
data,
|
|
56
|
+
body
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function serializeEntryFile(file, fieldOrder = []) {
|
|
60
|
+
const keys = Object.keys(file.data).filter((k) => file.data[k] !== void 0);
|
|
61
|
+
const rank = (k) => {
|
|
62
|
+
const i = fieldOrder.indexOf(k);
|
|
63
|
+
return i === -1 ? 1e400 : i;
|
|
64
|
+
};
|
|
65
|
+
keys.sort((a, b) => {
|
|
66
|
+
const d = rank(a) - rank(b);
|
|
67
|
+
if (d !== 0 && !(rank(a) === 1e400 && rank(b) === 1e400)) return d;
|
|
68
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
69
|
+
});
|
|
70
|
+
const ordered = {};
|
|
71
|
+
for (const k of keys) ordered[k] = file.data[k];
|
|
72
|
+
const head = `---\n${keys.length === 0 ? "" : stringify(ordered, {
|
|
73
|
+
version: "1.1",
|
|
74
|
+
schema: "core",
|
|
75
|
+
customTags: (tags) => [...tags, ...YAML_11_SCALARS],
|
|
76
|
+
lineWidth: 0,
|
|
77
|
+
logLevel: "error"
|
|
78
|
+
})}---\n`;
|
|
79
|
+
return file.body === "" ? head : `${head}\n${file.body}`;
|
|
80
|
+
}
|
|
81
|
+
export { decodeUtf8, parseEntryFile, serializeEntryFile };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { SLUG_RE } from "./paths.js";
|
|
2
|
+
import { BLOCK_KEY } from "./schema.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
// core/fields.ts
|
|
5
|
+
/** Field kinds whose value is a single string. Only for these an empty string means "no value". */
|
|
6
|
+
function isStringValued(field) {
|
|
7
|
+
switch (field.type) {
|
|
8
|
+
case "string": return !field.list;
|
|
9
|
+
case "select":
|
|
10
|
+
case "reference": return !field.multiple;
|
|
11
|
+
case "text":
|
|
12
|
+
case "datetime":
|
|
13
|
+
case "url":
|
|
14
|
+
case "color": return true;
|
|
15
|
+
default: return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The one "absent" rule: `undefined`, `null`, or `''` for a string-valued field. Validation accepts exactly these
|
|
20
|
+
* for an optional field (and reports "Required" for a required one); `normalizeData` drops them.
|
|
21
|
+
*/
|
|
22
|
+
function isAbsentValue(field, value) {
|
|
23
|
+
return value === void 0 || value === null || value === "" && isStringValued(field);
|
|
24
|
+
}
|
|
25
|
+
/** Error map: absent-looking input reads as "Required" only when the field is required. */
|
|
26
|
+
function err(field, message) {
|
|
27
|
+
return (iss) => field.required && isAbsentValue(field, iss.input) ? "Required" : message;
|
|
28
|
+
}
|
|
29
|
+
/** Optional values: absent per `isAbsentValue` is fine. */
|
|
30
|
+
function optional(field, schema, message) {
|
|
31
|
+
if (field.required) return schema;
|
|
32
|
+
return isStringValued(field) ? z.union([schema, z.literal("")], { error: message }).nullish() : schema.nullish();
|
|
33
|
+
}
|
|
34
|
+
function isUrl(v) {
|
|
35
|
+
return v.startsWith("/") || URL.canParse(v);
|
|
36
|
+
}
|
|
37
|
+
/** Reports each repeat of an earlier item at its own index (`related.2`), named by `message`. */
|
|
38
|
+
function noRepeats(message) {
|
|
39
|
+
return (items, ctx) => {
|
|
40
|
+
const seen = /* @__PURE__ */ new Set();
|
|
41
|
+
items.forEach((item, i) => {
|
|
42
|
+
if (seen.has(item)) ctx.addIssue({
|
|
43
|
+
code: "custom",
|
|
44
|
+
path: [i],
|
|
45
|
+
message: message(item)
|
|
46
|
+
});
|
|
47
|
+
seen.add(item);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function listField(field, item, min, max, unique) {
|
|
52
|
+
const effective = Math.max(field.required ? 1 : 0, min);
|
|
53
|
+
let arr = z.array(item, { error: err(field, "Expected a list") });
|
|
54
|
+
if (effective > 0) arr = arr.min(effective, { error: (iss) => Array.isArray(iss.input) && iss.input.length === 0 && field.required ? "Required" : `Must have at least ${effective}` });
|
|
55
|
+
if (max !== void 0) arr = arr.max(max, { error: `Must have at most ${max}` });
|
|
56
|
+
return optional(field, unique ? arr.superRefine(noRepeats(unique)) : arr, "Expected a list");
|
|
57
|
+
}
|
|
58
|
+
const escapeRegex = (v) => v.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
59
|
+
/** Strict object of `fields` (inside an `object` or a block): a `markdown` field there is a Markdown string. */
|
|
60
|
+
function fieldsShape(fields) {
|
|
61
|
+
const shape = {};
|
|
62
|
+
for (const [key, field] of Object.entries(fields)) shape[key] = nestedValueSchema(field);
|
|
63
|
+
return shape;
|
|
64
|
+
}
|
|
65
|
+
/** A field's value schema inside an `object` or a block, where `markdown` is a string like `text`. */
|
|
66
|
+
function nestedValueSchema(field) {
|
|
67
|
+
if (field.type !== "markdown") return fieldValueSchema(field);
|
|
68
|
+
const base = z.string({ error: err(field, "Expected text") });
|
|
69
|
+
return field.required ? base.regex(/\S/, "Required") : base.nullish();
|
|
70
|
+
}
|
|
71
|
+
/** Zod schema of one field's frontmatter value; `null` for the markdown field (it is the body). */
|
|
72
|
+
function fieldValueSchema(field) {
|
|
73
|
+
switch (field.type) {
|
|
74
|
+
case "markdown": return null;
|
|
75
|
+
case "object": {
|
|
76
|
+
const obj = z.strictObject(fieldsShape(field.fields), { error: err(field, "Expected an object") });
|
|
77
|
+
if (field.list) return listField(field, obj, field.min ?? 0, field.max);
|
|
78
|
+
return optional(field, obj, "");
|
|
79
|
+
}
|
|
80
|
+
case "blocks": {
|
|
81
|
+
const kinds = Object.keys(field.blocks);
|
|
82
|
+
const message = `Expected ${BLOCK_KEY}: ${kinds.join(", ")}`;
|
|
83
|
+
const options = Object.entries(field.blocks).map(([kind, block]) => z.strictObject({
|
|
84
|
+
[BLOCK_KEY]: z.literal(kind),
|
|
85
|
+
...fieldsShape(block.fields)
|
|
86
|
+
}));
|
|
87
|
+
return listField(field, z.discriminatedUnion(BLOCK_KEY, options, { error: message }), field.min ?? 0, field.max);
|
|
88
|
+
}
|
|
89
|
+
case "string":
|
|
90
|
+
case "text": {
|
|
91
|
+
if (field.type === "string" && field.list) return listField(field, z.string({ error: "Expected text" }), 0);
|
|
92
|
+
const base = z.string({ error: err(field, "Expected text") });
|
|
93
|
+
return field.required ? base.regex(/\S/, "Required") : base.nullish();
|
|
94
|
+
}
|
|
95
|
+
case "boolean": return optional(field, z.boolean({ error: err(field, "Expected true or false") }), "");
|
|
96
|
+
case "select": {
|
|
97
|
+
const values = field.options.map((o) => o.value);
|
|
98
|
+
const message = `Expected one of: ${values.join(", ")}`;
|
|
99
|
+
if (field.multiple) return listField(field, z.enum(values, { error: message }), 0);
|
|
100
|
+
return optional(field, z.enum(values, { error: err(field, message) }), message);
|
|
101
|
+
}
|
|
102
|
+
case "number": {
|
|
103
|
+
let n = z.number({ error: err(field, "Expected a number") });
|
|
104
|
+
if (field.integer) n = n.int("Must be a whole number");
|
|
105
|
+
if (field.min !== void 0) n = n.min(field.min, `Must be at least ${field.min}`);
|
|
106
|
+
if (field.max !== void 0) n = n.max(field.max, `Must be at most ${field.max}`);
|
|
107
|
+
return optional(field, n, "");
|
|
108
|
+
}
|
|
109
|
+
case "datetime": return optional(field, field.dateOnly ? z.iso.date({ error: err(field, "Invalid date") }) : z.iso.datetime({
|
|
110
|
+
offset: true,
|
|
111
|
+
local: true,
|
|
112
|
+
error: err(field, "Invalid date")
|
|
113
|
+
}), "Invalid date");
|
|
114
|
+
case "json":
|
|
115
|
+
if (!field.required) return z.unknown().optional();
|
|
116
|
+
return z.union([
|
|
117
|
+
z.string(),
|
|
118
|
+
z.number(),
|
|
119
|
+
z.boolean(),
|
|
120
|
+
z.array(z.unknown()),
|
|
121
|
+
z.record(z.string(), z.unknown())
|
|
122
|
+
], { error: "Required" });
|
|
123
|
+
case "url": {
|
|
124
|
+
const message = "Must be an http(s) URL or start with /";
|
|
125
|
+
return optional(field, z.string({ error: err(field, "Expected text") }).regex(/^(?:https?:\/\/|\/(?!\/))/i, { error: err(field, message) }).refine(isUrl, { error: message }), message);
|
|
126
|
+
}
|
|
127
|
+
case "color": {
|
|
128
|
+
const message = "Must be a hex color like #a1b2c3";
|
|
129
|
+
return optional(field, z.string({ error: err(field, "Expected text") }).regex(/^#[0-9a-fA-F]{6}$/, { error: err(field, message) }), message);
|
|
130
|
+
}
|
|
131
|
+
case "image": {
|
|
132
|
+
const nonEmpty = z.string({ error: (iss) => iss.input === void 0 || iss.input === null ? "Required" : "Expected text" }).regex(/\S/, "Required");
|
|
133
|
+
return optional(field, z.strictObject({
|
|
134
|
+
src: nonEmpty,
|
|
135
|
+
alt: nonEmpty
|
|
136
|
+
}, { error: err(field, "Expected { src, alt }") }), "");
|
|
137
|
+
}
|
|
138
|
+
case "reference": {
|
|
139
|
+
const message = `Must reference an entry in: ${field.collections.join(", ")}`;
|
|
140
|
+
const slug = SLUG_RE.source.slice(1, -1);
|
|
141
|
+
const ref = z.string({ error: err(field, "Expected a reference like \"collection/slug\"") }).regex(new RegExp(`^(?:${field.collections.map(escapeRegex).join("|")})/${slug}$`), { error: err(field, message) });
|
|
142
|
+
if (field.multiple) return listField(field, ref, field.min ?? 0, field.max, (r) => `Duplicate reference: ${r}`);
|
|
143
|
+
return optional(field, ref, message);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export { fieldValueSchema, isAbsentValue, nestedValueSchema };
|