@bettercms-ai/sveltekit 0.0.0-stage → 0.3.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/README.md CHANGED
@@ -1,3 +1,102 @@
1
- # Temporary Holding Version
1
+ # @bettercms-ai/sveltekit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ BetterCMS for SvelteKit. Your own components stay your own: they spread a few attributes onto their elements, and every element becomes editable in the BetterCMS visual editor. Pages stay current when content is published.
4
+
5
+ ```bash
6
+ npm i @bettercms-ai/sveltekit
7
+ ```
8
+
9
+ ```ts
10
+ // src/lib/bcms.ts
11
+ import { env as pub } from "$env/dynamic/public";
12
+ import { env as priv } from "$env/dynamic/private";
13
+ export const bcms = createBcms({ env: { ...pub, ...priv } });
14
+
15
+ // src/hooks.server.ts: drafts, <html data-bcms-draft>, <meta name="generator">
16
+ export const handle = bcmsHandle(bcms); // or sequence(bcmsHandle(bcms), yourHandle)
17
+
18
+ // src/routes/api/draft/+server.ts: the path the editor's "Preview on site" opens
19
+ export const GET = ({ request }) => bcms.draftEnable(request);
20
+
21
+ // src/routes/+page.server.ts: drafts while the editor previews
22
+ export const load = async ({ request }) => ({ post: await (await bcms.client(request)).getEntry("hello-world") });
23
+ ```
24
+
25
+ ```svelte
26
+ <!-- src/routes/+layout.svelte: a publish, and the editor's re-render request, re-run `load` -->
27
+ <script>
28
+ import { onMount } from "svelte";
29
+ import { invalidateAll } from "$app/navigation";
30
+ import { env } from "$env/dynamic/public";
31
+ import { startBcmsLive } from "@bettercms-ai/sveltekit";
32
+ onMount(() => startBcmsLive({ refresh: invalidateAll, src: env.PUBLIC_BCMS_LIVE_URL }));
33
+ </script>
34
+ <slot />
35
+
36
+ <!-- your own markup, bound to its content -->
37
+ <article {...entryAttrs(data.post)}>
38
+ <h1 {...entryField(data.post, "title")}>{data.post.data.title}</h1>
39
+ <img {...imageAttrs(data.post.data.cover, { sizes: "100vw" })} {...entryField(data.post, "cover")} class="cover" />
40
+ <div {...entryField(data.post, "body")}>{@html rich(data.post.data.body)}</div>
41
+ </article>
42
+ ```
43
+
44
+ | Helper | What it binds |
45
+ |---|---|
46
+ | `fieldAttrs(path, page.fieldStyles)` | A page field. It also applies the field's stored style, which turns on the editor's Align, Size and Weight controls. |
47
+ | `entryAttrs(entry)` | A collection entry's card. |
48
+ | `entryField(entry, path)` | A field of that entry. |
49
+ | `sectionRootAttrs(block)` | A section root (its spacing and design). |
50
+ | `sectionColumnAttrs(block)` | The section's content column. |
51
+ | `imageAttrs(value, { path, sizes })` | An optimized `<img>` with a srcset, alt text, the focal point and the field binding. |
52
+ | `rich(value)`, `lines(value)`, `plain(value)` | Rich text as HTML, a one-line value, or plain text for attributes. |
53
+
54
+ ## Sections
55
+
56
+ A page built from sections in BetterCMS renders as the hosted renderer draws it, so forms, sliders and tabs work exactly as they do on a BetterCMS-hosted site. Pass the request and the editor's session gets the draft, with every section and value clickable:
57
+
58
+ ```ts
59
+ // src/routes/+page.server.ts
60
+ export const load = async ({ fetch, request }) => ({ sections: await bcms.sections("home", fetch, request) });
61
+ ```
62
+
63
+ ```svelte
64
+ <div use:bcmsSections={data.sections}>{@html sectionsMarkup(data.sections)}</div>
65
+ ```
66
+
67
+ ### Your own sections in the CMS order
68
+
69
+ A page that draws its own sections can let BetterCMS order them, and draw sections added in the editor between them: name the page's sections (`slots`, in source order) and loop over `sectionItems`. `npx @bettercms-ai/convert --sections` writes this for you.
70
+
71
+ ```ts
72
+ // src/routes/+page.server.ts
73
+ const bcmsSlots = ["hero", "code:features"];
74
+ export const load = async ({ fetch, request }) => ({
75
+ bcmsSections: sectionItems(await bcms.sections("home", fetch, request, { slots: bcmsSlots }).catch(() => null), bcmsSlots, "home"),
76
+ });
77
+ ```
78
+
79
+ ```svelte
80
+ {#each data.bcmsSections as item (item.key)}
81
+ {#if item.slot === "hero"}<section class="hero" {...item.root}>…</section>
82
+ {:else if item.slot === "code:features"}<div {...item.box}><Features /></div>
83
+ {:else if item.slot === null}<div data-bcms-rendered="platform" use:bcmsSections={item.html}>{@html item.html}</div>
84
+ {/if}
85
+ {/each}
86
+ ```
87
+
88
+ `root` goes on a section's own root element (its placement's `data-bcms-block` and style); `box` on a wrapper when the section is a component. With no answer, every slot renders in source order.
89
+
90
+ ## Settings
91
+
92
+ Options are read first, then `PUBLIC_BCMS_*`, then `BCMS_*`, the same precedence as every adapter:
93
+
94
+ | Variable | Purpose |
95
+ |---|---|
96
+ | `PUBLIC_BCMS_API_URL` | The API to read from. |
97
+ | `PUBLIC_BCMS_WORKSPACE` | Your workspace. |
98
+ | `PUBLIC_BCMS_PROJECT_ID` | Your project. |
99
+ | `PUBLIC_BCMS_LIVE_URL` | The live stream. Optional. |
100
+ | `BCMS_DRAFT_SECRET` | Signs the draft cookie. |
101
+
102
+ The sections run small inline scripts (forms, sliders and tabs), so a Content-Security-Policy must allow inline scripts.
@@ -0,0 +1,73 @@
1
+ import { BcmsSite } from '@bettercms-ai/sdk/sections';
2
+ export { BcmsSite, SectionItem, SectionsFragment, SectionsFragmentItem, sectionItems, sectionsMarkup } from '@bettercms-ai/sdk/sections';
3
+ import { BcmsSiteOptions } from '@bettercms-ai/sdk/config';
4
+ export * from '@bettercms-ai/sdk/bindings';
5
+ export { BcmsChangeEvent, StartBcmsLiveOptions, inEditor, liveSrc, onContentApplied, startBcmsLive } from '@bettercms-ai/sdk/live';
6
+ export { lines, plain, rich } from '@bettercms-ai/sdk/read';
7
+
8
+ /**
9
+ * @bettercms-ai/sveltekit — BetterCMS for SvelteKit: the site's own markup bound to its content,
10
+ * so every element is editable in the BetterCMS visual editor, and the site stays live on publish.
11
+ *
12
+ * // src/lib/bcms.ts
13
+ * import { env as pub } from "$env/dynamic/public";
14
+ * import { env as priv } from "$env/dynamic/private";
15
+ * export const bcms = createBcms({ env: { ...pub, ...priv } });
16
+ *
17
+ * // src/hooks.server.ts — drafts, `<html data-bcms-draft>`, `<meta name="generator">`
18
+ * export const handle = bcmsHandle(bcms);
19
+ *
20
+ * // src/routes/+layout.svelte — a publish, and the editor's re-render request, re-run `load`
21
+ * onMount(() => startBcmsLive({ refresh: invalidateAll, src: env.PUBLIC_BCMS_LIVE_URL }));
22
+ *
23
+ * // src/routes/+page.server.ts — drafts while the editor previews
24
+ * export const load = async ({ request }) => ({ page: await (await bcms.client(request)).getPage("home") });
25
+ *
26
+ * // src/routes/+page.svelte — the site's OWN markup, bound to its fields
27
+ * <h1 {...fieldAttrs("hero-title", data.page.fieldStyles)}>{data.page.data["hero-title"]}</h1>
28
+ * <img {...imageAttrs(data.page.data.cover, { path: "cover", sizes: "100vw" })} class="cover" />
29
+ * <div {...fieldAttrs("body")}>{@html rich(data.page.data.body)}</div>
30
+ *
31
+ * // or a page's sections as the hosted renderer draws them
32
+ * <div use:bcmsSections={data.sections}>{@html sectionsMarkup(data.sections)}</div>
33
+ *
34
+ * // src/routes/api/draft/+server.ts — the path the editor's "Preview on site" opens
35
+ * export const GET = ({ request }) => bcms.draftEnable(request);
36
+ *
37
+ * Settings: options, then `PUBLIC_BCMS_*`, then `BCMS_*` (the same precedence as every adapter).
38
+ */
39
+
40
+ /** Build the site's BetterCMS access from its settings (pass `{ ...publicEnv, ...privateEnv }`). */
41
+ declare function createBcms(options?: BcmsSiteOptions & {
42
+ env?: Record<string, string | undefined>;
43
+ draftSecret?: string;
44
+ }): BcmsSite;
45
+ /** The least of SvelteKit's `handle` this needs, so the package carries no @sveltejs/kit types. */
46
+ interface HandleInput {
47
+ event: {
48
+ request: Request;
49
+ locals: object;
50
+ };
51
+ resolve(event: HandleInput["event"], opts?: {
52
+ transformPageChunk?: (input: {
53
+ html: string;
54
+ done: boolean;
55
+ }) => string | undefined;
56
+ }): Response | Promise<Response>;
57
+ }
58
+ /**
59
+ * The server hook: `locals.bcmsDraft` says whether this request previews drafts; every page names
60
+ * BetterCMS in its `<head>`, and in drafts carries `<html data-bcms-draft="1">`, the canvas's proof
61
+ * that the frame renders drafts. Compose it with the site's own hooks through `sequence()`.
62
+ */
63
+ declare function bcmsHandle(bcms: BcmsSite): ({ event, resolve }: HandleInput) => Promise<Response>;
64
+ /**
65
+ * Svelte action for the element holding `{@html sectionsMarkup(…)}`: its scripts run on every page.
66
+ * Pass the sections as its parameter (`use:bcmsSections={data.sections}`): Svelte calls `update`
67
+ * only when the parameter changes, which is how a move between two pages of one route reruns them.
68
+ */
69
+ declare function bcmsSections(node: Element, _sections?: unknown): {
70
+ update(): void;
71
+ };
72
+
73
+ export { bcmsHandle, bcmsSections, createBcms };
package/dist/index.js ADDED
@@ -0,0 +1,36 @@
1
+ // src/index.ts
2
+ import { createBcmsSite, runSectionScripts } from "@bettercms-ai/sdk/sections";
3
+ import { markDraft, withGenerator } from "@bettercms-ai/sdk/live";
4
+ import { sectionsMarkup, sectionItems } from "@bettercms-ai/sdk/sections";
5
+ export * from "@bettercms-ai/sdk/bindings";
6
+ import { startBcmsLive, liveSrc, inEditor, onContentApplied } from "@bettercms-ai/sdk/live";
7
+ import { plain, rich, lines } from "@bettercms-ai/sdk/read";
8
+ function createBcms(options = {}) {
9
+ return createBcmsSite({ ...options, prefixes: ["PUBLIC_"], adapter: "@bettercms-ai/sveltekit" });
10
+ }
11
+ function bcmsHandle(bcms) {
12
+ return async ({ event, resolve }) => {
13
+ const draft = await bcms.isDraft(event.request);
14
+ event.locals.bcmsDraft = draft;
15
+ return resolve(event, { transformPageChunk: ({ html }) => draft ? markDraft(withGenerator(html)) : withGenerator(html) });
16
+ };
17
+ }
18
+ function bcmsSections(node, _sections) {
19
+ runSectionScripts(node);
20
+ return { update: () => queueMicrotask(() => runSectionScripts(node)) };
21
+ }
22
+ export {
23
+ bcmsHandle,
24
+ bcmsSections,
25
+ createBcms,
26
+ inEditor,
27
+ lines,
28
+ liveSrc,
29
+ onContentApplied,
30
+ plain,
31
+ rich,
32
+ sectionItems,
33
+ sectionsMarkup,
34
+ startBcmsLive
35
+ };
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/// <reference lib=\"dom\" />\n/**\n * @bettercms-ai/sveltekit — BetterCMS for SvelteKit: the site's own markup bound to its content,\n * so every element is editable in the BetterCMS visual editor, and the site stays live on publish.\n *\n * // src/lib/bcms.ts\n * import { env as pub } from \"$env/dynamic/public\";\n * import { env as priv } from \"$env/dynamic/private\";\n * export const bcms = createBcms({ env: { ...pub, ...priv } });\n *\n * // src/hooks.server.ts — drafts, `<html data-bcms-draft>`, `<meta name=\"generator\">`\n * export const handle = bcmsHandle(bcms);\n *\n * // src/routes/+layout.svelte — a publish, and the editor's re-render request, re-run `load`\n * onMount(() => startBcmsLive({ refresh: invalidateAll, src: env.PUBLIC_BCMS_LIVE_URL }));\n *\n * // src/routes/+page.server.ts — drafts while the editor previews\n * export const load = async ({ request }) => ({ page: await (await bcms.client(request)).getPage(\"home\") });\n *\n * // src/routes/+page.svelte — the site's OWN markup, bound to its fields\n * <h1 {...fieldAttrs(\"hero-title\", data.page.fieldStyles)}>{data.page.data[\"hero-title\"]}</h1>\n * <img {...imageAttrs(data.page.data.cover, { path: \"cover\", sizes: \"100vw\" })} class=\"cover\" />\n * <div {...fieldAttrs(\"body\")}>{@html rich(data.page.data.body)}</div>\n *\n * // or a page's sections as the hosted renderer draws them\n * <div use:bcmsSections={data.sections}>{@html sectionsMarkup(data.sections)}</div>\n *\n * // src/routes/api/draft/+server.ts — the path the editor's \"Preview on site\" opens\n * export const GET = ({ request }) => bcms.draftEnable(request);\n *\n * Settings: options, then `PUBLIC_BCMS_*`, then `BCMS_*` (the same precedence as every adapter).\n */\nimport { createBcmsSite, runSectionScripts, type BcmsSite } from \"@bettercms-ai/sdk/sections\";\nimport type { BcmsSiteOptions } from \"@bettercms-ai/sdk/config\";\nimport { markDraft, withGenerator } from \"@bettercms-ai/sdk/live\";\n\nexport { sectionsMarkup, sectionItems, type SectionsFragment, type SectionsFragmentItem, type SectionItem, type BcmsSite } from \"@bettercms-ai/sdk/sections\";\nexport * from \"@bettercms-ai/sdk/bindings\";\nexport { startBcmsLive, liveSrc, inEditor, onContentApplied, type BcmsChangeEvent, type StartBcmsLiveOptions } from \"@bettercms-ai/sdk/live\";\nexport { plain, rich, lines } from \"@bettercms-ai/sdk/read\";\n\n/** Build the site's BetterCMS access from its settings (pass `{ ...publicEnv, ...privateEnv }`). */\nexport function createBcms(options: BcmsSiteOptions & { env?: Record<string, string | undefined>; draftSecret?: string } = {}): BcmsSite {\n return createBcmsSite({ ...options, prefixes: [\"PUBLIC_\"], adapter: \"@bettercms-ai/sveltekit\" });\n}\n\n/** The least of SvelteKit's `handle` this needs, so the package carries no @sveltejs/kit types. */\ninterface HandleInput {\n event: { request: Request; locals: object };\n resolve(\n event: HandleInput[\"event\"],\n opts?: { transformPageChunk?: (input: { html: string; done: boolean }) => string | undefined },\n ): Response | Promise<Response>;\n}\n\n/**\n * The server hook: `locals.bcmsDraft` says whether this request previews drafts; every page names\n * BetterCMS in its `<head>`, and in drafts carries `<html data-bcms-draft=\"1\">`, the canvas's proof\n * that the frame renders drafts. Compose it with the site's own hooks through `sequence()`.\n */\nexport function bcmsHandle(bcms: BcmsSite) {\n return async ({ event, resolve }: HandleInput): Promise<Response> => {\n const draft = await bcms.isDraft(event.request);\n (event.locals as { bcmsDraft?: boolean }).bcmsDraft = draft;\n // Each streamed chunk is stamped where it holds the tag; both stamps are no-ops anywhere else.\n return resolve(event, { transformPageChunk: ({ html }) => (draft ? markDraft(withGenerator(html)) : withGenerator(html)) });\n };\n}\n\n/**\n * Svelte action for the element holding `{@html sectionsMarkup(…)}`: its scripts run on every page.\n * Pass the sections as its parameter (`use:bcmsSections={data.sections}`): Svelte calls `update`\n * only when the parameter changes, which is how a move between two pages of one route reruns them.\n */\nexport function bcmsSections(node: Element, _sections?: unknown): { update(): void } {\n runSectionScripts(node);\n // After Svelte has written the new markup.\n return { update: () => queueMicrotask(() => runSectionScripts(node)) };\n}\n"],"mappings":";AAgCA,SAAS,gBAAgB,yBAAwC;AAEjE,SAAS,WAAW,qBAAqB;AAEzC,SAAS,gBAAgB,oBAAuG;AAChI,cAAc;AACd,SAAS,eAAe,SAAS,UAAU,wBAAyE;AACpH,SAAS,OAAO,MAAM,aAAa;AAG5B,SAAS,WAAW,UAAgG,CAAC,GAAa;AACvI,SAAO,eAAe,EAAE,GAAG,SAAS,UAAU,CAAC,SAAS,GAAG,SAAS,0BAA0B,CAAC;AACjG;AAgBO,SAAS,WAAW,MAAgB;AACzC,SAAO,OAAO,EAAE,OAAO,QAAQ,MAAsC;AACnE,UAAM,QAAQ,MAAM,KAAK,QAAQ,MAAM,OAAO;AAC9C,IAAC,MAAM,OAAmC,YAAY;AAEtD,WAAO,QAAQ,OAAO,EAAE,oBAAoB,CAAC,EAAE,KAAK,MAAO,QAAQ,UAAU,cAAc,IAAI,CAAC,IAAI,cAAc,IAAI,EAAG,CAAC;AAAA,EAC5H;AACF;AAOO,SAAS,aAAa,MAAe,WAAyC;AACnF,oBAAkB,IAAI;AAEtB,SAAO,EAAE,QAAQ,MAAM,eAAe,MAAM,kBAAkB,IAAI,CAAC,EAAE;AACvE;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "@bettercms-ai/sveltekit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "description": "BetterCMS for SvelteKit: settings from PUBLIC_BCMS_*, the read client per request with draft mode, the editor's draft routes, and the page sections server-rendered by BetterCMS.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "files": [
11
+ "dist",
12
+ "README.md"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js",
18
+ "default": "./dist/index.js"
19
+ }
20
+ },
21
+ "dependencies": {
22
+ "@bettercms-ai/sdk": "^1.40.0"
23
+ },
24
+ "peerDependencies": {
25
+ "svelte": ">=4"
26
+ },
27
+ "peerDependenciesMeta": {
28
+ "svelte": {
29
+ "optional": true
30
+ }
31
+ },
32
+ "scripts": {
33
+ "typecheck": "tsc --noEmit",
34
+ "build": "tsup",
35
+ "test": "vitest run",
36
+ "prepublishOnly": "bun run build"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^20",
40
+ "tsup": "^8.5.1",
41
+ "typescript": "^5",
42
+ "vitest": "^4.1.4"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "registry": "https://registry.npmjs.org/"
47
+ }
48
+ }