@kidecms/core 0.1.11 → 0.14.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/LICENSE +21 -0
- package/admin/components/AssetsGrid.tsx +366 -173
- package/admin/components/BlockEditor.tsx +260 -679
- package/admin/components/CheckboxField.tsx +2 -2
- package/admin/components/CollaborationReviewBar.tsx +384 -0
- package/admin/components/ColorField.tsx +87 -0
- package/admin/components/CommandPalette.tsx +169 -0
- package/admin/components/ContentEditor.tsx +687 -0
- package/admin/components/DocumentActions.tsx +4 -2
- package/admin/components/DocumentsDataTable.tsx +91 -10
- package/admin/components/FieldControl.astro +151 -26
- package/admin/components/FocalPointSelector.tsx +27 -1
- package/admin/components/InternalLinkPicker.tsx +4 -3
- package/admin/components/LinkField.tsx +126 -0
- package/admin/components/RelationField.tsx +105 -26
- package/admin/components/RepeaterInput.tsx +46 -0
- package/admin/components/RichTextEditor.tsx +44 -9
- package/admin/components/SharedSectionBlockField.tsx +93 -0
- package/admin/components/SidebarUserMenu.tsx +1 -1
- package/admin/components/Toast.astro +1 -1
- package/admin/components/TreeItemsEditor.tsx +20 -19
- package/admin/components/TreeSelect.tsx +2 -2
- package/admin/components/YoutubeField.tsx +48 -0
- package/admin/components/block-fields.tsx +777 -0
- package/admin/components/content-block-spec.ts +114 -0
- package/admin/components/slash-command.tsx +242 -0
- package/admin/components/tree-utils.ts +14 -2
- package/admin/components/ui/badge.tsx +2 -3
- package/admin/components/ui/button.tsx +5 -4
- package/admin/components/ui/checkbox.tsx +1 -1
- package/admin/components/ui/input.tsx +1 -1
- package/admin/components/ui/select.tsx +1 -1
- package/admin/components/ui/table.tsx +1 -1
- package/admin/components/ui/textarea.tsx +1 -1
- package/admin/layouts/AdminLayout.astro +323 -123
- package/admin/lib/access.ts +54 -0
- package/admin/lib/collaboration.ts +178 -0
- package/admin/lib/edit-data.ts +448 -0
- package/admin/views/AccessDeniedView.astro +8 -0
- package/admin/views/EditView.astro +706 -0
- package/admin/views/ListView.astro +202 -0
- package/admin/views/RecentView.astro +76 -0
- package/admin/views/SinglesView.astro +65 -0
- package/admin/views/edit/EditScripts.astro +145 -0
- package/admin/views/edit/ReverseReferencesList.astro +68 -0
- package/core/admin.ts +193 -0
- package/core/ai.ts +87 -0
- package/core/api.ts +1392 -0
- package/core/assets.ts +336 -0
- package/core/audit.ts +86 -0
- package/core/auth-config.ts +44 -0
- package/core/auth.ts +303 -0
- package/core/collaboration.ts +276 -0
- package/core/content.ts +27 -0
- package/core/create-admin.ts +28 -0
- package/core/define.ts +623 -0
- package/core/field-model.ts +172 -0
- package/core/generator.ts +519 -0
- package/core/http.ts +42 -0
- package/core/image.ts +219 -0
- package/core/index.ts +211 -0
- package/core/locks.ts +52 -0
- package/core/log.ts +37 -0
- package/core/migrate.ts +278 -0
- package/core/rate-limit.ts +110 -0
- package/core/request-scope.ts +33 -0
- package/core/richtext.ts +1 -0
- package/core/runtime.ts +63 -0
- package/core/schema.ts +32 -0
- package/core/search.ts +489 -0
- package/core/seed.ts +101 -0
- package/core/shared-sections.ts +96 -0
- package/core/tasks.ts +176 -0
- package/core/values.ts +433 -0
- package/core/webhooks.ts +85 -0
- package/core/youtube.ts +39 -0
- package/internals/cli.mjs +34 -0
- package/internals/context.ts +67 -0
- package/internals/create-admin.ts +40 -0
- package/internals/describe.ts +25 -0
- package/internals/eject.ts +144 -0
- package/internals/generator.ts +11 -0
- package/internals/integration.ts +481 -0
- package/internals/mcp.ts +397 -0
- package/internals/project.ts +31 -0
- package/internals/push.ts +160 -0
- package/internals/reindex.ts +11 -0
- package/internals/restore.ts +257 -0
- package/internals/seed.ts +15 -0
- package/internals/upgrade.ts +785 -0
- package/{virtual.d.ts → internals/virtual.d.ts} +39 -5
- package/middleware/auth.ts +130 -17
- package/middleware/push.ts +121 -0
- package/package.json +55 -67
- package/platform/cloudflare/cf-env.ts +60 -0
- package/platform/cloudflare/database.ts +28 -0
- package/platform/cloudflare/globals.d.ts +5 -0
- package/platform/cloudflare/storage.ts +33 -0
- package/platform/cloudflare/uploads-route.ts +37 -0
- package/platform/node/database.ts +39 -0
- package/platform/node/storage.ts +52 -0
- package/routes/admin/[...path].astro +71 -0
- package/routes/{pages/admin → admin}/assets/[id].astro +25 -19
- package/routes/admin/assets/index.astro +90 -0
- package/routes/{pages/admin/login.astro → admin/forgot-password.astro} +21 -19
- package/routes/{pages/admin → admin}/invite.astro +13 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +32 -8
- package/routes/api/{cms/[collection] → [collection]}/[...path].ts +67 -15
- package/routes/api/admin/search.ts +89 -0
- package/routes/api/{cms/assets → assets}/[id].ts +24 -14
- package/routes/api/{cms/assets → assets}/index.ts +4 -2
- package/routes/api/assets/upload.ts +147 -0
- package/routes/api/auth/forgot-password.ts +77 -0
- package/routes/api/{cms/auth → auth}/invite.ts +41 -9
- package/routes/api/auth/login.ts +167 -0
- package/routes/api/{cms/auth → auth}/logout.ts +11 -2
- package/routes/api/auth/reset-password.ts +85 -0
- package/routes/api/auth/setup.ts +110 -0
- package/routes/api/auth/sso/[provider]/start.ts +34 -0
- package/routes/api/collaboration/index.ts +106 -0
- package/routes/api/cron/_authorize.ts +25 -0
- package/routes/api/{cms/cron → cron}/publish.ts +3 -9
- package/routes/api/cron/tasks.ts +34 -0
- package/routes/api/forms/submit/[slug].ts +143 -0
- package/routes/api/img/[...path].ts +65 -0
- package/routes/api/{cms/preview → preview}/render.ts +8 -3
- package/routes/api/{cms/references → references}/[collection]/[id].ts +31 -1
- package/routes/api/webhooks/[provider].ts +54 -0
- package/README.md +0 -28
- package/dist/admin.d.ts +0 -33
- package/dist/admin.js +0 -92
- package/dist/ai.d.ts +0 -15
- package/dist/ai.js +0 -70
- package/dist/api.d.ts +0 -14
- package/dist/api.js +0 -833
- package/dist/assets.d.ts +0 -51
- package/dist/assets.js +0 -163
- package/dist/auth.d.ts +0 -30
- package/dist/auth.js +0 -132
- package/dist/content.d.ts +0 -3
- package/dist/content.js +0 -29
- package/dist/create-admin.d.ts +0 -5
- package/dist/create-admin.js +0 -23
- package/dist/define.d.ts +0 -254
- package/dist/define.js +0 -36
- package/dist/generator.d.ts +0 -9
- package/dist/generator.js +0 -370
- package/dist/image.d.ts +0 -9
- package/dist/image.js +0 -69
- package/dist/index.d.ts +0 -28
- package/dist/index.js +0 -16
- package/dist/integration.d.ts +0 -14
- package/dist/integration.js +0 -327
- package/dist/locks.d.ts +0 -7
- package/dist/locks.js +0 -37
- package/dist/richtext.d.ts +0 -1
- package/dist/richtext.js +0 -1
- package/dist/runtime.d.ts +0 -24
- package/dist/runtime.js +0 -26
- package/dist/schema.d.ts +0 -17
- package/dist/schema.js +0 -13
- package/dist/seed.d.ts +0 -2
- package/dist/seed.js +0 -84
- package/dist/values.d.ts +0 -8
- package/dist/values.js +0 -102
- package/dist/webhooks.d.ts +0 -2
- package/dist/webhooks.js +0 -73
- package/routes/api/cms/assets/upload.ts +0 -112
- package/routes/api/cms/auth/login.ts +0 -124
- package/routes/api/cms/auth/setup.ts +0 -77
- package/routes/api/cms/img/[...path].ts +0 -24
- package/routes/pages/admin/[...path].astro +0 -1107
- package/routes/pages/admin/assets/index.astro +0 -63
- /package/routes/api/{cms/ai → ai}/alt-text.ts +0 -0
- /package/routes/api/{cms/ai → ai}/seo.ts +0 -0
- /package/routes/api/{cms/ai → ai}/translate.ts +0 -0
- /package/routes/api/{cms/assets → assets}/folders.ts +0 -0
- /package/routes/api/{cms/locks → locks}/[...path].ts +0 -0
package/core/search.ts
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { and, eq, sql } from "drizzle-orm";
|
|
2
|
+
|
|
3
|
+
import type { CollectionConfig, FieldConfig, RichTextDocument, SearchableConfig } from "./define";
|
|
4
|
+
import { getLabelField, getTranslatableFieldNames } from "./define";
|
|
5
|
+
import { getDb } from "./runtime";
|
|
6
|
+
import { getSchema } from "./schema";
|
|
7
|
+
import { escapeHtml, richTextToPlainText } from "./values";
|
|
8
|
+
|
|
9
|
+
// Control-character delimiters for FTS5's snippet() — impossible in real content,
|
|
10
|
+
// so escapeHtml + split/join produces safe HTML with <mark>…</mark> around matches.
|
|
11
|
+
const MARK_OPEN = "M";
|
|
12
|
+
const MARK_CLOSE = "E";
|
|
13
|
+
|
|
14
|
+
export type SearchResult = {
|
|
15
|
+
collection: string;
|
|
16
|
+
docId: string;
|
|
17
|
+
locale: string | null;
|
|
18
|
+
title: string;
|
|
19
|
+
url: string;
|
|
20
|
+
snippet: string;
|
|
21
|
+
publishedAt: string | null;
|
|
22
|
+
rank: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type SearchSort = "relevance" | "title" | "date";
|
|
26
|
+
|
|
27
|
+
export type SearchOptions = {
|
|
28
|
+
locale?: string;
|
|
29
|
+
collections?: string[];
|
|
30
|
+
docIds?: string[];
|
|
31
|
+
sort?: SearchSort;
|
|
32
|
+
limit?: number;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const DEFAULT_SEARCHABLE_TYPES = new Set(["text", "slug", "richText", "content", "blocks", "array"]);
|
|
36
|
+
|
|
37
|
+
let schemaReady = false;
|
|
38
|
+
|
|
39
|
+
export const resetSearchSchemaCache = () => {
|
|
40
|
+
schemaReady = false;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const ensureSearchSchema = async (): Promise<void> => {
|
|
44
|
+
if (schemaReady) return;
|
|
45
|
+
const db = await getDb();
|
|
46
|
+
|
|
47
|
+
// Schema migration: if the existing table was created with an older column set
|
|
48
|
+
// or tokenizer, drop it so we can recreate. Rows are rebuilt on the next
|
|
49
|
+
// indexDocument / reindex.
|
|
50
|
+
const existing = (await db.all(
|
|
51
|
+
sql`SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'cms_search_index'`,
|
|
52
|
+
)) as Array<{ sql: string }>;
|
|
53
|
+
const existingSql = existing.length > 0 ? String(existing[0].sql) : "";
|
|
54
|
+
const schemaUpToDate = existingSql.includes("published_at") && existingSql.includes("trigram");
|
|
55
|
+
if (existing.length > 0 && !schemaUpToDate) {
|
|
56
|
+
console.log("[search] FTS5 schema outdated, rebuilding table (run pnpm cms:reindex to repopulate)");
|
|
57
|
+
await db.run(sql`DROP TABLE cms_search_index`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Trigram tokenizer indexes every 3-char slice, so MATCH matches substrings
|
|
61
|
+
// of tokens (handles compound words) and overlapping trigrams give partial
|
|
62
|
+
// scores for typos.
|
|
63
|
+
await db.run(
|
|
64
|
+
sql`CREATE VIRTUAL TABLE IF NOT EXISTS cms_search_index USING fts5(
|
|
65
|
+
title,
|
|
66
|
+
body,
|
|
67
|
+
collection UNINDEXED,
|
|
68
|
+
doc_id UNINDEXED,
|
|
69
|
+
locale UNINDEXED,
|
|
70
|
+
url UNINDEXED,
|
|
71
|
+
status UNINDEXED,
|
|
72
|
+
published_at UNINDEXED,
|
|
73
|
+
tokenize = 'trigram'
|
|
74
|
+
)`,
|
|
75
|
+
);
|
|
76
|
+
schemaReady = true;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const isJsonField = (field: FieldConfig) =>
|
|
80
|
+
field.type === "richText" ||
|
|
81
|
+
field.type === "content" ||
|
|
82
|
+
field.type === "array" ||
|
|
83
|
+
field.type === "json" ||
|
|
84
|
+
field.type === "blocks" ||
|
|
85
|
+
(field.type === "relation" && field.hasMany);
|
|
86
|
+
|
|
87
|
+
const parseJsonFields = (collection: CollectionConfig, row: Record<string, unknown>) => {
|
|
88
|
+
const out: Record<string, unknown> = { ...row };
|
|
89
|
+
for (const [name, field] of Object.entries(collection.fields)) {
|
|
90
|
+
if (isJsonField(field) && typeof out[name] === "string") {
|
|
91
|
+
try {
|
|
92
|
+
out[name] = JSON.parse(out[name] as string);
|
|
93
|
+
} catch {
|
|
94
|
+
// leave as-is
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const extractStrings = (value: unknown): string => {
|
|
102
|
+
if (value == null) return "";
|
|
103
|
+
if (typeof value === "string") return value;
|
|
104
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
105
|
+
if (Array.isArray(value)) return value.map(extractStrings).filter(Boolean).join(" ");
|
|
106
|
+
if (typeof value === "object") {
|
|
107
|
+
return Object.entries(value as Record<string, unknown>)
|
|
108
|
+
.filter(([key]) => key !== "type")
|
|
109
|
+
.map(([, v]) => extractStrings(v))
|
|
110
|
+
.filter(Boolean)
|
|
111
|
+
.join(" ");
|
|
112
|
+
}
|
|
113
|
+
return "";
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const flattenField = (field: FieldConfig, value: unknown): string => {
|
|
117
|
+
if (value == null) return "";
|
|
118
|
+
if (field.type === "richText") {
|
|
119
|
+
try {
|
|
120
|
+
return richTextToPlainText(value as RichTextDocument);
|
|
121
|
+
} catch {
|
|
122
|
+
return "";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (field.type === "content" || field.type === "blocks" || field.type === "array") {
|
|
126
|
+
return extractStrings(value);
|
|
127
|
+
}
|
|
128
|
+
if (typeof value === "string") return value;
|
|
129
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
130
|
+
return "";
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const isCollectionSearchable = (collection: CollectionConfig): boolean => !!collection.searchable;
|
|
134
|
+
|
|
135
|
+
const resolveSearchableFieldNames = (collection: CollectionConfig): string[] => {
|
|
136
|
+
const cfg: SearchableConfig | undefined = collection.searchable;
|
|
137
|
+
if (!cfg) return [];
|
|
138
|
+
if (cfg === true) {
|
|
139
|
+
return Object.entries(collection.fields)
|
|
140
|
+
.filter(([, field]) => DEFAULT_SEARCHABLE_TYPES.has(field.type))
|
|
141
|
+
.map(([name]) => name);
|
|
142
|
+
}
|
|
143
|
+
return cfg.fields;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const buildUrl = (collection: CollectionConfig, doc: Record<string, unknown>, locale: string | null): string => {
|
|
147
|
+
const slugValue = String(doc.slug ?? "");
|
|
148
|
+
const prefix = collection.pathPrefix ? `/${collection.pathPrefix}` : "";
|
|
149
|
+
const path = slugValue === "home" ? "" : `/${slugValue}`;
|
|
150
|
+
const localePrefix = locale ? `/${locale}` : "";
|
|
151
|
+
return `${localePrefix}${prefix}${path}`;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Re-indexes a single document across all supported locales. Always deletes existing rows first,
|
|
156
|
+
* then inserts fresh rows for each locale where the document is published. Idempotent.
|
|
157
|
+
* Errors are logged but never thrown — search must never break a write.
|
|
158
|
+
*/
|
|
159
|
+
export const indexDocument = async (collection: CollectionConfig, docId: string, locales: string[]): Promise<void> => {
|
|
160
|
+
if (!isCollectionSearchable(collection)) return;
|
|
161
|
+
try {
|
|
162
|
+
await ensureSearchSchema();
|
|
163
|
+
const db = await getDb();
|
|
164
|
+
const schema = getSchema();
|
|
165
|
+
const tables = schema.cmsTables[collection.slug] as
|
|
166
|
+
| {
|
|
167
|
+
main: any;
|
|
168
|
+
translations?: any;
|
|
169
|
+
}
|
|
170
|
+
| undefined;
|
|
171
|
+
if (!tables) return;
|
|
172
|
+
|
|
173
|
+
await db.run(sql`DELETE FROM cms_search_index WHERE collection = ${collection.slug} AND doc_id = ${docId}`);
|
|
174
|
+
|
|
175
|
+
const rows = await db.select().from(tables.main).where(eq(tables.main._id, docId)).limit(1);
|
|
176
|
+
if (rows.length === 0) return;
|
|
177
|
+
const base = parseJsonFields(collection, rows[0] as Record<string, unknown>);
|
|
178
|
+
|
|
179
|
+
// Published-only — if the collection supports drafts and this doc isn't published, skip.
|
|
180
|
+
if (collection.drafts && base._status !== "published") return;
|
|
181
|
+
|
|
182
|
+
const translatableFields = getTranslatableFieldNames(collection);
|
|
183
|
+
const hasTranslations = !!tables.translations && translatableFields.length > 0;
|
|
184
|
+
const localesToIndex: Array<string | null> = hasTranslations && locales.length > 0 ? [...locales] : [null];
|
|
185
|
+
|
|
186
|
+
const searchableFieldNames = resolveSearchableFieldNames(collection);
|
|
187
|
+
const labelField = getLabelField(collection);
|
|
188
|
+
|
|
189
|
+
for (const locale of localesToIndex) {
|
|
190
|
+
const doc: Record<string, unknown> = { ...base };
|
|
191
|
+
if (locale && hasTranslations) {
|
|
192
|
+
const trRows = await db
|
|
193
|
+
.select()
|
|
194
|
+
.from(tables.translations)
|
|
195
|
+
.where(and(eq(tables.translations._entityId, docId), eq(tables.translations._languageCode, locale)))
|
|
196
|
+
.limit(1);
|
|
197
|
+
if (trRows.length > 0) {
|
|
198
|
+
const translation = parseJsonFields(collection, trRows[0] as Record<string, unknown>);
|
|
199
|
+
for (const fieldName of translatableFields) {
|
|
200
|
+
if (translation[fieldName] !== undefined && translation[fieldName] !== null) {
|
|
201
|
+
doc[fieldName] = translation[fieldName];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const title = String(doc[labelField] ?? doc.slug ?? docId);
|
|
208
|
+
const bodyParts = searchableFieldNames
|
|
209
|
+
.map((name) => {
|
|
210
|
+
const field = collection.fields[name];
|
|
211
|
+
if (!field) return "";
|
|
212
|
+
return flattenField(field, doc[name]);
|
|
213
|
+
})
|
|
214
|
+
.map((chunk) => chunk.trim())
|
|
215
|
+
.filter(Boolean);
|
|
216
|
+
const body = bodyParts.join("\n\n");
|
|
217
|
+
const url = buildUrl(collection, doc, locale);
|
|
218
|
+
const publishedAt = String(doc._publishedAt ?? doc._createdAt ?? "");
|
|
219
|
+
|
|
220
|
+
await db.run(
|
|
221
|
+
sql`INSERT INTO cms_search_index (title, body, collection, doc_id, locale, url, status, published_at)
|
|
222
|
+
VALUES (${title}, ${body}, ${collection.slug}, ${docId}, ${locale ?? ""}, ${url}, 'published', ${publishedAt})`,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.warn("[search] failed to index", collection.slug, docId, error);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
export const removeDocument = async (collectionSlug: string, docId: string): Promise<void> => {
|
|
231
|
+
try {
|
|
232
|
+
await ensureSearchSchema();
|
|
233
|
+
const db = await getDb();
|
|
234
|
+
await db.run(sql`DELETE FROM cms_search_index WHERE collection = ${collectionSlug} AND doc_id = ${docId}`);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
console.warn("[search] failed to remove", collectionSlug, docId, error);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const termToTrigrams = (term: string): string[] => {
|
|
241
|
+
const out: string[] = [];
|
|
242
|
+
for (let i = 0; i <= term.length - 3; i++) out.push(term.slice(i, i + 3));
|
|
243
|
+
return out;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Build an FTS5 query for the trigram tokenizer. Each term expands to
|
|
248
|
+
* `("term" OR "tri" OR "rig" OR …)` — matching either the literal substring
|
|
249
|
+
* or any of its overlapping trigrams. The trigram OR is what catches typos
|
|
250
|
+
* (most edits leave most trigrams intact); the JS fuzzy re-rank then puts
|
|
251
|
+
* the best-matching row on top. Terms shorter than 3 chars are dropped
|
|
252
|
+
* because the trigram tokenizer cannot index them.
|
|
253
|
+
*/
|
|
254
|
+
const toFtsQuery = (raw: string): string => {
|
|
255
|
+
const terms = raw
|
|
256
|
+
.replace(/["'()]/g, " ")
|
|
257
|
+
.split(/\s+/)
|
|
258
|
+
.map((term) => term.trim())
|
|
259
|
+
.filter((term) => term.length >= 3);
|
|
260
|
+
if (terms.length === 0) return "";
|
|
261
|
+
|
|
262
|
+
return terms
|
|
263
|
+
.map((term) => {
|
|
264
|
+
const parts = [`"${term}"`, ...termToTrigrams(term).map((t) => `"${t}"`)];
|
|
265
|
+
return `(${parts.join(" OR ")})`;
|
|
266
|
+
})
|
|
267
|
+
.join(" ");
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Classic two-row Levenshtein — cheap for short strings, O(|a|·|b|) time.
|
|
272
|
+
* Used for fuzzy re-ranking after trigram match.
|
|
273
|
+
*/
|
|
274
|
+
const levenshtein = (a: string, b: string): number => {
|
|
275
|
+
if (a === b) return 0;
|
|
276
|
+
if (a.length === 0) return b.length;
|
|
277
|
+
if (b.length === 0) return a.length;
|
|
278
|
+
let prev = new Array(b.length + 1);
|
|
279
|
+
let curr = new Array(b.length + 1);
|
|
280
|
+
for (let j = 0; j <= b.length; j++) prev[j] = j;
|
|
281
|
+
for (let i = 1; i <= a.length; i++) {
|
|
282
|
+
curr[0] = i;
|
|
283
|
+
for (let j = 1; j <= b.length; j++) {
|
|
284
|
+
const cost = a.charCodeAt(i - 1) === b.charCodeAt(j - 1) ? 0 : 1;
|
|
285
|
+
curr[j] = Math.min(curr[j - 1] + 1, prev[j] + 1, prev[j - 1] + cost);
|
|
286
|
+
}
|
|
287
|
+
[prev, curr] = [curr, prev];
|
|
288
|
+
}
|
|
289
|
+
return prev[b.length];
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Max absolute edit distance we accept before declaring a term to have no
|
|
294
|
+
* real match. Tight for short terms so noise like "kissa" ↔ "heissan"
|
|
295
|
+
* (distance 2-3) gets filtered, looser for longer terms where 2 edits is
|
|
296
|
+
* still a plausible typo.
|
|
297
|
+
*/
|
|
298
|
+
const distanceCap = (termLength: number) => (termLength <= 5 ? 1 : 2);
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Best edit distance between `term` and the most relevant substring of
|
|
302
|
+
* `word`. Candidate substrings are anchored to positions where any trigram
|
|
303
|
+
* of the query term actually appears in the word — this is what lets
|
|
304
|
+
* "kieko" match inside "jääkiekko" (the trigram "kie" pins the window to
|
|
305
|
+
* position 3, so we compare "kiekk" / "kiekko" to "kieko" and find dist 1)
|
|
306
|
+
* while still rejecting "kissa" in "heissan" (no query trigram anchors to
|
|
307
|
+
* a substring that's close enough).
|
|
308
|
+
*/
|
|
309
|
+
const bestDistance = (term: string, word: string): number => {
|
|
310
|
+
if (word === term) return 0;
|
|
311
|
+
if (word.includes(term)) return 0;
|
|
312
|
+
let best = levenshtein(term, word);
|
|
313
|
+
if (best === 0 || word.length <= term.length) return best;
|
|
314
|
+
|
|
315
|
+
const trigrams = termToTrigrams(term);
|
|
316
|
+
if (trigrams.length === 0) return best;
|
|
317
|
+
|
|
318
|
+
const anchors = new Set<number>();
|
|
319
|
+
for (const tri of trigrams) {
|
|
320
|
+
for (let i = word.indexOf(tri); i !== -1; i = word.indexOf(tri, i + 1)) {
|
|
321
|
+
anchors.add(i);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
for (const pos of anchors) {
|
|
326
|
+
for (const len of [term.length, term.length + 1]) {
|
|
327
|
+
if (pos + len > word.length) continue;
|
|
328
|
+
const d = levenshtein(term, word.substring(pos, pos + len));
|
|
329
|
+
if (d < best) best = d;
|
|
330
|
+
if (best === 0) return 0;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return best;
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Score a doc against the query: for each ≥3-char query term, find the best
|
|
338
|
+
* matching word in the doc. Returns null if any term has no match within
|
|
339
|
+
* its `distanceCap`, meaning the doc is not a real hit and should be dropped.
|
|
340
|
+
* Otherwise returns a summed-distance score where lower is better (0 = all
|
|
341
|
+
* terms substring-matched in the doc).
|
|
342
|
+
*/
|
|
343
|
+
const fuzzyScore = (queryTerms: string[], docText: string): number | null => {
|
|
344
|
+
if (queryTerms.length === 0) return 0;
|
|
345
|
+
const words = docText
|
|
346
|
+
.toLowerCase()
|
|
347
|
+
.split(/[^\p{L}\p{N}]+/u)
|
|
348
|
+
.filter(Boolean);
|
|
349
|
+
if (words.length === 0) return null;
|
|
350
|
+
|
|
351
|
+
let total = 0;
|
|
352
|
+
for (const term of queryTerms) {
|
|
353
|
+
const cap = distanceCap(term.length);
|
|
354
|
+
let best = Infinity;
|
|
355
|
+
for (const word of words) {
|
|
356
|
+
const d = bestDistance(term, word);
|
|
357
|
+
if (d < best) best = d;
|
|
358
|
+
if (best === 0) break;
|
|
359
|
+
}
|
|
360
|
+
if (best > cap) return null;
|
|
361
|
+
total += best;
|
|
362
|
+
}
|
|
363
|
+
return total;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
export const search = async (query: string, options: SearchOptions = {}): Promise<SearchResult[]> => {
|
|
367
|
+
const fts = toFtsQuery(query);
|
|
368
|
+
if (!fts) return [];
|
|
369
|
+
await ensureSearchSchema();
|
|
370
|
+
const db = await getDb();
|
|
371
|
+
|
|
372
|
+
const limit = Math.min(Math.max(options.limit ?? 20, 1), 200);
|
|
373
|
+
|
|
374
|
+
const conditions: Array<ReturnType<typeof sql>> = [sql`cms_search_index MATCH ${fts}`];
|
|
375
|
+
if (options.locale) {
|
|
376
|
+
conditions.push(sql`locale = ${options.locale}`);
|
|
377
|
+
}
|
|
378
|
+
if (options.collections && options.collections.length > 0) {
|
|
379
|
+
const values = options.collections.map((slug) => sql`${slug}`);
|
|
380
|
+
conditions.push(sql`collection IN (${sql.join(values, sql`, `)})`);
|
|
381
|
+
}
|
|
382
|
+
if (options.docIds && options.docIds.length > 0) {
|
|
383
|
+
const values = options.docIds.map((id) => sql`${id}`);
|
|
384
|
+
conditions.push(sql`doc_id IN (${sql.join(values, sql`, `)})`);
|
|
385
|
+
}
|
|
386
|
+
const whereClause = sql.join(conditions, sql` AND `);
|
|
387
|
+
|
|
388
|
+
const orderBy =
|
|
389
|
+
options.sort === "title"
|
|
390
|
+
? sql`title COLLATE NOCASE ASC`
|
|
391
|
+
: options.sort === "date"
|
|
392
|
+
? // Empty strings sort to the end so docs with a real publishedAt come first.
|
|
393
|
+
sql`CASE WHEN published_at = '' THEN 1 ELSE 0 END, published_at DESC`
|
|
394
|
+
: sql`bm25(cms_search_index)`;
|
|
395
|
+
|
|
396
|
+
const rows = (await db.all(
|
|
397
|
+
sql`SELECT
|
|
398
|
+
collection,
|
|
399
|
+
doc_id AS "docId",
|
|
400
|
+
locale,
|
|
401
|
+
title,
|
|
402
|
+
url,
|
|
403
|
+
published_at AS "publishedAt",
|
|
404
|
+
snippet(cms_search_index, 1, ${MARK_OPEN}, ${MARK_CLOSE}, '…', 12) AS snippet,
|
|
405
|
+
bm25(cms_search_index) AS rank
|
|
406
|
+
FROM cms_search_index
|
|
407
|
+
WHERE ${whereClause}
|
|
408
|
+
ORDER BY ${orderBy}
|
|
409
|
+
LIMIT ${limit}`,
|
|
410
|
+
)) as Array<{
|
|
411
|
+
collection: string;
|
|
412
|
+
docId: string;
|
|
413
|
+
locale: string;
|
|
414
|
+
title: string;
|
|
415
|
+
url: string;
|
|
416
|
+
publishedAt: string;
|
|
417
|
+
snippet: string;
|
|
418
|
+
rank: number;
|
|
419
|
+
}>;
|
|
420
|
+
|
|
421
|
+
const mapped = rows.map((row) => ({
|
|
422
|
+
collection: row.collection,
|
|
423
|
+
docId: row.docId,
|
|
424
|
+
locale: row.locale && row.locale !== "" ? row.locale : null,
|
|
425
|
+
title: row.title,
|
|
426
|
+
url: row.url,
|
|
427
|
+
publishedAt: row.publishedAt && row.publishedAt !== "" ? row.publishedAt : null,
|
|
428
|
+
snippet: escapeHtml(row.snippet).split(MARK_OPEN).join("<mark>").split(MARK_CLOSE).join("</mark>"),
|
|
429
|
+
rank: row.rank,
|
|
430
|
+
// Raw snippet with FTS5 markers stripped, for fuzzy scoring
|
|
431
|
+
_scoringText: `${row.title} ${row.snippet.split(MARK_OPEN).join("").split(MARK_CLOSE).join("")}`,
|
|
432
|
+
}));
|
|
433
|
+
|
|
434
|
+
// Fuzzy filter runs regardless of sort mode — it's a correctness gate, not
|
|
435
|
+
// a ranking concern. FTS5's trigram OR is permissive by design (to catch
|
|
436
|
+
// typos); without this filter, a random string like "kikkomaanidkssoecncru"
|
|
437
|
+
// matches any doc that happens to contain any 3-char slice.
|
|
438
|
+
const queryTerms = query
|
|
439
|
+
.toLowerCase()
|
|
440
|
+
.replace(/["'()]/g, " ")
|
|
441
|
+
.split(/\s+/)
|
|
442
|
+
.map((t) => t.trim())
|
|
443
|
+
.filter((t) => t.length >= 3);
|
|
444
|
+
const scored = mapped
|
|
445
|
+
.map((row) => ({ row, fuzz: fuzzyScore(queryTerms, row._scoringText) }))
|
|
446
|
+
.filter((entry): entry is { row: (typeof mapped)[number]; fuzz: number } => entry.fuzz !== null);
|
|
447
|
+
|
|
448
|
+
// Relevance mode re-sorts by fuzzy distance (bm25 tiebreaker). Title/date
|
|
449
|
+
// sorts stay in the order SQL already gave us — Array.filter is stable.
|
|
450
|
+
if ((options.sort ?? "relevance") === "relevance") {
|
|
451
|
+
scored.sort((a, b) => {
|
|
452
|
+
if (a.fuzz !== b.fuzz) return a.fuzz - b.fuzz;
|
|
453
|
+
return a.row.rank - b.row.rank;
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return scored.map(({ row }) => {
|
|
458
|
+
const { _scoringText, ...rest } = row;
|
|
459
|
+
return rest;
|
|
460
|
+
});
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Full rebuild: clears the index and re-indexes every document in every searchable collection.
|
|
465
|
+
* Safe to run at any time; used by `pnpm cms:reindex`.
|
|
466
|
+
*/
|
|
467
|
+
export const reindexAll = async (collections: CollectionConfig[], locales: string[]): Promise<{ indexed: number }> => {
|
|
468
|
+
await ensureSearchSchema();
|
|
469
|
+
const db = await getDb();
|
|
470
|
+
await db.run(sql`DELETE FROM cms_search_index`);
|
|
471
|
+
|
|
472
|
+
const schema = getSchema();
|
|
473
|
+
let indexed = 0;
|
|
474
|
+
|
|
475
|
+
for (const collection of collections) {
|
|
476
|
+
if (!isCollectionSearchable(collection)) continue;
|
|
477
|
+
const tables = schema.cmsTables[collection.slug] as { main: any } | undefined;
|
|
478
|
+
if (!tables) continue;
|
|
479
|
+
|
|
480
|
+
const rows = await db.select().from(tables.main);
|
|
481
|
+
for (const row of rows) {
|
|
482
|
+
const doc = row as Record<string, unknown>;
|
|
483
|
+
await indexDocument(collection, String(doc._id), locales);
|
|
484
|
+
indexed++;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return { indexed };
|
|
489
|
+
};
|
package/core/seed.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { sql } from "drizzle-orm";
|
|
2
|
+
import { nanoid } from "nanoid";
|
|
3
|
+
|
|
4
|
+
import { hashPassword } from "./auth";
|
|
5
|
+
import type { CMSConfig, CollectionConfig, FieldConfig, SeedDocument } from "./define";
|
|
6
|
+
import { getDb } from "./runtime";
|
|
7
|
+
import { getSchema } from "./schema";
|
|
8
|
+
import { cloneValue, slugify } from "./values";
|
|
9
|
+
|
|
10
|
+
const isJsonField = (field: FieldConfig) =>
|
|
11
|
+
field.type === "richText" ||
|
|
12
|
+
field.type === "content" ||
|
|
13
|
+
field.type === "array" ||
|
|
14
|
+
field.type === "json" ||
|
|
15
|
+
field.type === "blocks" ||
|
|
16
|
+
(field.type === "relation" && field.hasMany);
|
|
17
|
+
|
|
18
|
+
const serializeForDb = (collection: CollectionConfig, data: Record<string, unknown>): Record<string, unknown> => {
|
|
19
|
+
const result: Record<string, unknown> = {};
|
|
20
|
+
for (const [key, value] of Object.entries(data)) {
|
|
21
|
+
const field = collection.fields[key];
|
|
22
|
+
result[key] = field && isJsonField(field) && value !== undefined && value !== null ? JSON.stringify(value) : value;
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const seedDatabase = async (config: CMSConfig, seedData: Record<string, SeedDocument[]>) => {
|
|
28
|
+
const db = await getDb();
|
|
29
|
+
const schema = getSchema();
|
|
30
|
+
|
|
31
|
+
for (const collection of config.collections) {
|
|
32
|
+
const collectionSeed = seedData[collection.slug];
|
|
33
|
+
if (!collectionSeed || collectionSeed.length === 0) continue;
|
|
34
|
+
|
|
35
|
+
const tables = schema.cmsTables[collection.slug] as {
|
|
36
|
+
main: any;
|
|
37
|
+
translations?: any;
|
|
38
|
+
versions?: any;
|
|
39
|
+
};
|
|
40
|
+
if (!tables) continue;
|
|
41
|
+
|
|
42
|
+
const countResult = await db.select({ count: sql<number>`count(*)` }).from(tables.main);
|
|
43
|
+
const rowCount = Number(countResult[0]?.count ?? 0);
|
|
44
|
+
if (rowCount > 0) continue;
|
|
45
|
+
|
|
46
|
+
console.log(` Seeding ${collection.labels.plural}...`);
|
|
47
|
+
|
|
48
|
+
for (const seedDoc of collectionSeed) {
|
|
49
|
+
const timestamp = new Date().toISOString();
|
|
50
|
+
const docId = typeof seedDoc._id === "string" ? String(seedDoc._id) : nanoid();
|
|
51
|
+
const { _id, _status, ...fieldData } = seedDoc as Record<string, unknown>;
|
|
52
|
+
|
|
53
|
+
for (const [fieldName, field] of Object.entries(collection.fields)) {
|
|
54
|
+
if (field.type !== "slug") continue;
|
|
55
|
+
if (fieldData[fieldName]) {
|
|
56
|
+
fieldData[fieldName] = slugify(String(fieldData[fieldName]));
|
|
57
|
+
} else if (field.from && fieldData[field.from]) {
|
|
58
|
+
fieldData[fieldName] = slugify(String(fieldData[field.from]));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const [fieldName, field] of Object.entries(collection.fields)) {
|
|
63
|
+
if (fieldData[fieldName] === undefined && field.defaultValue !== undefined) {
|
|
64
|
+
fieldData[fieldName] = cloneValue(field.defaultValue);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (collection.auth && typeof fieldData.password === "string") {
|
|
69
|
+
fieldData.password = await hashPassword(fieldData.password);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const serialized = serializeForDb(collection, fieldData);
|
|
73
|
+
const docValues: Record<string, unknown> = {
|
|
74
|
+
_id: docId,
|
|
75
|
+
...serialized,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
if (collection.drafts) {
|
|
79
|
+
const status = _status === "published" ? "published" : "draft";
|
|
80
|
+
docValues._status = status;
|
|
81
|
+
if (status === "published") docValues._publishedAt = timestamp;
|
|
82
|
+
}
|
|
83
|
+
if (collection.timestamps !== false) {
|
|
84
|
+
docValues._createdAt = timestamp;
|
|
85
|
+
docValues._updatedAt = timestamp;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await db.insert(tables.main).values(docValues);
|
|
89
|
+
|
|
90
|
+
if (collection.versions && tables.versions) {
|
|
91
|
+
await db.insert(tables.versions).values({
|
|
92
|
+
_id: nanoid(),
|
|
93
|
+
_docId: docId,
|
|
94
|
+
_version: 1,
|
|
95
|
+
_snapshot: JSON.stringify({ ...fieldData, _status: docValues._status }),
|
|
96
|
+
_createdAt: timestamp,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { CMSConfig, CollectionConfig, FieldConfig } from "./define";
|
|
2
|
+
|
|
3
|
+
export const SHARED_SECTIONS_COLLECTION = "shared-sections";
|
|
4
|
+
export const SHARED_BLOCK_TYPE = "__shared";
|
|
5
|
+
|
|
6
|
+
export type SharedBlockReference = {
|
|
7
|
+
type: typeof SHARED_BLOCK_TYPE;
|
|
8
|
+
ref: string;
|
|
9
|
+
title?: string;
|
|
10
|
+
blockType?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type SharedSectionOption = {
|
|
14
|
+
id: string;
|
|
15
|
+
title: string;
|
|
16
|
+
blockType: string;
|
|
17
|
+
status?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const isSharedBlockReference = (value: unknown): value is SharedBlockReference =>
|
|
21
|
+
!!value &&
|
|
22
|
+
typeof value === "object" &&
|
|
23
|
+
(value as Record<string, unknown>).type === SHARED_BLOCK_TYPE &&
|
|
24
|
+
typeof (value as Record<string, unknown>).ref === "string" &&
|
|
25
|
+
String((value as Record<string, unknown>).ref).length > 0;
|
|
26
|
+
|
|
27
|
+
export const getSharedSectionCacheTags = (id: string) => [SHARED_SECTIONS_COLLECTION, `shared-section:${id}`];
|
|
28
|
+
|
|
29
|
+
export const getSharedBlockTypes = (config: CMSConfig): Record<string, Record<string, FieldConfig>> => {
|
|
30
|
+
const types: Record<string, Record<string, FieldConfig>> = {};
|
|
31
|
+
|
|
32
|
+
for (const collection of config.collections) {
|
|
33
|
+
if (collection.slug === SHARED_SECTIONS_COLLECTION) continue;
|
|
34
|
+
for (const field of Object.values(collection.fields)) {
|
|
35
|
+
const blockTypes =
|
|
36
|
+
field.type === "blocks" && field.shared !== false
|
|
37
|
+
? field.types
|
|
38
|
+
: field.type === "content"
|
|
39
|
+
? field.blocks
|
|
40
|
+
: null;
|
|
41
|
+
if (!blockTypes) continue;
|
|
42
|
+
|
|
43
|
+
for (const [typeName, typeFields] of Object.entries(blockTypes)) {
|
|
44
|
+
if (!types[typeName]) types[typeName] = typeFields;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return types;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const extractSharedSectionRefsFromBlocks = (value: unknown): string[] => {
|
|
53
|
+
const blocks = Array.isArray(value) ? value : [];
|
|
54
|
+
return blocks
|
|
55
|
+
.filter(isSharedBlockReference)
|
|
56
|
+
.map((block) => block.ref)
|
|
57
|
+
.filter(Boolean);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const extractSharedSectionRefsFromContent = (value: unknown): string[] => {
|
|
61
|
+
if (!value || typeof value !== "object") return [];
|
|
62
|
+
const children = (value as { children?: unknown }).children;
|
|
63
|
+
if (!Array.isArray(children)) return [];
|
|
64
|
+
|
|
65
|
+
return children
|
|
66
|
+
.filter((node): node is { type: "block"; blockType: string; fields?: Record<string, unknown> } => {
|
|
67
|
+
if (!node || typeof node !== "object") return false;
|
|
68
|
+
const candidate = node as Record<string, unknown>;
|
|
69
|
+
return candidate.type === "block" && candidate.blockType === SHARED_BLOCK_TYPE;
|
|
70
|
+
})
|
|
71
|
+
.map((node) => String(node.fields?.ref ?? ""))
|
|
72
|
+
.filter(Boolean);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const extractSharedSectionRefsFromDocument = (
|
|
76
|
+
collection: Pick<CollectionConfig, "fields">,
|
|
77
|
+
doc: Record<string, unknown>,
|
|
78
|
+
): string[] => {
|
|
79
|
+
const refs = new Set<string>();
|
|
80
|
+
|
|
81
|
+
for (const [fieldName, field] of Object.entries(collection.fields)) {
|
|
82
|
+
const value = doc[fieldName];
|
|
83
|
+
const fieldRefs =
|
|
84
|
+
field.type === "blocks" && field.shared !== false
|
|
85
|
+
? extractSharedSectionRefsFromBlocks(value)
|
|
86
|
+
: field.type === "content"
|
|
87
|
+
? extractSharedSectionRefsFromContent(value)
|
|
88
|
+
: [];
|
|
89
|
+
for (const ref of fieldRefs) refs.add(ref);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return [...refs];
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const getSharedSectionTagsFromBlocks = (value: unknown): string[] =>
|
|
96
|
+
extractSharedSectionRefsFromBlocks(value).flatMap(getSharedSectionCacheTags);
|