aicms 0.0.0-stage → 0.5.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/ARCHITECTURE.md +467 -0
- package/LICENSE +21 -0
- package/README.md +200 -2
- package/README.uk.md +201 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +722 -0
- package/dist/config-edit.d.ts +18 -0
- package/dist/config-edit.js +201 -0
- package/dist/config.d.ts +55 -0
- package/dist/config.js +32 -0
- package/dist/edge.d.ts +22 -0
- package/dist/edge.js +56 -0
- package/dist/gates/a2a.d.ts +45 -0
- package/dist/gates/a2a.js +126 -0
- package/dist/gates/discovery.d.ts +36 -0
- package/dist/gates/discovery.js +54 -0
- package/dist/gates/events.d.ts +14 -0
- package/dist/gates/events.js +86 -0
- package/dist/gates/graphql.d.ts +41 -0
- package/dist/gates/graphql.js +226 -0
- package/dist/gates/mcp.d.ts +8 -0
- package/dist/gates/mcp.js +60 -0
- package/dist/gates/openapi.d.ts +56 -0
- package/dist/gates/openapi.js +61 -0
- package/dist/gates/rest.d.ts +21 -0
- package/dist/gates/rest.js +263 -0
- package/dist/gates/studio.d.ts +21 -0
- package/dist/gates/studio.js +107 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +19 -0
- package/dist/kernel/action.d.ts +49 -0
- package/dist/kernel/action.js +22 -0
- package/dist/kernel/briefs.d.ts +161 -0
- package/dist/kernel/briefs.js +197 -0
- package/dist/kernel/changesets.d.ts +367 -0
- package/dist/kernel/changesets.js +765 -0
- package/dist/kernel/cms.d.ts +127 -0
- package/dist/kernel/cms.js +419 -0
- package/dist/kernel/core-actions.d.ts +5 -0
- package/dist/kernel/core-actions.js +487 -0
- package/dist/kernel/errors.d.ts +14 -0
- package/dist/kernel/errors.js +25 -0
- package/dist/kernel/install.d.ts +32 -0
- package/dist/kernel/install.js +34 -0
- package/dist/kernel/model-actions.d.ts +5 -0
- package/dist/kernel/model-actions.js +126 -0
- package/dist/kernel/modules.d.ts +221 -0
- package/dist/kernel/modules.js +207 -0
- package/dist/kernel/policy.d.ts +42 -0
- package/dist/kernel/policy.js +47 -0
- package/dist/load.d.ts +10 -0
- package/dist/load.js +63 -0
- package/dist/modules/catalog.d.ts +15 -0
- package/dist/modules/catalog.js +21 -0
- package/dist/modules/commerce.d.ts +67 -0
- package/dist/modules/commerce.js +260 -0
- package/dist/modules/git-sync.d.ts +20 -0
- package/dist/modules/git-sync.js +187 -0
- package/dist/modules/i18n.d.ts +6 -0
- package/dist/modules/i18n.js +168 -0
- package/dist/modules/index.d.ts +9 -0
- package/dist/modules/index.js +10 -0
- package/dist/modules/lint.d.ts +22 -0
- package/dist/modules/lint.js +126 -0
- package/dist/modules/media.d.ts +52 -0
- package/dist/modules/media.js +319 -0
- package/dist/modules/payments.d.ts +19 -0
- package/dist/modules/payments.js +110 -0
- package/dist/modules/search.d.ts +27 -0
- package/dist/modules/search.js +81 -0
- package/dist/modules/seo.d.ts +34 -0
- package/dist/modules/seo.js +189 -0
- package/dist/modules/text.d.ts +10 -0
- package/dist/modules/text.js +42 -0
- package/dist/node.d.ts +7 -0
- package/dist/node.js +11 -0
- package/dist/registry.d.ts +57 -0
- package/dist/registry.js +110 -0
- package/dist/schema/fields.d.ts +100 -0
- package/dist/schema/fields.js +134 -0
- package/dist/schema/markdown.d.ts +7 -0
- package/dist/schema/markdown.js +168 -0
- package/dist/schema/model.d.ts +44 -0
- package/dist/schema/model.js +104 -0
- package/dist/schema/slug.d.ts +2 -0
- package/dist/schema/slug.js +40 -0
- package/dist/store/d1.d.ts +30 -0
- package/dist/store/d1.js +27 -0
- package/dist/store/postgres.d.ts +164 -0
- package/dist/store/postgres.js +552 -0
- package/dist/store/sql.d.ts +135 -0
- package/dist/store/sql.js +530 -0
- package/dist/store/sqlite.d.ts +14 -0
- package/dist/store/sqlite.js +60 -0
- package/dist/store/types.d.ts +226 -0
- package/dist/store/types.js +21 -0
- package/dist/studio/assets/geologica-cyrillic-ext-wght-normal-C8B9Chlh.woff2 +0 -0
- package/dist/studio/assets/geologica-cyrillic-wght-normal-DbeVG3Ps.woff2 +0 -0
- package/dist/studio/assets/geologica-greek-wght-normal-DPM5d9ub.woff2 +0 -0
- package/dist/studio/assets/geologica-latin-ext-wght-normal-DZyYlgcm.woff2 +0 -0
- package/dist/studio/assets/geologica-latin-wght-normal-BBL8hQxZ.woff2 +0 -0
- package/dist/studio/assets/geologica-vietnamese-wght-normal-DcI17N1q.woff2 +0 -0
- package/dist/studio/assets/index-3dJ0YHxb.js +22 -0
- package/dist/studio/assets/index-DuyGLFw4.css +1 -0
- package/dist/studio/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/studio/assets/unbounded-cyrillic-wght-normal-C627f1f8.woff2 +0 -0
- package/dist/studio/assets/unbounded-latin-ext-wght-normal-D6H-S6MW.woff2 +0 -0
- package/dist/studio/assets/unbounded-latin-wght-normal-D5afBTBx.woff2 +0 -0
- package/dist/studio/assets/unbounded-vietnamese-wght-normal-rat7GD3o.woff2 +0 -0
- package/dist/studio/index.html +21 -0
- package/dist/typegen.d.ts +7 -0
- package/dist/typegen.js +55 -0
- package/package.json +88 -4
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { action } from "../kernel/action.js";
|
|
3
|
+
import { AicmsError } from "../kernel/errors.js";
|
|
4
|
+
import { defineModule } from "../kernel/modules.js";
|
|
5
|
+
import { displayTitle, uniqueFields } from "../schema/model.js";
|
|
6
|
+
/** Translations: links entries across locales, shows what's missing, drafts translations for agents. */
|
|
7
|
+
export function i18n(opts = {}) {
|
|
8
|
+
const covered = (model) => !opts.models || opts.models.includes(model);
|
|
9
|
+
async function groupOf(ctx, id) {
|
|
10
|
+
const key = await ctx.kv.get(`e:${id}`);
|
|
11
|
+
if (!key)
|
|
12
|
+
return null;
|
|
13
|
+
return { key, members: (await ctx.kv.get(`g:${key}`)) ?? {} };
|
|
14
|
+
}
|
|
15
|
+
async function source(ctx, id) {
|
|
16
|
+
const e = await ctx.entry(id);
|
|
17
|
+
if (!e)
|
|
18
|
+
throw new AicmsError("not_found", `No entry ${id}`);
|
|
19
|
+
if (!covered(e.model))
|
|
20
|
+
throw new AicmsError("invalid", `Model "${e.model}" isn't translated (i18n covers: ${opts.models?.join(", ")})`);
|
|
21
|
+
if (!ctx.readable(e.model))
|
|
22
|
+
throw new AicmsError("forbidden", `You can't read ${e.model} entries`);
|
|
23
|
+
return e;
|
|
24
|
+
}
|
|
25
|
+
async function link(ctx, a, b) {
|
|
26
|
+
const g = (await groupOf(ctx, a.id)) ?? { key: a.id, members: { [a.locale]: a.id } };
|
|
27
|
+
if (g.members[b.locale] && g.members[b.locale] !== b.id)
|
|
28
|
+
throw new AicmsError("conflict", `${a.id} already has a ${b.locale} translation: ${g.members[b.locale]}`);
|
|
29
|
+
g.members[b.locale] = b.id;
|
|
30
|
+
await ctx.kv.set(`g:${g.key}`, g.members);
|
|
31
|
+
await ctx.kv.set(`e:${a.id}`, g.key);
|
|
32
|
+
await ctx.kv.set(`e:${b.id}`, g.key);
|
|
33
|
+
return g;
|
|
34
|
+
}
|
|
35
|
+
async function translations(ctx, id) {
|
|
36
|
+
const e = await source(ctx, id);
|
|
37
|
+
const g = (await groupOf(ctx, id))?.members ?? { [e.locale]: id };
|
|
38
|
+
const model = ctx.models().find((m) => m.name === e.model);
|
|
39
|
+
const out = {};
|
|
40
|
+
for (const locale of ctx.site.locales) {
|
|
41
|
+
const member = g[locale];
|
|
42
|
+
if (!member) {
|
|
43
|
+
out[locale] = null;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const t = await ctx.entry(member);
|
|
47
|
+
if (t && t.version > 0) {
|
|
48
|
+
out[locale] = { id: member, title: displayTitle(model, t.data), published: true };
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
// Not published: someone's draft (maybe waiting for review), or a draft that was dropped.
|
|
52
|
+
const p = await ctx.pending(member);
|
|
53
|
+
out[locale] = p ? { id: member, title: displayTitle(model, p.data), published: false, waiting: { changeset: p.changeset, actor: p.actor } } : null;
|
|
54
|
+
}
|
|
55
|
+
return { id, model: e.model, locale: e.locale, translations: out, missing: Object.entries(out).filter(([, v]) => !v).map(([l]) => l) };
|
|
56
|
+
}
|
|
57
|
+
return defineModule({
|
|
58
|
+
name: "i18n",
|
|
59
|
+
description: "Translations: links entries across locales, lists what's missing, drafts translations.",
|
|
60
|
+
provides: ["i18n"],
|
|
61
|
+
uses: opts.models ?? ["*"],
|
|
62
|
+
actions: {
|
|
63
|
+
translations: action({
|
|
64
|
+
description: "The translations of an entry: for each site locale, the linked entry or null if it's missing.",
|
|
65
|
+
readOnly: true,
|
|
66
|
+
input: z.strictObject({ id: z.string().min(1) }),
|
|
67
|
+
run: ({ input, ctx }) => translations(ctx, input.id),
|
|
68
|
+
}),
|
|
69
|
+
missing: action({
|
|
70
|
+
description: "Entries of a model in the default locale that have no translation yet, per locale.",
|
|
71
|
+
readOnly: true,
|
|
72
|
+
input: z.strictObject({ model: z.string().min(1), locale: z.string().optional(), limit: z.number().int().min(1).max(200).optional() }),
|
|
73
|
+
async run({ input, ctx }) {
|
|
74
|
+
if (!covered(input.model) || !ctx.models().some((m) => m.name === input.model))
|
|
75
|
+
throw new AicmsError("not_found", `i18n doesn't cover "${input.model}"`);
|
|
76
|
+
if (!ctx.readable(input.model))
|
|
77
|
+
throw new AicmsError("forbidden", `You can't read ${input.model} entries`);
|
|
78
|
+
const model = ctx.models().find((m) => m.name === input.model);
|
|
79
|
+
const targets = (input.locale ? [input.locale] : ctx.site.locales).filter((l) => l !== ctx.site.defaultLocale);
|
|
80
|
+
const missing = Object.fromEntries(targets.map((l) => [l, []]));
|
|
81
|
+
for (let offset = 0;; offset += 200) {
|
|
82
|
+
const page = await ctx.list(input.model, { locale: ctx.site.defaultLocale, limit: 200, offset });
|
|
83
|
+
for (const e of page.items) {
|
|
84
|
+
const g = (await groupOf(ctx, e.id))?.members ?? {};
|
|
85
|
+
for (const l of targets)
|
|
86
|
+
if (!g[l])
|
|
87
|
+
missing[l].push({ id: e.id, title: displayTitle(model, e) });
|
|
88
|
+
}
|
|
89
|
+
if (offset + 200 >= page.total)
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
const limit = input.limit ?? 50;
|
|
93
|
+
return { model: input.model, missing: Object.fromEntries(Object.entries(missing).map(([l, items]) => [l, { count: items.length, items: items.slice(0, limit) }])) };
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
translate: action({
|
|
97
|
+
description: "Draft a translation of an entry into another locale. Pass the translated fields in `fields` (block fields accept Markdown); everything else is copied from the source. It is linked to the source and goes through review like any draft.",
|
|
98
|
+
scope: "i18n:translate",
|
|
99
|
+
input: z.strictObject({
|
|
100
|
+
id: z.string().min(1).describe("The entry to translate"),
|
|
101
|
+
locale: z.string().min(1).describe("Target locale"),
|
|
102
|
+
fields: z.record(z.string(), z.unknown()).optional().describe("Translated field values"),
|
|
103
|
+
}),
|
|
104
|
+
async run({ input, ctx }) {
|
|
105
|
+
const e = await source(ctx, input.id);
|
|
106
|
+
if (!ctx.site.locales.includes(input.locale))
|
|
107
|
+
throw new AicmsError("invalid", `Unknown locale "${input.locale}". Site locales: ${ctx.site.locales.join(", ")}`);
|
|
108
|
+
if (input.locale === e.locale)
|
|
109
|
+
throw new AicmsError("invalid", `${input.id} is already in ${e.locale}`);
|
|
110
|
+
const existing = (await groupOf(ctx, input.id))?.members[input.locale];
|
|
111
|
+
if (existing && (await ctx.entry(existing))?.version)
|
|
112
|
+
throw new AicmsError("conflict", `${input.id} already has a ${input.locale} translation: ${existing}. Draft changes to it instead.`);
|
|
113
|
+
const waiting = existing ? await ctx.pending(existing) : null;
|
|
114
|
+
if (waiting)
|
|
115
|
+
throw new AicmsError("conflict", `A ${input.locale} translation of ${input.id} is already drafted by ${waiting.actor} (${waiting.changeset}, ${waiting.status === "submitted" ? "waiting for review" : "not submitted yet"})`);
|
|
116
|
+
const model = ctx.models().find((m) => m.name === e.model);
|
|
117
|
+
const unknown = Object.keys(input.fields ?? {}).filter((k) => !(k in model.fields));
|
|
118
|
+
if (unknown.length)
|
|
119
|
+
throw new AicmsError("invalid", `Model "${model.name}" has no field ${unknown.join(", ")}`);
|
|
120
|
+
// Unique fields (slugs) are per locale, so the source's values are fine to keep unless translated.
|
|
121
|
+
const data = { ...e.data, ...input.fields, locale: input.locale };
|
|
122
|
+
const draft = await ctx.draft(model.name, data);
|
|
123
|
+
await link(ctx, e, { id: draft.id, locale: input.locale });
|
|
124
|
+
return {
|
|
125
|
+
...draft,
|
|
126
|
+
translationOf: input.id,
|
|
127
|
+
note: uniqueFields(model).some((f) => !(input.fields ?? {})[f]) ? `Kept ${uniqueFields(model).join(", ")} from the source; translate them too if they should differ.` : undefined,
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
}),
|
|
131
|
+
link: action({
|
|
132
|
+
description: "Mark two existing entries of the same model as translations of each other.",
|
|
133
|
+
scope: "i18n:translate",
|
|
134
|
+
input: z.strictObject({ id: z.string().min(1), to: z.string().min(1) }),
|
|
135
|
+
async run({ input, ctx }) {
|
|
136
|
+
const a = await source(ctx, input.id);
|
|
137
|
+
const b = await source(ctx, input.to);
|
|
138
|
+
if (a.model !== b.model)
|
|
139
|
+
throw new AicmsError("invalid", `${input.id} is a ${a.model}, ${input.to} is a ${b.model}`);
|
|
140
|
+
if (a.locale === b.locale)
|
|
141
|
+
throw new AicmsError("invalid", `Both entries are in ${a.locale}`);
|
|
142
|
+
await link(ctx, a, b);
|
|
143
|
+
return translations(ctx, input.id);
|
|
144
|
+
},
|
|
145
|
+
}),
|
|
146
|
+
},
|
|
147
|
+
on: {
|
|
148
|
+
// A deleted entry leaves its group.
|
|
149
|
+
"changeset.merged": async ({ payload, ctx }) => {
|
|
150
|
+
for (const e of payload.entries ?? []) {
|
|
151
|
+
if (e.op !== "delete")
|
|
152
|
+
continue;
|
|
153
|
+
const g = await groupOf(ctx, e.id);
|
|
154
|
+
if (!g)
|
|
155
|
+
continue;
|
|
156
|
+
for (const [locale, id] of Object.entries(g.members))
|
|
157
|
+
if (id === e.id)
|
|
158
|
+
delete g.members[locale];
|
|
159
|
+
await ctx.kv.delete(`e:${e.id}`);
|
|
160
|
+
if (Object.keys(g.members).length)
|
|
161
|
+
await ctx.kv.set(`g:${g.key}`, g.members);
|
|
162
|
+
else
|
|
163
|
+
await ctx.kv.delete(`g:${g.key}`);
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { seo, auditEntry, type SeoOptions, type SeoIssue } from "./seo.ts";
|
|
2
|
+
export { search, type SearchOptions, type SearchService, type SearchHit } from "./search.ts";
|
|
3
|
+
export { media, imageSize, sniff, type MediaOptions } from "./media.ts";
|
|
4
|
+
export { i18n, type I18nOptions } from "./i18n.ts";
|
|
5
|
+
export { lint, lintEntry, DEFAULT_AVOID, type LintOptions } from "./lint.ts";
|
|
6
|
+
export { gitSync, toMarkdown, fromMarkdown, type GitSyncOptions } from "./git-sync.ts";
|
|
7
|
+
export { commerce, product, order, type CommerceOptions, type PaymentsService, type PaymentUpdated } from "./commerce.ts";
|
|
8
|
+
export { manualPayments, stripe, verifyStripeSignature, type ManualPaymentsOptions, type StripeOptions } from "./payments.ts";
|
|
9
|
+
export { catalog, findCatalog, type CatalogEntry } from "./catalog.ts";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Official AICMS modules: import { seo, search, media, i18n, gitSync, lint } from "aicms/modules"
|
|
2
|
+
export { seo, auditEntry } from "./seo.js";
|
|
3
|
+
export { search } from "./search.js";
|
|
4
|
+
export { media, imageSize, sniff } from "./media.js";
|
|
5
|
+
export { i18n } from "./i18n.js";
|
|
6
|
+
export { lint, lintEntry, DEFAULT_AVOID } from "./lint.js";
|
|
7
|
+
export { gitSync, toMarkdown, fromMarkdown } from "./git-sync.js";
|
|
8
|
+
export { commerce, product, order } from "./commerce.js";
|
|
9
|
+
export { manualPayments, stripe, verifyStripeSignature } from "./payments.js";
|
|
10
|
+
export { catalog, findCatalog } from "./catalog.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type CheckFinding, type CheckInput } from "../kernel/modules.ts";
|
|
2
|
+
type Level = "error" | "warning";
|
|
3
|
+
export interface LintOptions {
|
|
4
|
+
/** Phrases to avoid: words, phrases or regular expressions. Default: DEFAULT_AVOID. Extend with [...DEFAULT_AVOID, "synergy"]. */
|
|
5
|
+
avoid?: (string | RegExp)[];
|
|
6
|
+
/** Level for avoided phrases. Default "warning". */
|
|
7
|
+
avoidLevel?: Level;
|
|
8
|
+
/** Placeholder text left in a draft ("[insert …]", "TODO", "lorem ipsum"). Default "error"; false turns it off. */
|
|
9
|
+
placeholders?: Level | false;
|
|
10
|
+
/** Sentences longer than this many words get a warning. Default 40; 0 turns it off. */
|
|
11
|
+
maxSentenceWords?: number;
|
|
12
|
+
/** Images and media fields without alt text. Default "warning". */
|
|
13
|
+
altText?: Level | false;
|
|
14
|
+
/** Models to lint. Default: all. */
|
|
15
|
+
models?: string[];
|
|
16
|
+
}
|
|
17
|
+
/** Filler that reads as machine-written. Matched as whole words, case-insensitively. */
|
|
18
|
+
export declare const DEFAULT_AVOID: string[];
|
|
19
|
+
export declare function lintEntry(input: Pick<CheckInput, "model" | "data" | "op" | "changed">, opts?: LintOptions): CheckFinding[];
|
|
20
|
+
/** Content lint for every change: placeholders block, filler and sloppiness warn. */
|
|
21
|
+
export declare function lint(opts?: LintOptions): import("../kernel/modules.ts").ModuleDef;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Content lint: checks that run on every submitted change, like CI for content.
|
|
2
|
+
// Placeholders an agent left behind ("[insert statistic]", "TODO") stop the
|
|
3
|
+
// changeset; filler phrases, long sentences, empty headings, images without alt
|
|
4
|
+
// text and dead links go to the reviewer as warnings. Only the fields a change
|
|
5
|
+
// touches are checked, so fixing a typo doesn't drag up the whole entry.
|
|
6
|
+
import { defineModule } from "../kernel/modules.js";
|
|
7
|
+
/** Filler that reads as machine-written. Matched as whole words, case-insensitively. */
|
|
8
|
+
export const DEFAULT_AVOID = [
|
|
9
|
+
"delve",
|
|
10
|
+
"delves",
|
|
11
|
+
"delving",
|
|
12
|
+
"in today's fast-paced world",
|
|
13
|
+
"in today's digital age",
|
|
14
|
+
"it's important to note",
|
|
15
|
+
"it is important to note",
|
|
16
|
+
"it's worth noting",
|
|
17
|
+
"game-changer",
|
|
18
|
+
"game changer",
|
|
19
|
+
"unlock the power",
|
|
20
|
+
"unleash the power",
|
|
21
|
+
"a testament to",
|
|
22
|
+
"rich tapestry",
|
|
23
|
+
"navigate the complexities",
|
|
24
|
+
"in the ever-evolving",
|
|
25
|
+
"ever-changing landscape",
|
|
26
|
+
"look no further",
|
|
27
|
+
];
|
|
28
|
+
const PLACEHOLDERS = [
|
|
29
|
+
/\[(?:insert|add|your|link|source|citation|todo|tbd|placeholder)[^\]]*\]/i,
|
|
30
|
+
/\{\{[^}]*\}\}/,
|
|
31
|
+
/\b(?:TODO|TBD|FIXME|XXX)\b/,
|
|
32
|
+
/\blorem ipsum\b/i,
|
|
33
|
+
/\b(?:example\.com|example\.org)\b/i,
|
|
34
|
+
];
|
|
35
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
36
|
+
const phrase = (p) => (p instanceof RegExp ? p : new RegExp(`(?<![\\p{L}\\p{N}])${escape(p).replace(/'/g, "['’]")}(?![\\p{L}\\p{N}])`, "iu"));
|
|
37
|
+
/** Readable text of one field, as [where, text] pairs: "paragraph 3", "heading 1", "seo.title"… */
|
|
38
|
+
function texts(key, field, value) {
|
|
39
|
+
if (value == null)
|
|
40
|
+
return [];
|
|
41
|
+
if (field.kind === "blocks" && Array.isArray(value)) {
|
|
42
|
+
const out = [];
|
|
43
|
+
const count = {};
|
|
44
|
+
for (const b of value) {
|
|
45
|
+
count[b.type] = (count[b.type] ?? 0) + 1;
|
|
46
|
+
const where = `${key}: ${b.type} ${count[b.type]}`;
|
|
47
|
+
for (const k of ["text", "title", "caption", "label"])
|
|
48
|
+
if (typeof b[k] === "string")
|
|
49
|
+
out.push([where, b[k]]);
|
|
50
|
+
if (Array.isArray(b.items))
|
|
51
|
+
for (const item of b.items)
|
|
52
|
+
if (typeof item === "string")
|
|
53
|
+
out.push([where, item]);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
if (field.kind === "object" && typeof value === "object") {
|
|
58
|
+
const sub = field.options.fields;
|
|
59
|
+
return Object.entries(sub).flatMap(([k, f]) => texts(`${key}.${k}`, f, value[k]));
|
|
60
|
+
}
|
|
61
|
+
if (field.kind === "list" && Array.isArray(value))
|
|
62
|
+
return value.filter((v) => typeof v === "string").map((v) => [key, v]);
|
|
63
|
+
if (field.kind === "text" && typeof value === "string")
|
|
64
|
+
return [[key, value]];
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const sentences = (text) => text.replace(/\]\([^)]*\)/g, "]").split(/(?<=[.!?…])\s+|\n+/).map((s) => s.trim()).filter(Boolean);
|
|
68
|
+
const words = (s) => s.match(/[\p{L}\p{N}][\p{L}\p{N}'’-]*/gu)?.length ?? 0;
|
|
69
|
+
const short = (s, n = 60) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
|
|
70
|
+
export function lintEntry(input, opts = {}) {
|
|
71
|
+
const out = [];
|
|
72
|
+
const avoid = (opts.avoid ?? DEFAULT_AVOID).map(phrase);
|
|
73
|
+
const maxWords = opts.maxSentenceWords ?? 40;
|
|
74
|
+
const alt = opts.altText ?? "warning";
|
|
75
|
+
const placeholders = opts.placeholders ?? "error";
|
|
76
|
+
const touched = (key) => input.op === "create" || input.changed.includes(key);
|
|
77
|
+
for (const [key, field] of Object.entries(input.model.fields)) {
|
|
78
|
+
if (!touched(key))
|
|
79
|
+
continue;
|
|
80
|
+
const value = input.data[key];
|
|
81
|
+
for (const [where, text] of texts(key, field, value)) {
|
|
82
|
+
const top = where.split(":")[0];
|
|
83
|
+
if (placeholders) {
|
|
84
|
+
const hit = PLACEHOLDERS.map((re) => text.match(re)?.[0]).find(Boolean);
|
|
85
|
+
if (hit)
|
|
86
|
+
out.push({ level: placeholders, field: top, message: `${where}: placeholder "${short(hit, 40)}" left in the text` });
|
|
87
|
+
}
|
|
88
|
+
for (const re of avoid) {
|
|
89
|
+
const hit = text.match(re)?.[0];
|
|
90
|
+
if (hit)
|
|
91
|
+
out.push({ level: opts.avoidLevel ?? "warning", field: top, message: `${where}: avoid "${hit}"` });
|
|
92
|
+
}
|
|
93
|
+
if (maxWords > 0) {
|
|
94
|
+
const long = sentences(text).find((s) => words(s) > maxWords);
|
|
95
|
+
if (long)
|
|
96
|
+
out.push({ level: "warning", field: top, message: `${where}: a ${words(long)}-word sentence ("${short(long, 50)}"); split it` });
|
|
97
|
+
}
|
|
98
|
+
const dead = text.match(/\[[^\]]*\]\(\s*(#?|https?:\/\/(?:localhost|127\.0\.0\.1)[^)]*)\s*\)/);
|
|
99
|
+
if (dead)
|
|
100
|
+
out.push({ level: "warning", field: top, message: `${where}: link "${short(dead[0], 50)}" goes nowhere` });
|
|
101
|
+
}
|
|
102
|
+
if (field.kind === "blocks" && Array.isArray(value)) {
|
|
103
|
+
value.forEach((b, i) => {
|
|
104
|
+
if (b.type === "heading" && !String(b.text ?? "").trim())
|
|
105
|
+
out.push({ level: "warning", field: key, message: `${key}: block ${i + 1} is an empty heading` });
|
|
106
|
+
if (alt && b.type === "image" && !String(b.alt ?? "").trim())
|
|
107
|
+
out.push({ level: alt, field: key, message: `${key}: image ${short(String(b.src ?? b.url ?? i + 1), 50)} has no alt text` });
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (alt && field.kind === "media" && value && typeof value === "object" && value.url && !String(value.alt ?? "").trim()) {
|
|
111
|
+
out.push({ level: alt, field: key, message: `${key} has no alt text` });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
/** Content lint for every change: placeholders block, filler and sloppiness warn. */
|
|
117
|
+
export function lint(opts = {}) {
|
|
118
|
+
return defineModule({
|
|
119
|
+
name: "lint",
|
|
120
|
+
description: "Content checks on every change: placeholders left in drafts block; filler phrases, long sentences, empty headings, missing alt text and dead links go to the reviewer.",
|
|
121
|
+
uses: opts.models ?? ["*"],
|
|
122
|
+
checks: {
|
|
123
|
+
text: (input) => lintEntry(input, opts),
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Where file bytes live. Metadata always stays in the database. */
|
|
2
|
+
export interface MediaStorage {
|
|
3
|
+
put(key: string, bytes: Uint8Array, type: string): Promise<void>;
|
|
4
|
+
get(key: string): Promise<{
|
|
5
|
+
body: ReadableStream | Uint8Array;
|
|
6
|
+
size?: number;
|
|
7
|
+
} | null>;
|
|
8
|
+
delete(key: string): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
/** Files on disk, under the module's data folder (servers). */
|
|
11
|
+
export declare function localStorage(dir: () => string): MediaStorage;
|
|
12
|
+
/** The parts of a Cloudflare R2 bucket binding the module uses. */
|
|
13
|
+
interface R2BucketLike {
|
|
14
|
+
put(key: string, value: Uint8Array, options?: {
|
|
15
|
+
httpMetadata?: {
|
|
16
|
+
contentType?: string;
|
|
17
|
+
};
|
|
18
|
+
}): Promise<unknown>;
|
|
19
|
+
get(key: string): Promise<{
|
|
20
|
+
body: ReadableStream;
|
|
21
|
+
size: number;
|
|
22
|
+
} | null>;
|
|
23
|
+
delete(key: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
/** Files in a Cloudflare R2 bucket, by binding name (edge). */
|
|
26
|
+
export declare function r2Storage(bucket: () => R2BucketLike): MediaStorage;
|
|
27
|
+
export interface MediaOptions {
|
|
28
|
+
/** "local" (default: files next to the database) or { r2: "BINDING" } on Cloudflare. */
|
|
29
|
+
storage?: "local" | {
|
|
30
|
+
r2: string;
|
|
31
|
+
};
|
|
32
|
+
/** Largest accepted file, in bytes. Default 10 MB. */
|
|
33
|
+
maxBytes?: number;
|
|
34
|
+
/** Prefix for stored URLs, e.g. "https://cms.example.com". Default: relative ("/media/…"). */
|
|
35
|
+
baseUrl?: string;
|
|
36
|
+
/** Let agents import files from http(s) URLs. Default true. */
|
|
37
|
+
allowRemote?: boolean;
|
|
38
|
+
/** Allow URL imports from private and loopback addresses (local development only). Default false. */
|
|
39
|
+
allowPrivateNetworks?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** Loopback, private, link-local and other non-public ranges (IPv4 and IPv6). */
|
|
42
|
+
export declare function isPrivateAddress(ip: string): boolean;
|
|
43
|
+
/** The type from the file's own bytes; the name or a client header is never trusted. */
|
|
44
|
+
export declare function sniff(buf: Buffer): string | null;
|
|
45
|
+
/** Width and height read from the image header (PNG, JPEG, GIF, WebP), without decoding it. */
|
|
46
|
+
export declare function imageSize(buf: Buffer): {
|
|
47
|
+
width: number;
|
|
48
|
+
height: number;
|
|
49
|
+
} | null;
|
|
50
|
+
/** Media library: uploads (file, URL or base64), image sizes, alt text, served under /media. */
|
|
51
|
+
export declare function media(opts?: MediaOptions): import("../kernel/modules.ts").ModuleDef;
|
|
52
|
+
export {};
|