@mainsoft/agent-cms 0.0.0-stage → 0.7.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.
- package/CHANGELOG.md +54 -0
- package/LICENSE +21 -0
- package/README.md +359 -2
- package/TODO.md +178 -0
- package/UPGRADE-0.6.md +236 -0
- package/dist/agent.cjs +250 -0
- package/dist/agent.d.cts +88 -0
- package/dist/agent.d.ts +88 -0
- package/dist/agent.js +24 -0
- package/dist/ai.cjs +555 -0
- package/dist/ai.d.cts +48 -0
- package/dist/ai.d.ts +48 -0
- package/dist/ai.js +73 -0
- package/dist/bin.js +1556 -0
- package/dist/chunk-2NTK7H4W.js +10 -0
- package/dist/chunk-52NUUZC7.js +1446 -0
- package/dist/chunk-6K5ZTGHQ.js +30 -0
- package/dist/chunk-AKN7SUXU.js +281 -0
- package/dist/chunk-CRDW6Q72.js +188 -0
- package/dist/chunk-DD56PTZ4.js +78 -0
- package/dist/chunk-DOADAH4L.js +52 -0
- package/dist/chunk-EXV6JNLY.js +251 -0
- package/dist/chunk-IGL2V6US.js +324 -0
- package/dist/chunk-IW3D4I7Q.js +317 -0
- package/dist/chunk-OQ55GEF3.js +6 -0
- package/dist/chunk-QC2M4NZY.js +158 -0
- package/dist/chunk-QQV6VLYJ.js +204 -0
- package/dist/chunk-T66ZKBHF.js +25 -0
- package/dist/chunk-VICZ76TR.js +118 -0
- package/dist/chunk-Y63XQS2J.js +6 -0
- package/dist/cli.cjs +1596 -0
- package/dist/cli.d.cts +89 -0
- package/dist/cli.d.ts +89 -0
- package/dist/cli.js +534 -0
- package/dist/client.cjs +185 -0
- package/dist/client.d.cts +228 -0
- package/dist/client.d.ts +228 -0
- package/dist/client.js +13 -0
- package/dist/cms-BPp01z-8.d.ts +411 -0
- package/dist/cms-D6D89-qZ.d.cts +411 -0
- package/dist/config.cjs +61 -0
- package/dist/config.d.cts +36 -0
- package/dist/config.d.ts +36 -0
- package/dist/config.js +11 -0
- package/dist/diff.cjs +316 -0
- package/dist/diff.d.cts +122 -0
- package/dist/diff.d.ts +122 -0
- package/dist/diff.js +29 -0
- package/dist/drizzle-repo-B1rt7r4j.d.ts +90 -0
- package/dist/drizzle-repo-Cn-kL2FK.d.cts +90 -0
- package/dist/errors.cjs +193 -0
- package/dist/errors.d.cts +109 -0
- package/dist/errors.d.ts +109 -0
- package/dist/errors.js +16 -0
- package/dist/frontmatter.cjs +103 -0
- package/dist/frontmatter.d.cts +32 -0
- package/dist/frontmatter.d.ts +32 -0
- package/dist/frontmatter.js +9 -0
- package/dist/http.cjs +1105 -0
- package/dist/http.d.cts +75 -0
- package/dist/http.d.ts +75 -0
- package/dist/http.js +321 -0
- package/dist/index.cjs +3066 -0
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +263 -0
- package/dist/markdown.cjs +220 -0
- package/dist/markdown.d.cts +88 -0
- package/dist/markdown.d.ts +88 -0
- package/dist/markdown.js +23 -0
- package/dist/mcp.cjs +652 -0
- package/dist/mcp.d.cts +43 -0
- package/dist/mcp.d.ts +43 -0
- package/dist/mcp.js +26 -0
- package/dist/messages/en.cjs +76 -0
- package/dist/messages/en.d.cts +7 -0
- package/dist/messages/en.d.ts +7 -0
- package/dist/messages/en.js +7 -0
- package/dist/messages/ko.cjs +76 -0
- package/dist/messages/ko.d.cts +7 -0
- package/dist/messages/ko.d.ts +7 -0
- package/dist/messages/ko.js +53 -0
- package/dist/registry/cms-audit.json +25 -0
- package/dist/registry/cms-command.json +23 -0
- package/dist/registry/cms-doc-editor.json +36 -0
- package/dist/registry/cms-doc-list.json +29 -0
- package/dist/registry/cms-empty.json +22 -0
- package/dist/registry/cms-keys.json +29 -0
- package/dist/registry/cms-lib.json +33 -0
- package/dist/registry/cms-merge.json +26 -0
- package/dist/registry/cms-shell.json +22 -0
- package/dist/registry/cms-versions.json +26 -0
- package/dist/registry/registry.json +255 -0
- package/dist/repo-BWI2Pkrv.d.ts +162 -0
- package/dist/repo-CeDxUxO6.d.cts +162 -0
- package/dist/tables.cjs +356 -0
- package/dist/tables.d.cts +3515 -0
- package/dist/tables.d.ts +3515 -0
- package/dist/tables.js +35 -0
- package/dist/tools.cjs +420 -0
- package/dist/tools.d.cts +27 -0
- package/dist/tools.d.ts +27 -0
- package/dist/tools.js +18 -0
- package/dist/types.cjs +226 -0
- package/dist/types.d.cts +286 -0
- package/dist/types.d.ts +286 -0
- package/dist/types.js +28 -0
- package/package.json +277 -4
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var DOC_STATUSES = ["draft", "published", "archived"];
|
|
3
|
+
var KEY_SCOPES = ["read", "write", "publish", "admin"];
|
|
4
|
+
var VIAS = ["rest", "mcp", "ai", "cli", "app"];
|
|
5
|
+
var AUDIT_ACTIONS = [
|
|
6
|
+
"key.create",
|
|
7
|
+
"key.revoke",
|
|
8
|
+
"doc.publish",
|
|
9
|
+
"doc.unpublish",
|
|
10
|
+
"doc.archive",
|
|
11
|
+
"doc.unarchive",
|
|
12
|
+
"doc.delete",
|
|
13
|
+
"doc.restore",
|
|
14
|
+
"preview.create",
|
|
15
|
+
"access.denied",
|
|
16
|
+
"auth.failed"
|
|
17
|
+
];
|
|
18
|
+
function docState(doc) {
|
|
19
|
+
if (doc.status === "archived") return { state: "archived", modifiedSincePublish: false };
|
|
20
|
+
if (doc.publishedVersion === null) return { state: "draft", modifiedSincePublish: false };
|
|
21
|
+
return { state: "published", modifiedSincePublish: doc.version !== doc.publishedVersion };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
DOC_STATUSES,
|
|
26
|
+
KEY_SCOPES,
|
|
27
|
+
VIAS,
|
|
28
|
+
AUDIT_ACTIONS,
|
|
29
|
+
docState
|
|
30
|
+
};
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// src/diff.ts
|
|
2
|
+
function diffSeq(a, b, eq = Object.is) {
|
|
3
|
+
let start = 0;
|
|
4
|
+
while (start < a.length && start < b.length && eq(a[start], b[start])) start++;
|
|
5
|
+
let endA = a.length;
|
|
6
|
+
let endB = b.length;
|
|
7
|
+
while (endA > start && endB > start && eq(a[endA - 1], b[endB - 1])) {
|
|
8
|
+
endA--;
|
|
9
|
+
endB--;
|
|
10
|
+
}
|
|
11
|
+
const A = a.slice(start, endA);
|
|
12
|
+
const B = b.slice(start, endB);
|
|
13
|
+
const n = A.length;
|
|
14
|
+
const m = B.length;
|
|
15
|
+
const max = n + m;
|
|
16
|
+
const v = /* @__PURE__ */ new Map([[1, 0]]);
|
|
17
|
+
const trace = [];
|
|
18
|
+
outer: for (let d = 0; d <= max; d++) {
|
|
19
|
+
trace.push(new Map(v));
|
|
20
|
+
for (let k = -d; k <= d; k += 2) {
|
|
21
|
+
let x2 = k === -d || k !== d && (v.get(k - 1) ?? -1) < (v.get(k + 1) ?? -1) ? v.get(k + 1) ?? 0 : (v.get(k - 1) ?? 0) + 1;
|
|
22
|
+
let y2 = x2 - k;
|
|
23
|
+
while (x2 < n && y2 < m && eq(A[x2], B[y2])) {
|
|
24
|
+
x2++;
|
|
25
|
+
y2++;
|
|
26
|
+
}
|
|
27
|
+
v.set(k, x2);
|
|
28
|
+
if (x2 >= n && y2 >= m) break outer;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const mid = [];
|
|
32
|
+
let x = n;
|
|
33
|
+
let y = m;
|
|
34
|
+
for (let d = trace.length - 1; d >= 0 && (x > 0 || y > 0); d--) {
|
|
35
|
+
const vd = trace[d];
|
|
36
|
+
const k = x - y;
|
|
37
|
+
const prevK = k === -d || k !== d && (vd.get(k - 1) ?? -1) < (vd.get(k + 1) ?? -1) ? k + 1 : k - 1;
|
|
38
|
+
const prevX = vd.get(prevK) ?? 0;
|
|
39
|
+
const prevY = prevX - prevK;
|
|
40
|
+
while (x > prevX && y > prevY) {
|
|
41
|
+
x--;
|
|
42
|
+
y--;
|
|
43
|
+
mid.push({ t: "eq", a: x, b: y, v: A[x] });
|
|
44
|
+
}
|
|
45
|
+
if (d > 0) {
|
|
46
|
+
if (x === prevX) {
|
|
47
|
+
y--;
|
|
48
|
+
mid.push({ t: "ins", a: x, b: y, v: B[y] });
|
|
49
|
+
} else {
|
|
50
|
+
x--;
|
|
51
|
+
mid.push({ t: "del", a: x, b: y, v: A[x] });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
mid.reverse();
|
|
56
|
+
const out = [];
|
|
57
|
+
for (let i = 0; i < start; i++) out.push({ t: "eq", a: i, b: i, v: a[i] });
|
|
58
|
+
for (const o of mid) out.push({ ...o, a: o.a + start, b: o.b + start });
|
|
59
|
+
for (let i = 0; i < a.length - endA; i++) out.push({ t: "eq", a: endA + i, b: endB + i, v: a[endA + i] });
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
var splitLines = (s) => s === "" ? [] : s.replace(/\r\n?/g, "\n").split("\n");
|
|
63
|
+
function diffLines(a, b) {
|
|
64
|
+
return diffSeq(splitLines(a), splitLines(b));
|
|
65
|
+
}
|
|
66
|
+
function diffWords(a, b) {
|
|
67
|
+
const tok = (s) => s.match(/\s+|[\p{L}\p{N}_]+|[^\s\p{L}\p{N}_]/gu) ?? [];
|
|
68
|
+
const ops = diffSeq(tok(a), tok(b));
|
|
69
|
+
const out = [];
|
|
70
|
+
for (const o of ops) {
|
|
71
|
+
const last = out[out.length - 1];
|
|
72
|
+
if (last && last.t === o.t) last.v += o.v;
|
|
73
|
+
else out.push({ t: o.t, v: o.v });
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
function hunks(a, b, context = 3) {
|
|
78
|
+
const ops = diffLines(a, b);
|
|
79
|
+
const changed = ops.map((o, i) => o.t === "eq" ? -1 : i).filter((i) => i >= 0);
|
|
80
|
+
if (!changed.length) return [];
|
|
81
|
+
const groups = [];
|
|
82
|
+
for (const i of changed) {
|
|
83
|
+
const last = groups[groups.length - 1];
|
|
84
|
+
if (last && i - last[1] <= context * 2 + 1) last[1] = i;
|
|
85
|
+
else groups.push([i, i]);
|
|
86
|
+
}
|
|
87
|
+
return groups.map(([from, to]) => {
|
|
88
|
+
const s = Math.max(0, from - context);
|
|
89
|
+
const e = Math.min(ops.length - 1, to + context);
|
|
90
|
+
const lines = [];
|
|
91
|
+
const removed = [];
|
|
92
|
+
const added = [];
|
|
93
|
+
for (let i = s; i <= e; i++) {
|
|
94
|
+
const o = ops[i];
|
|
95
|
+
if (o.t === "eq") lines.push({ t: " ", v: o.v, a: o.a + 1, b: o.b + 1 });
|
|
96
|
+
else if (o.t === "del") {
|
|
97
|
+
lines.push({ t: "-", v: o.v, a: o.a + 1 });
|
|
98
|
+
removed.push(o.v);
|
|
99
|
+
} else {
|
|
100
|
+
lines.push({ t: "+", v: o.v, b: o.b + 1 });
|
|
101
|
+
added.push(o.v);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const first = ops[s];
|
|
105
|
+
const aLines = lines.filter((l) => l.t !== "+").length;
|
|
106
|
+
const bLines = lines.filter((l) => l.t !== "-").length;
|
|
107
|
+
return { aStart: aLines ? first.a + 1 : first.a, aLines, bStart: bLines ? first.b + 1 : first.b, bLines, lines, removed, added };
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function unifiedDiff(a, b, o = {}) {
|
|
111
|
+
const hs = hunks(a, b, o.context ?? 3);
|
|
112
|
+
if (!hs.length) return "";
|
|
113
|
+
const out = [`--- ${o.from ?? "a"}`, `+++ ${o.to ?? "b"}`];
|
|
114
|
+
for (const h of hs) {
|
|
115
|
+
out.push(`@@ -${h.aStart},${h.aLines} +${h.bStart},${h.bLines} @@`);
|
|
116
|
+
for (const l of h.lines) out.push(l.t + l.v);
|
|
117
|
+
}
|
|
118
|
+
return out.join("\n");
|
|
119
|
+
}
|
|
120
|
+
function matchMap(base, other) {
|
|
121
|
+
const m = new Array(base.length).fill(-1);
|
|
122
|
+
for (const o of diffSeq(base, other)) if (o.t === "eq") m[o.a] = o.b;
|
|
123
|
+
return m;
|
|
124
|
+
}
|
|
125
|
+
var sameLines = (x, y) => x.length === y.length && x.every((l, i) => l === y[i]);
|
|
126
|
+
function merge3(base, mine, theirs) {
|
|
127
|
+
const O = splitLines(base);
|
|
128
|
+
const A = splitLines(mine);
|
|
129
|
+
const B = splitLines(theirs);
|
|
130
|
+
const ma = matchMap(O, A);
|
|
131
|
+
const mb = matchMap(O, B);
|
|
132
|
+
const chunks = [];
|
|
133
|
+
const conflicts = [];
|
|
134
|
+
const pushOk = (lines, from) => {
|
|
135
|
+
if (!lines.length) return;
|
|
136
|
+
const last = chunks[chunks.length - 1];
|
|
137
|
+
if (last && last.t === "ok" && last.from === from) last.lines.push(...lines);
|
|
138
|
+
else chunks.push({ t: "ok", lines: [...lines], from });
|
|
139
|
+
};
|
|
140
|
+
let io = 0;
|
|
141
|
+
let ia = 0;
|
|
142
|
+
let ib = 0;
|
|
143
|
+
while (io < O.length || ia < A.length || ib < B.length) {
|
|
144
|
+
if (io < O.length && ma[io] === ia && mb[io] === ib) {
|
|
145
|
+
pushOk([O[io]], "base");
|
|
146
|
+
io++;
|
|
147
|
+
ia++;
|
|
148
|
+
ib++;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
let o = io;
|
|
152
|
+
while (o < O.length && !(ma[o] >= ia && mb[o] >= ib)) o++;
|
|
153
|
+
const ea = o < O.length ? ma[o] : A.length;
|
|
154
|
+
const eb = o < O.length ? mb[o] : B.length;
|
|
155
|
+
const oc = O.slice(io, o);
|
|
156
|
+
const ac = A.slice(ia, ea);
|
|
157
|
+
const bc = B.slice(ib, eb);
|
|
158
|
+
if (sameLines(ac, oc)) pushOk(bc, "theirs");
|
|
159
|
+
else if (sameLines(bc, oc)) pushOk(ac, "mine");
|
|
160
|
+
else if (sameLines(ac, bc)) pushOk(ac, "both");
|
|
161
|
+
else {
|
|
162
|
+
const c = { t: "conflict", id: conflicts.length, base: oc, mine: ac, theirs: bc };
|
|
163
|
+
chunks.push(c);
|
|
164
|
+
conflicts.push(c);
|
|
165
|
+
}
|
|
166
|
+
io = o;
|
|
167
|
+
ia = ea;
|
|
168
|
+
ib = eb;
|
|
169
|
+
}
|
|
170
|
+
return { chunks, conflicts, text: conflicts.length ? null : chunks.flatMap((c) => c.t === "ok" ? c.lines : []).join("\n") };
|
|
171
|
+
}
|
|
172
|
+
function resolveMerge(r, choices) {
|
|
173
|
+
const out = [];
|
|
174
|
+
for (const c of r.chunks) {
|
|
175
|
+
if (c.t === "ok") out.push(...c.lines);
|
|
176
|
+
else {
|
|
177
|
+
const pick = choices[c.id];
|
|
178
|
+
if (!pick) throw new Error(`conflict ${c.id} has no choice`);
|
|
179
|
+
if (pick === "theirs") out.push(...c.theirs);
|
|
180
|
+
else if (pick === "mine") out.push(...c.mine);
|
|
181
|
+
else out.push(...c.theirs, ...c.mine);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return out.join("\n");
|
|
185
|
+
}
|
|
186
|
+
var stable = (v) => JSON.stringify(v, (_k, x) => x && typeof x === "object" && !Array.isArray(x) ? Object.fromEntries(Object.keys(x).sort().map((k) => [k, x[k]])) : x);
|
|
187
|
+
function flattenFields(v, prefix = "") {
|
|
188
|
+
const out = {};
|
|
189
|
+
for (const [k, x] of Object.entries(v ?? {})) {
|
|
190
|
+
const p = prefix ? `${prefix}.${k}` : k;
|
|
191
|
+
if (x && typeof x === "object" && !Array.isArray(x) && Object.keys(x).length) Object.assign(out, flattenFields(x, p));
|
|
192
|
+
else out[p] = x;
|
|
193
|
+
}
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
function unflatten(flat) {
|
|
197
|
+
const out = {};
|
|
198
|
+
for (const [p, v] of Object.entries(flat)) {
|
|
199
|
+
if (v === void 0) continue;
|
|
200
|
+
const parts = p.split(".");
|
|
201
|
+
let cur = out;
|
|
202
|
+
for (const part of parts.slice(0, -1)) cur = cur[part] ??= {};
|
|
203
|
+
cur[parts[parts.length - 1]] = v;
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
function fieldChanges(a, b) {
|
|
208
|
+
const out = [];
|
|
209
|
+
for (const k of ["slug", "title", "summary", "category", "keywords"]) {
|
|
210
|
+
if (stable(a[k]) !== stable(b[k])) out.push({ path: k, from: a[k], to: b[k] });
|
|
211
|
+
}
|
|
212
|
+
const fa = flattenFields(a.fields);
|
|
213
|
+
const fb = flattenFields(b.fields);
|
|
214
|
+
for (const p of [.../* @__PURE__ */ new Set([...Object.keys(fa), ...Object.keys(fb)])].sort()) {
|
|
215
|
+
if (stable(fa[p]) !== stable(fb[p])) out.push({ path: `fields.${p}`, from: fa[p], to: fb[p] });
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
function mergeDocs(base, mine, theirs) {
|
|
220
|
+
const fieldConflicts = [];
|
|
221
|
+
const autoMerged = [];
|
|
222
|
+
const pick = (path, o, a, b) => {
|
|
223
|
+
const so = stable(o);
|
|
224
|
+
const sa = stable(a);
|
|
225
|
+
const sb = stable(b);
|
|
226
|
+
if (sa === sb) {
|
|
227
|
+
if (sa !== so) autoMerged.push({ path, from: "both" });
|
|
228
|
+
return a;
|
|
229
|
+
}
|
|
230
|
+
if (sa === so) {
|
|
231
|
+
autoMerged.push({ path, from: "theirs" });
|
|
232
|
+
return b;
|
|
233
|
+
}
|
|
234
|
+
if (sb === so) {
|
|
235
|
+
autoMerged.push({ path, from: "mine" });
|
|
236
|
+
return a;
|
|
237
|
+
}
|
|
238
|
+
fieldConflicts.push({ path, base: o, mine: a, theirs: b });
|
|
239
|
+
return b;
|
|
240
|
+
};
|
|
241
|
+
const merged = { ...theirs };
|
|
242
|
+
for (const k of ["slug", "title", "summary", "category", "keywords"]) merged[k] = pick(k, base[k], mine[k], theirs[k]);
|
|
243
|
+
const fo = flattenFields(base.fields);
|
|
244
|
+
const fa = flattenFields(mine.fields);
|
|
245
|
+
const fb = flattenFields(theirs.fields);
|
|
246
|
+
const flat = {};
|
|
247
|
+
for (const p of [.../* @__PURE__ */ new Set([...Object.keys(fo), ...Object.keys(fa), ...Object.keys(fb)])].sort()) flat[p] = pick(`fields.${p}`, fo[p], fa[p], fb[p]);
|
|
248
|
+
merged.fields = unflatten(flat);
|
|
249
|
+
const body = merge3(base.body, mine.body, theirs.body);
|
|
250
|
+
merged.body = body.text ?? theirs.body;
|
|
251
|
+
for (const c of body.chunks) if (c.t === "ok" && c.from !== "base") autoMerged.push({ path: "body", from: c.from });
|
|
252
|
+
return { merged, body, fieldConflicts, autoMerged };
|
|
253
|
+
}
|
|
254
|
+
function resolveDocMerge(m, choices) {
|
|
255
|
+
const out = { ...m.merged, fields: structuredClone(m.merged.fields) };
|
|
256
|
+
out.body = m.body.conflicts.length ? resolveMerge(m.body, choices.body ?? {}) : m.body.text ?? out.body;
|
|
257
|
+
for (const c of m.fieldConflicts) {
|
|
258
|
+
const v = (choices.fields?.[c.path] ?? "theirs") === "mine" ? c.mine : c.theirs;
|
|
259
|
+
if (c.path.startsWith("fields.")) {
|
|
260
|
+
const flat = flattenFields(out.fields);
|
|
261
|
+
flat[c.path.slice(7)] = v;
|
|
262
|
+
out.fields = unflatten(flat);
|
|
263
|
+
} else out[c.path] = v;
|
|
264
|
+
}
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export {
|
|
269
|
+
diffSeq,
|
|
270
|
+
splitLines,
|
|
271
|
+
diffLines,
|
|
272
|
+
diffWords,
|
|
273
|
+
hunks,
|
|
274
|
+
unifiedDiff,
|
|
275
|
+
merge3,
|
|
276
|
+
resolveMerge,
|
|
277
|
+
flattenFields,
|
|
278
|
+
fieldChanges,
|
|
279
|
+
mergeDocs,
|
|
280
|
+
resolveDocMerge
|
|
281
|
+
};
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// src/markdown.ts
|
|
2
|
+
var APP_LINK_RE = /\[\[link:([a-z0-9][a-z0-9._-]*)\|([^\]]+)\]\]/gi;
|
|
3
|
+
function anchorOf(text) {
|
|
4
|
+
const base = text.trim().toLowerCase().replace(/[`*_[\]()<>!?.,:;'"“”‘’/\\|]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "");
|
|
5
|
+
return base || "section";
|
|
6
|
+
}
|
|
7
|
+
function parseInline(src) {
|
|
8
|
+
const out = [];
|
|
9
|
+
const re = /(\[\[link:([a-z0-9][a-z0-9._-]*)\|([^\]]+)\]\]|\*\*([^*]+)\*\*|`([^`]+)`|\[([^\]]+)\]\(((?:[^()\s]|\([^()\s]*\))+)\))/gi;
|
|
10
|
+
let last = 0;
|
|
11
|
+
let m;
|
|
12
|
+
while (m = re.exec(src)) {
|
|
13
|
+
if (m.index > last) out.push({ t: "text", v: src.slice(last, m.index) });
|
|
14
|
+
if (m[2]) out.push({ t: "applink", key: m[2], v: m[3] });
|
|
15
|
+
else if (m[4] !== void 0) out.push({ t: "strong", v: m[4] });
|
|
16
|
+
else if (m[5] !== void 0) out.push({ t: "code", v: m[5] });
|
|
17
|
+
else if (m[6] !== void 0) {
|
|
18
|
+
const href = m[7];
|
|
19
|
+
if (/^(https?:\/\/|mailto:|\/(?!\/)|#)/i.test(href)) out.push({ t: "link", href, v: m[6] });
|
|
20
|
+
else out.push({ t: "text", v: m[6] });
|
|
21
|
+
}
|
|
22
|
+
last = m.index + m[0].length;
|
|
23
|
+
}
|
|
24
|
+
if (last < src.length) out.push({ t: "text", v: src.slice(last) });
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
function inlineText(inline) {
|
|
28
|
+
return inline.map((i) => i.v).join("");
|
|
29
|
+
}
|
|
30
|
+
function parseMarkdown(source) {
|
|
31
|
+
const lines = source.replace(/\r/g, "").split("\n");
|
|
32
|
+
const blocks = [];
|
|
33
|
+
const used = /* @__PURE__ */ new Map();
|
|
34
|
+
let para = [];
|
|
35
|
+
let list = null;
|
|
36
|
+
const uniqueId = (text) => {
|
|
37
|
+
const base = anchorOf(text);
|
|
38
|
+
const n = used.get(base) ?? 0;
|
|
39
|
+
used.set(base, n + 1);
|
|
40
|
+
return n ? `${base}-${n}` : base;
|
|
41
|
+
};
|
|
42
|
+
const flushPara = () => {
|
|
43
|
+
if (!para.length) return;
|
|
44
|
+
const text = para.join(" ").trim();
|
|
45
|
+
para = [];
|
|
46
|
+
if (!text) return;
|
|
47
|
+
const inline = parseInline(text);
|
|
48
|
+
const onlyLinks = inline.every((i) => i.t === "applink" || i.t === "text" && !i.v.trim());
|
|
49
|
+
if (onlyLinks && inline.some((i) => i.t === "applink")) {
|
|
50
|
+
blocks.push({
|
|
51
|
+
t: "links",
|
|
52
|
+
items: inline.filter((i) => i.t === "applink").map((i) => ({ key: i.key, v: i.v }))
|
|
53
|
+
});
|
|
54
|
+
} else blocks.push({ t: "p", inline });
|
|
55
|
+
};
|
|
56
|
+
const flushList = () => {
|
|
57
|
+
if (!list) return;
|
|
58
|
+
blocks.push({ t: "list", ordered: list.ordered, items: list.items.map(parseInline) });
|
|
59
|
+
list = null;
|
|
60
|
+
};
|
|
61
|
+
const flush = () => {
|
|
62
|
+
flushPara();
|
|
63
|
+
flushList();
|
|
64
|
+
};
|
|
65
|
+
for (let i = 0; i < lines.length; i++) {
|
|
66
|
+
const line = lines[i];
|
|
67
|
+
const fence = /^```(\S*)\s*$/.exec(line);
|
|
68
|
+
if (fence) {
|
|
69
|
+
flush();
|
|
70
|
+
const buf = [];
|
|
71
|
+
i++;
|
|
72
|
+
while (i < lines.length && !/^```\s*$/.test(lines[i])) buf.push(lines[i++]);
|
|
73
|
+
blocks.push({ t: "code", lang: fence[1] ?? "", v: buf.join("\n") });
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const h = /^(#{2,3})\s+(.+?)\s*#*\s*$/.exec(line);
|
|
77
|
+
if (h) {
|
|
78
|
+
flush();
|
|
79
|
+
const text = h[2];
|
|
80
|
+
const inline = parseInline(text);
|
|
81
|
+
blocks.push({ t: "heading", level: h[1].length, id: uniqueId(inlineText(inline)), text: inlineText(inline), inline });
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (/^#\s+/.test(line)) {
|
|
85
|
+
flush();
|
|
86
|
+
const inline = parseInline(line.replace(/^#\s+/, ""));
|
|
87
|
+
blocks.push({ t: "heading", level: 2, id: uniqueId(inlineText(inline)), text: inlineText(inline), inline });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (/^\s*(---|\*\*\*)\s*$/.test(line)) {
|
|
91
|
+
flush();
|
|
92
|
+
blocks.push({ t: "hr" });
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (/^>\s?/.test(line)) {
|
|
96
|
+
flush();
|
|
97
|
+
const buf = [];
|
|
98
|
+
while (i < lines.length && /^>\s?/.test(lines[i])) buf.push(lines[i++].replace(/^>\s?/, ""));
|
|
99
|
+
i--;
|
|
100
|
+
let callout = null;
|
|
101
|
+
const tag = /^\[!(note|tip|warning)\]\s*$/i.exec(buf[0] ?? "");
|
|
102
|
+
if (tag) {
|
|
103
|
+
callout = tag[1].toLowerCase();
|
|
104
|
+
buf.shift();
|
|
105
|
+
}
|
|
106
|
+
const inner = parseMarkdown(buf.join("\n")).map((b) => b.t === "heading" ? { t: "p", inline: b.inline } : b);
|
|
107
|
+
blocks.push({ t: "quote", callout, blocks: inner });
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const ul = /^\s*[-*]\s+(.*)$/.exec(line);
|
|
111
|
+
const ol = /^\s*\d+[.)]\s+(.*)$/.exec(line);
|
|
112
|
+
if (ul || ol) {
|
|
113
|
+
flushPara();
|
|
114
|
+
const ordered = !!ol;
|
|
115
|
+
if (list && list.ordered !== ordered) flushList();
|
|
116
|
+
if (!list) list = { ordered, items: [] };
|
|
117
|
+
list.items.push((ul ?? ol)[1]);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (!line.trim()) {
|
|
121
|
+
flush();
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (list && /^\s{2,}\S/.test(line)) {
|
|
125
|
+
list.items[list.items.length - 1] += " " + line.trim();
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
flushList();
|
|
129
|
+
para.push(line.trim());
|
|
130
|
+
}
|
|
131
|
+
flush();
|
|
132
|
+
return blocks;
|
|
133
|
+
}
|
|
134
|
+
function extractToc(blocks) {
|
|
135
|
+
return blocks.flatMap((b) => b.t === "heading" ? [{ id: b.id, text: b.text, level: b.level }] : []);
|
|
136
|
+
}
|
|
137
|
+
function appLinkKeys(body) {
|
|
138
|
+
const keys = /* @__PURE__ */ new Set();
|
|
139
|
+
for (const m of body.matchAll(APP_LINK_RE)) keys.add(m[1]);
|
|
140
|
+
return [...keys];
|
|
141
|
+
}
|
|
142
|
+
function plainText(body) {
|
|
143
|
+
return body.replace(/```[\s\S]*?```/g, " ").replace(APP_LINK_RE, "$2").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/^>\s?\[![a-z]+\]\s*$/gim, "").replace(/[*`>#]/g, "").replace(/^\s*(\d+[.)]|[-*])\s+/gm, "").replace(/[ \t]+/g, " ").trim();
|
|
144
|
+
}
|
|
145
|
+
function splitSections(body) {
|
|
146
|
+
const lines = body.replace(/\r/g, "").split("\n");
|
|
147
|
+
const out = [];
|
|
148
|
+
const used = /* @__PURE__ */ new Map();
|
|
149
|
+
let cur = { anchor: "", heading: "", markdown: "" };
|
|
150
|
+
let inFence = false;
|
|
151
|
+
const push = () => {
|
|
152
|
+
cur.markdown = cur.markdown.trim();
|
|
153
|
+
if (cur.markdown || cur.heading) out.push(cur);
|
|
154
|
+
};
|
|
155
|
+
for (const line of lines) {
|
|
156
|
+
if (/^```/.test(line)) inFence = !inFence;
|
|
157
|
+
const h = !inFence ? /^#{1,2}\s+(.+?)\s*#*\s*$/.exec(line) : null;
|
|
158
|
+
if (h) {
|
|
159
|
+
push();
|
|
160
|
+
const text = inlineText(parseInline(h[1]));
|
|
161
|
+
const base = anchorOf(text);
|
|
162
|
+
const n = used.get(base) ?? 0;
|
|
163
|
+
used.set(base, n + 1);
|
|
164
|
+
cur = { anchor: n ? `${base}-${n}` : base, heading: text, markdown: "" };
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const h3 = !inFence ? /^###\s+(.+?)\s*#*\s*$/.exec(line) : null;
|
|
168
|
+
if (h3) {
|
|
169
|
+
const base = anchorOf(inlineText(parseInline(h3[1])));
|
|
170
|
+
used.set(base, (used.get(base) ?? 0) + 1);
|
|
171
|
+
}
|
|
172
|
+
cur.markdown += line + "\n";
|
|
173
|
+
}
|
|
174
|
+
push();
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export {
|
|
179
|
+
APP_LINK_RE,
|
|
180
|
+
anchorOf,
|
|
181
|
+
parseInline,
|
|
182
|
+
inlineText,
|
|
183
|
+
parseMarkdown,
|
|
184
|
+
extractToc,
|
|
185
|
+
appLinkKeys,
|
|
186
|
+
plainText,
|
|
187
|
+
splitSections
|
|
188
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// src/frontmatter.ts
|
|
2
|
+
var DOC_KEYS = /* @__PURE__ */ new Set(["slug", "title", "summary", "category", "keywords"]);
|
|
3
|
+
function scalar(raw) {
|
|
4
|
+
const v = raw.trim();
|
|
5
|
+
if (v === "") return "";
|
|
6
|
+
if (v === "true") return true;
|
|
7
|
+
if (v === "false") return false;
|
|
8
|
+
if (v === "null" || v === "~") return null;
|
|
9
|
+
if (/^-?\d+(\.\d+)?$/.test(v)) return Number(v);
|
|
10
|
+
if (v.startsWith('"') && v.endsWith('"') || v.startsWith("'") && v.endsWith("'")) return v.slice(1, -1);
|
|
11
|
+
if (v.startsWith("[") && v.endsWith("]")) {
|
|
12
|
+
const inner = v.slice(1, -1).trim();
|
|
13
|
+
return inner ? splitOutsideQuotes(inner, ",").map((x) => scalar(x)) : [];
|
|
14
|
+
}
|
|
15
|
+
return v;
|
|
16
|
+
}
|
|
17
|
+
function splitOutsideQuotes(s, sep) {
|
|
18
|
+
const out = [];
|
|
19
|
+
let cur = "";
|
|
20
|
+
let q = null;
|
|
21
|
+
for (const ch of s) {
|
|
22
|
+
if (q) {
|
|
23
|
+
if (ch === q) q = null;
|
|
24
|
+
} else if (ch === '"' || ch === "'") q = ch;
|
|
25
|
+
else if (ch === sep) {
|
|
26
|
+
out.push(cur);
|
|
27
|
+
cur = "";
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
cur += ch;
|
|
31
|
+
}
|
|
32
|
+
out.push(cur);
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
function stripComment(line) {
|
|
36
|
+
let q = null;
|
|
37
|
+
for (let i = 0; i < line.length; i++) {
|
|
38
|
+
const ch = line[i];
|
|
39
|
+
if (q) {
|
|
40
|
+
if (ch === q) q = null;
|
|
41
|
+
} else if (ch === '"' || ch === "'") q = ch;
|
|
42
|
+
else if (ch === "#" && (i === 0 || /\s/.test(line[i - 1]))) return line.slice(0, i).trimEnd();
|
|
43
|
+
}
|
|
44
|
+
return line;
|
|
45
|
+
}
|
|
46
|
+
function parseFrontmatter(text) {
|
|
47
|
+
const src = text.replace(/^\uFEFF/, "").replace(/\r\n/g, "\n");
|
|
48
|
+
const m = /^---\n([\s\S]*?)\n---\n?/.exec(src);
|
|
49
|
+
if (!m) return { data: {}, body: src.trim() };
|
|
50
|
+
const data = {};
|
|
51
|
+
for (const line of m[1].split("\n")) {
|
|
52
|
+
const t = stripComment(line);
|
|
53
|
+
if (!t.trim()) continue;
|
|
54
|
+
const i = t.indexOf(":");
|
|
55
|
+
if (i < 1) throw new Error(`frontmatter: cannot read line "${line}"`);
|
|
56
|
+
data[t.slice(0, i).trim()] = scalar(t.slice(i + 1));
|
|
57
|
+
}
|
|
58
|
+
return { data, body: src.slice(m[0].length).trim() };
|
|
59
|
+
}
|
|
60
|
+
function docFromMarkdown(text, fallbackSlug) {
|
|
61
|
+
const { data, body } = parseFrontmatter(text);
|
|
62
|
+
const fields = {};
|
|
63
|
+
for (const [k, v] of Object.entries(data)) if (!DOC_KEYS.has(k)) fields[k] = v;
|
|
64
|
+
const doc = { body };
|
|
65
|
+
const slug = data.slug ?? fallbackSlug;
|
|
66
|
+
if (slug) doc.slug = String(slug);
|
|
67
|
+
if (data.title !== void 0) doc.title = String(data.title);
|
|
68
|
+
if (data.summary !== void 0) doc.summary = String(data.summary);
|
|
69
|
+
if (data.category !== void 0) doc.category = data.category === null ? null : String(data.category);
|
|
70
|
+
if (Array.isArray(data.keywords)) doc.keywords = data.keywords.map(String);
|
|
71
|
+
if (Object.keys(fields).length) doc.fields = fields;
|
|
72
|
+
return doc;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export {
|
|
76
|
+
parseFrontmatter,
|
|
77
|
+
docFromMarkdown
|
|
78
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// src/messages/en.ts
|
|
2
|
+
var en = {
|
|
3
|
+
not_found: "Document not found",
|
|
4
|
+
version_not_found: "Version v{version} not found",
|
|
5
|
+
key_not_found: "API key not found",
|
|
6
|
+
route_not_found: "No such route",
|
|
7
|
+
unknown_collection: "Unknown collection: {collection}",
|
|
8
|
+
unauthorized: "Sign in or use an API key",
|
|
9
|
+
invalid_key: "The API key is wrong or revoked",
|
|
10
|
+
key_expired: "The API key expired on {expiresAt}",
|
|
11
|
+
preview_invalid: "This preview link is not valid",
|
|
12
|
+
preview_expired: "This preview link expired",
|
|
13
|
+
forbidden: "Not allowed",
|
|
14
|
+
forbidden_read: "No read access to {collection}",
|
|
15
|
+
forbidden_write: "No edit access to {collection}",
|
|
16
|
+
forbidden_publish: "No publish access to {collection}",
|
|
17
|
+
forbidden_keys: "No access to API keys",
|
|
18
|
+
forbidden_audit: "No access to the audit log",
|
|
19
|
+
key_too_wide: "A key can only create keys narrower than itself",
|
|
20
|
+
cross_site: "Cross-site request refused",
|
|
21
|
+
invalid: "Invalid input",
|
|
22
|
+
invalid_slug: "The slug may use lowercase letters, digits and hyphens (max {max})",
|
|
23
|
+
invalid_title: "The title must be 1 to {max} characters",
|
|
24
|
+
summary_too_long: "The summary must be at most {max} characters",
|
|
25
|
+
body_too_long: "The body is too long (max {max} characters)",
|
|
26
|
+
invalid_keywords: "At most {max} keywords of {maxLength} characters each",
|
|
27
|
+
forbidden_char: "The character \u201C{char}\u201D is not allowed",
|
|
28
|
+
invalid_category: "The category must be one of: {allowed}",
|
|
29
|
+
invalid_field: "{path}: {issue}",
|
|
30
|
+
unknown_link: "Unknown screen link: {keys}",
|
|
31
|
+
slug_locked: "A published document's slug cannot change",
|
|
32
|
+
not_published: "Only a published document can be hidden",
|
|
33
|
+
invalid_key_name: "The key name must be 1 to 60 characters",
|
|
34
|
+
scopes_required: "Choose at least one scope",
|
|
35
|
+
invalid_expiry: "The expiry date must be in the future",
|
|
36
|
+
invalid_anchor: "No section \u201C{anchor}\u201D. Sections: {valid}",
|
|
37
|
+
invalid_version: "Invalid version number",
|
|
38
|
+
invalid_request: "Invalid request: {issues}",
|
|
39
|
+
invalid_json: "The request body is not valid JSON",
|
|
40
|
+
body_too_large: "The request is too large",
|
|
41
|
+
preview_disabled: "Preview links are off (set previewSecret)",
|
|
42
|
+
no_public_url: "This collection has no public URL (set url)",
|
|
43
|
+
schema_outdated: "The database schema is v{found}, this version needs v{expected}. Run: {command}",
|
|
44
|
+
conflict_version: "Someone else saved v{current} since you read v{expected}. Reload or merge, then save",
|
|
45
|
+
slug_taken: "The slug {slug} is already used",
|
|
46
|
+
precondition_required: "Send If-Match (or expectedVersion) to change this document",
|
|
47
|
+
internal: "Something went wrong"
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export {
|
|
51
|
+
en
|
|
52
|
+
};
|