@spillway-cms/astro 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -2
- package/client.d.ts +12 -0
- package/components/SpillwayPage.astro +54 -0
- package/components/SpillwayStyles.astro +13 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -0
- package/dist/render.d.ts +47 -0
- package/dist/render.js +134 -0
- package/dist/render.js.map +1 -0
- package/dist/server.d.ts +25 -0
- package/dist/server.js +39 -0
- package/dist/server.js.map +1 -0
- package/package.json +77 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Accorbis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @spillway-cms/astro
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Astro integration for [Spillway CMS](https://github.com/kwall-llc/spillway-cms). MIT licensed.
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
// astro.config.mjs
|
|
7
|
+
import { spillway } from '@spillway-cms/astro';
|
|
8
|
+
export default defineConfig({ integrations: [spillway({ baseUrl: process.env.SPILLWAY_URL })] });
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```astro
|
|
12
|
+
---
|
|
13
|
+
import { client } from 'spillway:client';
|
|
14
|
+
import { spillwayStaticPaths, pathFromParams } from '@spillway-cms/astro';
|
|
15
|
+
import SpillwayPage from '@spillway-cms/astro/components/SpillwayPage.astro';
|
|
16
|
+
export const getStaticPaths = () => spillwayStaticPaths(client);
|
|
17
|
+
---
|
|
18
|
+
<SpillwayPage path={Astro.props.path ?? pathFromParams(Astro.params)} />
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- `spillway:client` is the configured `@spillway-cms/client` (server only; add `/// <reference types="@spillway-cms/astro/client" />` for types).
|
|
22
|
+
- `<SpillwayPage>` and `<SpillwayStyles>` render pages with the core block library through React server rendering; `@spillway-cms/astro/render` has `renderPage`, `renderHead`, `renderMenu`, `pageStyles` for custom layouts.
|
|
23
|
+
- `@spillway-cms/astro/server`: `createRevalidateEndpoint({ secret, onEvent: deployHook(url) })` answers Spillway's signed publish webhooks.
|
|
24
|
+
|
|
25
|
+
Peer dependencies: `astro`, `react`, `react-dom`, `@spillway-cms/blocks`, `@puckeditor/core`. Example: `examples/astro-site`. Full documentation: `docs/adapters.md` in the Spillway repository.
|
package/client.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the `spillway:client` virtual module the integration registers.
|
|
3
|
+
* Add `/// <reference types="@spillway-cms/astro/client" />` to your `src/env.d.ts`.
|
|
4
|
+
*/
|
|
5
|
+
declare module 'spillway:client' {
|
|
6
|
+
import type { ClientOptions, SpillwayClient } from '@spillway-cms/client';
|
|
7
|
+
/** The options the integration was given (plus SPILLWAY_URL / SPILLWAY_TOKEN from the environment). */
|
|
8
|
+
export const options: ClientOptions;
|
|
9
|
+
/** The configured client. Server only: never import it from a client-side script, it may hold the API token. */
|
|
10
|
+
export const client: SpillwayClient;
|
|
11
|
+
export default client;
|
|
12
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* <SpillwayPage path="/about" />: fetches the page from Spillway and renders its blocks with the core block library.
|
|
4
|
+
* Renders the article template for types without a layout. With `styles` (default) the theme and block CSS are
|
|
5
|
+
* inlined ahead of the page; set it to false when <SpillwayStyles /> is in your layout already.
|
|
6
|
+
*
|
|
7
|
+
* Astro streams the response, so a status set from inside a component arrives too late when a layout has already
|
|
8
|
+
* started the document. To answer a real 404 (or to read the SEO data in your layout), fetch in the page's frontmatter
|
|
9
|
+
* with `client.render(path)` and pass the result as `page`; this component then only renders it.
|
|
10
|
+
*/
|
|
11
|
+
import { client as defaultClient } from 'spillway:client';
|
|
12
|
+
import { SpillwayError, previewFromRequest, type PreviewState, type PublicRender, type SpillwayClient } from '@spillway-cms/client';
|
|
13
|
+
import { pageStyles, renderPage, type RenderPageOptions } from '@spillway-cms/astro/render';
|
|
14
|
+
|
|
15
|
+
interface Props extends RenderPageOptions {
|
|
16
|
+
/** The public path to fetch; ignored when `page` is given. */
|
|
17
|
+
path?: string;
|
|
18
|
+
/** An already fetched page (from `client.render` in the page's frontmatter). */
|
|
19
|
+
page?: PublicRender | null;
|
|
20
|
+
/** Query parameters for blocks that read them (exposed filters, `page`, `month`). */
|
|
21
|
+
params?: Record<string, string | number | undefined>;
|
|
22
|
+
/** A preview state, `null` for none (default), or `'auto'` to read `?release=&token=` (or `?workstream=`) and the preview cookie from the request (SSR pages). */
|
|
23
|
+
preview?: PreviewState | null | 'auto';
|
|
24
|
+
styles?: boolean;
|
|
25
|
+
client?: SpillwayClient;
|
|
26
|
+
/** Receives the fetched page, for layouts that want its SEO data or menus. */
|
|
27
|
+
onPage?: (page: PublicRender) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const { path = '/', params, styles = true, onPage, page: given, ...renderOptions } = Astro.props;
|
|
31
|
+
const client = Astro.props.client ?? defaultClient;
|
|
32
|
+
let previewState: PreviewState | null = null;
|
|
33
|
+
if (Astro.props.preview === 'auto') {
|
|
34
|
+
try {
|
|
35
|
+
previewState = previewFromRequest(Astro.request);
|
|
36
|
+
} catch {
|
|
37
|
+
previewState = null;
|
|
38
|
+
}
|
|
39
|
+
} else if (Astro.props.preview) previewState = Astro.props.preview;
|
|
40
|
+
|
|
41
|
+
let page: PublicRender | null = given ?? null;
|
|
42
|
+
if (given === undefined) {
|
|
43
|
+
try {
|
|
44
|
+
page = await client.render(path, { params, previewState });
|
|
45
|
+
} catch (err) {
|
|
46
|
+
if (!(err instanceof SpillwayError && err.status === 404)) throw err;
|
|
47
|
+
Astro.response.status = 404;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (page) onPage?.(page);
|
|
51
|
+
const html = page ? renderPage(page, renderOptions) : '';
|
|
52
|
+
const css = styles && page ? `<style>${pageStyles(page.site.themeCss)}</style>` : '';
|
|
53
|
+
---
|
|
54
|
+
{page ? <Fragment set:html={css + html} /> : <slot name="not-found"><div class="sw-page"><section class="sw-container"><h1>Page not found</h1></section></div></slot>}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* <SpillwayStyles /> in a layout's <head>: the block stylesheet, with the site's theme variables when `themeCss`
|
|
4
|
+
* (from `page.site.themeCss` or `client.site()`) is given.
|
|
5
|
+
*/
|
|
6
|
+
import { pageStyles } from '@spillway-cms/astro/render';
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
themeCss?: string;
|
|
10
|
+
}
|
|
11
|
+
const css = pageStyles(Astro.props.themeCss);
|
|
12
|
+
---
|
|
13
|
+
<Fragment set:html={`<style>${css}</style>`} />
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @spillway-cms/astro: the Astro integration for Spillway CMS.
|
|
3
|
+
*
|
|
4
|
+
* `spillway({ baseUrl, token })` registers the `spillway:client` virtual module (a configured `@spillway-cms/client`), the
|
|
5
|
+
* `<SpillwayPage>` and `<SpillwayStyles>` components render pages with the core block library, `spillwayStaticPaths`
|
|
6
|
+
* feeds `getStaticPaths`, and `createRevalidateEndpoint` (in `@spillway-cms/astro/server`) answers Spillway's publish
|
|
7
|
+
* webhooks. docs/adapters.md explains the setup.
|
|
8
|
+
*/
|
|
9
|
+
import type { ClientOptions, PublicPath, SpillwayClient } from '@spillway-cms/client';
|
|
10
|
+
export { createClient, previewFromRequest, previewCookieValue, parsePreviewCookie, SpillwayError, SpillwayClient, PREVIEW_COOKIE } from '@spillway-cms/client';
|
|
11
|
+
export type { ClientOptions, PreviewState, PublicRender, PublicSite, PublicMenu, PublicPath, Entity, CollectionItem, Paginated } from '@spillway-cms/client';
|
|
12
|
+
export interface SpillwayIntegrationOptions extends Partial<Omit<ClientOptions, 'fetch' | 'requestInit'>> {
|
|
13
|
+
/** The Spillway origin; falls back to `SPILLWAY_URL` in the environment at runtime. */
|
|
14
|
+
baseUrl?: string;
|
|
15
|
+
/** An API token for previews and multisite; falls back to `SPILLWAY_TOKEN`. Server only. */
|
|
16
|
+
token?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare const VIRTUAL_MODULE_ID = "spillway:client";
|
|
19
|
+
/** The source of the virtual module: the options as given, with the environment filling in what is missing at runtime. */
|
|
20
|
+
export declare function virtualModuleSource(options: SpillwayIntegrationOptions): string;
|
|
21
|
+
/** A Vite plugin that serves `spillway:client`; the integration adds it, and a plain Vite project can use it directly. */
|
|
22
|
+
export declare function spillwayVitePlugin(options?: SpillwayIntegrationOptions): {
|
|
23
|
+
name: string;
|
|
24
|
+
resolveId(id: string): string | undefined;
|
|
25
|
+
load(id: string): string | undefined;
|
|
26
|
+
};
|
|
27
|
+
/** The part of Astro's integration API this package uses, declared here so Astro itself is not a dependency. */
|
|
28
|
+
export interface AstroIntegrationLike {
|
|
29
|
+
name: string;
|
|
30
|
+
hooks: {
|
|
31
|
+
'astro:config:setup'?: (ctx: {
|
|
32
|
+
updateConfig(config: {
|
|
33
|
+
vite?: {
|
|
34
|
+
plugins?: unknown[];
|
|
35
|
+
};
|
|
36
|
+
}): unknown;
|
|
37
|
+
logger?: {
|
|
38
|
+
info(msg: string): void;
|
|
39
|
+
};
|
|
40
|
+
}) => void | Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** `integrations: [spillway({ baseUrl: 'https://cms.example.org' })]` in astro.config. */
|
|
44
|
+
export declare function spillway(options?: SpillwayIntegrationOptions): AstroIntegrationLike;
|
|
45
|
+
export interface StaticPathsOptions {
|
|
46
|
+
/** The rest parameter of the page, e.g. `path` for `src/pages/[...path].astro`. */
|
|
47
|
+
param?: string;
|
|
48
|
+
type?: string;
|
|
49
|
+
locale?: string;
|
|
50
|
+
/** Called when Spillway cannot be reached at build time; by default the build fails. Return the paths to use instead. */
|
|
51
|
+
onError?: (err: unknown) => StaticPath[];
|
|
52
|
+
}
|
|
53
|
+
export interface StaticPath {
|
|
54
|
+
params: Record<string, string | undefined>;
|
|
55
|
+
props: {
|
|
56
|
+
path: string;
|
|
57
|
+
entry: PublicPath | null;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* `getStaticPaths` for a rest route: every published path as `{ params: { path: 'blog/hello' }, props: { path: '/blog/hello' } }`
|
|
62
|
+
* (`params.path` is undefined for the home page, as Astro expects).
|
|
63
|
+
*/
|
|
64
|
+
export declare function spillwayStaticPaths(client: SpillwayClient, opts?: StaticPathsOptions): Promise<StaticPath[]>;
|
|
65
|
+
/** The public path for a rest route's params (`{ path: 'blog/hello' }` → `/blog/hello`, undefined → `/`). */
|
|
66
|
+
export declare function pathFromParams(params: Record<string, string | undefined>, param?: string): string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export { createClient, previewFromRequest, previewCookieValue, parsePreviewCookie, SpillwayError, SpillwayClient, PREVIEW_COOKIE } from '@spillway-cms/client';
|
|
2
|
+
export const VIRTUAL_MODULE_ID = 'spillway:client';
|
|
3
|
+
const RESOLVED_ID = '\0' + VIRTUAL_MODULE_ID;
|
|
4
|
+
/** The source of the virtual module: the options as given, with the environment filling in what is missing at runtime. */
|
|
5
|
+
export function virtualModuleSource(options) {
|
|
6
|
+
const { baseUrl, token, ...rest } = options;
|
|
7
|
+
return [
|
|
8
|
+
`import { createClient } from '@spillway-cms/client';`,
|
|
9
|
+
`const env = typeof process !== 'undefined' && process.env ? process.env : {};`,
|
|
10
|
+
`export const options = { ...${JSON.stringify(rest)}, baseUrl: ${JSON.stringify(baseUrl ?? null)} ?? env.SPILLWAY_URL ?? 'http://localhost:3000', token: ${JSON.stringify(token ?? null)} ?? env.SPILLWAY_TOKEN ?? undefined };`,
|
|
11
|
+
`export const client = createClient(options);`,
|
|
12
|
+
`export default client;`,
|
|
13
|
+
'',
|
|
14
|
+
].join('\n');
|
|
15
|
+
}
|
|
16
|
+
/** A Vite plugin that serves `spillway:client`; the integration adds it, and a plain Vite project can use it directly. */
|
|
17
|
+
export function spillwayVitePlugin(options = {}) {
|
|
18
|
+
return {
|
|
19
|
+
name: '@spillway-cms/astro:client',
|
|
20
|
+
resolveId(id) {
|
|
21
|
+
return id === VIRTUAL_MODULE_ID ? RESOLVED_ID : undefined;
|
|
22
|
+
},
|
|
23
|
+
load(id) {
|
|
24
|
+
return id === RESOLVED_ID ? virtualModuleSource(options) : undefined;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** `integrations: [spillway({ baseUrl: 'https://cms.example.org' })]` in astro.config. */
|
|
29
|
+
export function spillway(options = {}) {
|
|
30
|
+
return {
|
|
31
|
+
name: '@spillway-cms/astro',
|
|
32
|
+
hooks: {
|
|
33
|
+
'astro:config:setup'({ updateConfig, logger }) {
|
|
34
|
+
updateConfig({ vite: { plugins: [spillwayVitePlugin(options)] } });
|
|
35
|
+
logger?.info(`spillway:client → ${options.baseUrl ?? '$SPILLWAY_URL'}`);
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `getStaticPaths` for a rest route: every published path as `{ params: { path: 'blog/hello' }, props: { path: '/blog/hello' } }`
|
|
42
|
+
* (`params.path` is undefined for the home page, as Astro expects).
|
|
43
|
+
*/
|
|
44
|
+
export async function spillwayStaticPaths(client, opts = {}) {
|
|
45
|
+
const param = opts.param ?? 'path';
|
|
46
|
+
try {
|
|
47
|
+
const paths = await client.paths({ type: opts.type, locale: opts.locale });
|
|
48
|
+
return paths.map((entry) => ({ params: { [param]: entry.path === '/' ? undefined : entry.path.slice(1) }, props: { path: entry.path, entry } }));
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (opts.onError)
|
|
52
|
+
return opts.onError(err);
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The public path for a rest route's params (`{ path: 'blog/hello' }` → `/blog/hello`, undefined → `/`). */
|
|
57
|
+
export function pathFromParams(params, param = 'path') {
|
|
58
|
+
const v = params[param];
|
|
59
|
+
return v ? `/${v.replace(/^\/+/, '')}` : '/';
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAUA,OAAO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,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;AAC/C,CAAC"}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server rendering of a Spillway page to HTML with the core block library: Puck's RSC `Render` through
|
|
3
|
+
* `react-dom/server`, the same way Spillway's own server renders. Pure functions, no Astro runtime needed,
|
|
4
|
+
* so they also serve any other string-rendering host (and the unit tests).
|
|
5
|
+
*/
|
|
6
|
+
import { type ReactNode } from 'react';
|
|
7
|
+
import type { Config } from '@puckeditor/core';
|
|
8
|
+
import type { PublicRender, PuckData } from '@spillway-cms/client';
|
|
9
|
+
/** The core block config, exported so a site can extend it with its own blocks. */
|
|
10
|
+
export declare const config: Config;
|
|
11
|
+
export interface RenderPageOptions {
|
|
12
|
+
/** Replaces the core block config. */
|
|
13
|
+
config?: Config;
|
|
14
|
+
/**
|
|
15
|
+
* Where the Form block posts. The block's `action` is `/api/forms/<key>/submit`, relative; a static site has nothing
|
|
16
|
+
* there, so by default it is made absolute to the Spillway origin. Pass `''` to keep it relative (behind a proxy).
|
|
17
|
+
*/
|
|
18
|
+
formsOrigin?: string;
|
|
19
|
+
/** Renders article types (no layout field, or an inline one); defaults to `renderArticle`. */
|
|
20
|
+
article?: (page: PublicRender) => string;
|
|
21
|
+
/** The wrapper element's class; `sw-page` like the server (the block CSS expects it). */
|
|
22
|
+
className?: string;
|
|
23
|
+
}
|
|
24
|
+
/** A Puck document to HTML. */
|
|
25
|
+
export declare function renderBlocks(data: PuckData, cfg?: Config): string;
|
|
26
|
+
/** A whole page to HTML: the blocks (or the article template) inside `<div class="sw-page">`, form actions made absolute. */
|
|
27
|
+
export declare function renderPage(page: PublicRender, opts?: RenderPageOptions): string;
|
|
28
|
+
/**
|
|
29
|
+
* The body with the blocks placed in regions around it (Structure → Placements, `page.regions`), in the server's markup: above
|
|
30
|
+
* and below full width, the sidebars beside it with the main content first. The block stylesheet lays them out.
|
|
31
|
+
*/
|
|
32
|
+
export declare function renderRegions(page: Pick<PublicRender, 'regions'>, body: string, cfg?: Config): string;
|
|
33
|
+
/** The theme variables (when known) and the block stylesheet; inline it once per page. */
|
|
34
|
+
export declare function pageStyles(themeCss?: string): string;
|
|
35
|
+
/** The type's layout field when its blocks show inside the article (placement 'inline'). */
|
|
36
|
+
export declare function inlineLayoutField(page: Pick<PublicRender, 'type'>): PublicRender['type']['fields'][number] | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* A plain article: title, date, hero image, rich text and text fields in order. An inline layout field's blocks go full width
|
|
39
|
+
* where the field sits, between two article columns, like the server renders it.
|
|
40
|
+
*/
|
|
41
|
+
export declare function renderArticle(page: PublicRender, cfg?: Config): string;
|
|
42
|
+
/** `<head>` markup from the page's SEO data: title, description, canonical, hreflang alternates, Open Graph, robots, JSON-LD. */
|
|
43
|
+
export declare function renderHead(page: PublicRender): string;
|
|
44
|
+
/** Resolved menu items as a nested `<ul>` (what the server shell does for `main` and `footer`). */
|
|
45
|
+
export declare function renderMenu(items: PublicRender['menus']['main'], className?: string): string;
|
|
46
|
+
/** React elements for hosts that render React themselves (for example `@astrojs/react` islands or a custom SSR). */
|
|
47
|
+
export declare function pageElement(page: PublicRender, cfg?: Config): ReactNode;
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server rendering of a Spillway page to HTML with the core block library: Puck's RSC `Render` through
|
|
3
|
+
* `react-dom/server`, the same way Spillway's own server renders. Pure functions, no Astro runtime needed,
|
|
4
|
+
* so they also serve any other string-rendering host (and the unit tests).
|
|
5
|
+
*/
|
|
6
|
+
import { createElement } from 'react';
|
|
7
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
8
|
+
import { Render } from '@puckeditor/core/rsc';
|
|
9
|
+
import { blockCss, config as coreConfig } from '@spillway-cms/blocks';
|
|
10
|
+
/** The core block config, exported so a site can extend it with its own blocks. */
|
|
11
|
+
export const config = coreConfig;
|
|
12
|
+
/** A Puck document to HTML. */
|
|
13
|
+
export function renderBlocks(data, cfg = config) {
|
|
14
|
+
return renderToStaticMarkup(createElement(Render, { config: cfg, data: data }));
|
|
15
|
+
}
|
|
16
|
+
/** A whole page to HTML: the blocks (or the article template) inside `<div class="sw-page">`, form actions made absolute. */
|
|
17
|
+
export function renderPage(page, opts = {}) {
|
|
18
|
+
const body = page.data && !inlineLayoutField(page) ? renderBlocks(page.data, opts.config) : opts.article ? opts.article(page) : renderArticle(page, opts.config);
|
|
19
|
+
const html = `<div class="${opts.className ?? 'sw-page'}">${renderRegions(page, body, opts.config)}</div>`;
|
|
20
|
+
const origin = opts.formsOrigin ?? page.site.origin;
|
|
21
|
+
return origin ? html.replace(/ action="\/api\/forms\//g, ` action="${origin}/api/forms/`) : html;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The body with the blocks placed in regions around it (Structure → Placements, `page.regions`), in the server's markup: above
|
|
25
|
+
* and below full width, the sidebars beside it with the main content first. The block stylesheet lays them out.
|
|
26
|
+
*/
|
|
27
|
+
export function renderRegions(page, body, cfg = config) {
|
|
28
|
+
const r = page.regions ?? {};
|
|
29
|
+
const block = (k) => (r[k]?.content.length ? renderBlocks(r[k], cfg) : '');
|
|
30
|
+
const before = block('before_content');
|
|
31
|
+
const left = block('sidebar_left');
|
|
32
|
+
const right = block('sidebar_right');
|
|
33
|
+
const after = block('after_content');
|
|
34
|
+
if (!before && !left && !right && !after)
|
|
35
|
+
return body;
|
|
36
|
+
const sides = left || right;
|
|
37
|
+
const classes = ['sw-with-sidebar', left ? 'sw-with-sidebar--left' : '', right ? 'sw-with-sidebar--right' : ''].filter(Boolean).join(' ');
|
|
38
|
+
return [
|
|
39
|
+
before ? `<div class="sw-region sw-region--before-content">${before}</div>` : '',
|
|
40
|
+
sides
|
|
41
|
+
? `<div class="${classes}"><div class="sw-region-main">${body}</div>${left ? `<aside class="sw-region sw-region--sidebar-left" aria-label="Sidebar">${left}</aside>` : ''}${right ? `<aside class="sw-region sw-region--sidebar-right" aria-label="${left ? 'More' : 'Sidebar'}">${right}</aside>` : ''}</div>`
|
|
42
|
+
: body,
|
|
43
|
+
after ? `<div class="sw-region sw-region--after-content">${after}</div>` : '',
|
|
44
|
+
].join('');
|
|
45
|
+
}
|
|
46
|
+
/** The theme variables (when known) and the block stylesheet; inline it once per page. */
|
|
47
|
+
export function pageStyles(themeCss) {
|
|
48
|
+
return `${themeCss ?? ''}\n${blockCss}`;
|
|
49
|
+
}
|
|
50
|
+
function esc(s) {
|
|
51
|
+
return s.replace(/[&<>"]/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[ch]);
|
|
52
|
+
}
|
|
53
|
+
/** The type's layout field when its blocks show inside the article (placement 'inline'). */
|
|
54
|
+
export function inlineLayoutField(page) {
|
|
55
|
+
const f = page.type.fields.find((x) => x.type === 'layout');
|
|
56
|
+
return f?.placement === 'inline' ? f : undefined;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A plain article: title, date, hero image, rich text and text fields in order. An inline layout field's blocks go full width
|
|
60
|
+
* where the field sits, between two article columns, like the server renders it.
|
|
61
|
+
*/
|
|
62
|
+
export function renderArticle(page, cfg = config) {
|
|
63
|
+
const { entity, type, richHtml, media } = page;
|
|
64
|
+
const inline = inlineLayoutField(page);
|
|
65
|
+
const blocks = inline && page.data?.content.length ? renderBlocks(page.data, cfg) : null;
|
|
66
|
+
const data = entity.data;
|
|
67
|
+
const dateField = type.fields.find((f) => f.type === 'date');
|
|
68
|
+
const date = dateField && typeof data[dateField.key] === 'string' ? data[dateField.key] : entity.publishedAt;
|
|
69
|
+
const hero = type.fields.find((f) => f.type === 'media' && typeof data[f.key] === 'string' && media[data[f.key]]);
|
|
70
|
+
const heroItem = hero ? media[data[hero.key]] : undefined;
|
|
71
|
+
const parts = [`<header class="sw-article-header"><h1>${esc(entity.title)}</h1>`];
|
|
72
|
+
if (date)
|
|
73
|
+
parts.push(`<time datetime="${esc(date)}">${esc(new Date(date).toLocaleDateString(entity.locale, { year: 'numeric', month: 'long', day: 'numeric' }))}</time>`);
|
|
74
|
+
parts.push('</header>');
|
|
75
|
+
if (heroItem)
|
|
76
|
+
parts.push(`<img class="sw-article-hero" src="${esc(heroItem.url)}" alt="${esc(heroItem.alt)}"${heroItem.width ? ` width="${heroItem.width}"` : ''}${heroItem.height ? ` height="${heroItem.height}"` : ''}>`);
|
|
77
|
+
const after = [];
|
|
78
|
+
let out = parts;
|
|
79
|
+
for (const f of type.fields) {
|
|
80
|
+
if (blocks && f.key === inline.key) {
|
|
81
|
+
out = after;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (f.key === type.titleField || f.key === hero?.key || f.type === 'date' || f.type === 'media' || f.type === 'layout')
|
|
85
|
+
continue;
|
|
86
|
+
const value = data[f.key];
|
|
87
|
+
if (value === undefined || value === null || value === '')
|
|
88
|
+
continue;
|
|
89
|
+
if (f.type === 'richtext')
|
|
90
|
+
out.push(`<div class="sw-richtext sw-prose">${richHtml[f.key] ?? esc(String(value))}</div>`);
|
|
91
|
+
else if ((f.type === 'textarea' || f.type === 'text') && typeof value === 'string')
|
|
92
|
+
out.push(`<p>${esc(value)}</p>`);
|
|
93
|
+
}
|
|
94
|
+
if (!blocks)
|
|
95
|
+
return `<article class="sw-container sw-article">${parts.join('')}</article>`;
|
|
96
|
+
return `<article class="sw-article-with-blocks"><div class="sw-container sw-article">${parts.join('')}</div>${blocks}${after.length ? `<div class="sw-container sw-article sw-article--after">${after.join('')}</div>` : ''}</article>`;
|
|
97
|
+
}
|
|
98
|
+
/** `<head>` markup from the page's SEO data: title, description, canonical, hreflang alternates, Open Graph, robots, JSON-LD. */
|
|
99
|
+
export function renderHead(page) {
|
|
100
|
+
const { seo } = page;
|
|
101
|
+
const out = [`<title>${esc(seo.title)}</title>`];
|
|
102
|
+
if (seo.description)
|
|
103
|
+
out.push(`<meta name="description" content="${esc(seo.description)}">`);
|
|
104
|
+
if (seo.canonical)
|
|
105
|
+
out.push(`<link rel="canonical" href="${esc(seo.canonical)}">`);
|
|
106
|
+
for (const a of seo.alternates)
|
|
107
|
+
out.push(`<link rel="alternate" hreflang="${esc(a.hreflang)}" href="${esc(a.href)}">`);
|
|
108
|
+
out.push(`<meta property="og:title" content="${esc(seo.title)}">`, `<meta property="og:type" content="website">`, `<meta property="og:site_name" content="${esc(page.site.name)}">`);
|
|
109
|
+
if (seo.description)
|
|
110
|
+
out.push(`<meta property="og:description" content="${esc(seo.description)}">`);
|
|
111
|
+
if (seo.canonical)
|
|
112
|
+
out.push(`<meta property="og:url" content="${esc(seo.canonical)}">`);
|
|
113
|
+
if (seo.ogImage)
|
|
114
|
+
out.push(`<meta property="og:image" content="${esc(seo.ogImage)}">`);
|
|
115
|
+
if (seo.noindex)
|
|
116
|
+
out.push('<meta name="robots" content="noindex, nofollow">');
|
|
117
|
+
for (const ld of seo.jsonLd)
|
|
118
|
+
out.push(`<script type="application/ld+json">${JSON.stringify(ld).replace(/</g, '\\u003c')}</script>`);
|
|
119
|
+
return out.join('\n');
|
|
120
|
+
}
|
|
121
|
+
/** Resolved menu items as a nested `<ul>` (what the server shell does for `main` and `footer`). */
|
|
122
|
+
export function renderMenu(items, className = 'sw-nav') {
|
|
123
|
+
if (!items?.length)
|
|
124
|
+
return '';
|
|
125
|
+
const li = (list) => list
|
|
126
|
+
.map((i) => `<li${i.current ? ' class="is-current"' : ''}><a href="${esc(i.href)}"${i.current ? ' aria-current="page"' : ''}${i.newTab ? ' target="_blank" rel="noopener"' : ''}>${esc(i.label)}</a>${i.children?.length ? `<ul>${li(i.children)}</ul>` : ''}</li>`)
|
|
127
|
+
.join('');
|
|
128
|
+
return `<ul class="${esc(className)}">${li(items)}</ul>`;
|
|
129
|
+
}
|
|
130
|
+
/** React elements for hosts that render React themselves (for example `@astrojs/react` islands or a custom SSR). */
|
|
131
|
+
export function pageElement(page, cfg = config) {
|
|
132
|
+
return page.data && !inlineLayoutField(page) ? createElement(Render, { config: cfg, data: page.data }) : createElement('div', { dangerouslySetInnerHTML: { __html: renderArticle(page, cfg) } });
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,aAAa,EAAkB,MAAM,OAAO,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAExD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGtE,mFAAmF;AACnF,MAAM,CAAC,MAAM,MAAM,GAAW,UAAU,CAAC;AAgBzC,+BAA+B;AAC/B,MAAM,UAAU,YAAY,CAAC,IAAc,EAAE,MAAc,MAAM;IAC/D,OAAO,oBAAoB,CAAC,aAAa,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,IAAuB,EAAE,CAAC,CAAC,CAAC;AACrG,CAAC;AAED,6HAA6H;AAC7H,MAAM,UAAU,UAAU,CAAC,IAAkB,EAAE,OAA0B,EAAE;IACzE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACjK,MAAM,IAAI,GAAG,eAAe,IAAI,CAAC,SAAS,IAAI,SAAS,KAAK,aAAa,CAAC,IAAI,EAAE,IAA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+B,EAAU,EAAE,CACrD,IAAI;SACD,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,EAAE,aAAa,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,iCAAiC,CAAC,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC;SACnQ,IAAI,CAAC,EAAE,CAAC,CAAC;IACd,OAAO,cAAc,GAAG,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC;AAC3D,CAAC;AAED,oHAAoH;AACpH,MAAM,UAAU,WAAW,CAAC,IAAkB,EAAE,MAAc,MAAM;IAClE,OAAO,IAAI,CAAC,IAAI,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,IAAuB,EAAE,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,EAAE,EAAE,uBAAuB,EAAE,EAAE,MAAM,EAAE,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;AACtN,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side helpers for SSR and hybrid Astro sites: the signed revalidation endpoint Spillway's publish webhooks
|
|
3
|
+
* call, and the deploy-hook helper for static sites that rebuild on publish.
|
|
4
|
+
*/
|
|
5
|
+
import { affectedByWebhook, type WebhookPayload } from '@spillway-cms/client/webhook';
|
|
6
|
+
export { readWebhook, verifyWebhookSignature, affectedByWebhook, WebhookError } from '@spillway-cms/client/webhook';
|
|
7
|
+
export type { WebhookPayload } from '@spillway-cms/client/webhook';
|
|
8
|
+
export type Affected = ReturnType<typeof affectedByWebhook>;
|
|
9
|
+
export interface RevalidateEndpointOptions {
|
|
10
|
+
/** The secret of the webhook rule in Spillway (Settings → Notifications). */
|
|
11
|
+
secret: string;
|
|
12
|
+
/** What to do with a verified event: trigger a rebuild, purge a CDN by `affected.tags`, warm a cache. */
|
|
13
|
+
onEvent: (payload: WebhookPayload, affected: Affected) => void | Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/** The shape Astro gives an endpoint (`APIRoute`): only `request` is needed here. */
|
|
16
|
+
export type EndpointHandler = (context: {
|
|
17
|
+
request: Request;
|
|
18
|
+
}) => Promise<Response>;
|
|
19
|
+
/**
|
|
20
|
+
* `export const POST = createRevalidateEndpoint({ secret, onEvent: deployHook(url) })` in `src/pages/api/revalidate.ts`.
|
|
21
|
+
* Verifies the HMAC-SHA256 signature over the raw body, ignores test sends, runs `onEvent` and answers JSON.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createRevalidateEndpoint(opts: RevalidateEndpointOptions): EndpointHandler;
|
|
24
|
+
/** An `onEvent` that POSTs to a deploy hook (Netlify, Vercel, Cloudflare Pages, GitHub Actions) so a static site rebuilds. */
|
|
25
|
+
export declare function deployHook(url: string, init?: RequestInit): RevalidateEndpointOptions['onEvent'];
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side helpers for SSR and hybrid Astro sites: the signed revalidation endpoint Spillway's publish webhooks
|
|
3
|
+
* call, and the deploy-hook helper for static sites that rebuild on publish.
|
|
4
|
+
*/
|
|
5
|
+
import { WebhookError, affectedByWebhook, readWebhook } from '@spillway-cms/client/webhook';
|
|
6
|
+
export { readWebhook, verifyWebhookSignature, affectedByWebhook, WebhookError } from '@spillway-cms/client/webhook';
|
|
7
|
+
/**
|
|
8
|
+
* `export const POST = createRevalidateEndpoint({ secret, onEvent: deployHook(url) })` in `src/pages/api/revalidate.ts`.
|
|
9
|
+
* Verifies the HMAC-SHA256 signature over the raw body, ignores test sends, runs `onEvent` and answers JSON.
|
|
10
|
+
*/
|
|
11
|
+
export function createRevalidateEndpoint(opts) {
|
|
12
|
+
if (!opts.secret)
|
|
13
|
+
throw new TypeError('createRevalidateEndpoint: secret is required');
|
|
14
|
+
return async ({ request }) => {
|
|
15
|
+
let payload;
|
|
16
|
+
try {
|
|
17
|
+
payload = await readWebhook(request, opts.secret);
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
if (err instanceof WebhookError)
|
|
21
|
+
return Response.json({ error: err.message }, { status: err.status });
|
|
22
|
+
throw err;
|
|
23
|
+
}
|
|
24
|
+
const affected = affectedByWebhook(payload);
|
|
25
|
+
if (affected.test)
|
|
26
|
+
return Response.json({ ok: true, test: true });
|
|
27
|
+
await opts.onEvent(payload, affected);
|
|
28
|
+
return Response.json({ ok: true, event: payload.event, paths: affected.paths, tags: affected.tags });
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** An `onEvent` that POSTs to a deploy hook (Netlify, Vercel, Cloudflare Pages, GitHub Actions) so a static site rebuilds. */
|
|
32
|
+
export function deployHook(url, init = {}) {
|
|
33
|
+
return async (payload) => {
|
|
34
|
+
const res = await fetch(url, { method: 'POST', ...init, headers: { 'content-type': 'application/json', ...init.headers }, body: JSON.stringify({ source: 'spillway', event: payload.event, at: payload.at }) });
|
|
35
|
+
if (!res.ok)
|
|
36
|
+
throw new Error(`deploy hook responded ${res.status}`);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":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vG,CAAC,CAAC;AACJ,CAAC;AAED,8HAA8H;AAC9H,MAAM,UAAU,UAAU,CAAC,GAAW,EAAE,OAAoB,EAAE;IAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,GAAI,IAAI,CAAC,OAA8C,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;QACxP,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;IACtE,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spillway-cms/astro",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Astro integration for Spillway CMS: a spillway:client virtual module, a <SpillwayPage> component that renders pages with the core blocks, getStaticPaths from the published paths and a signed revalidation endpoint",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kwall-llc/spillway-cms.git",
|
|
9
|
+
"directory": "packages/astro"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/kwall-llc/spillway-cms/tree/main/packages/astro#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/kwall-llc/spillway-cms/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./render": {
|
|
25
|
+
"types": "./dist/render.d.ts",
|
|
26
|
+
"import": "./dist/render.js",
|
|
27
|
+
"default": "./dist/render.js"
|
|
28
|
+
},
|
|
29
|
+
"./server": {
|
|
30
|
+
"types": "./dist/server.d.ts",
|
|
31
|
+
"import": "./dist/server.js",
|
|
32
|
+
"default": "./dist/server.js"
|
|
33
|
+
},
|
|
34
|
+
"./client": {
|
|
35
|
+
"types": "./client.d.ts"
|
|
36
|
+
},
|
|
37
|
+
"./components/SpillwayPage.astro": "./components/SpillwayPage.astro",
|
|
38
|
+
"./components/SpillwayStyles.astro": "./components/SpillwayStyles.astro"
|
|
39
|
+
},
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"files": [
|
|
42
|
+
"dist",
|
|
43
|
+
"components",
|
|
44
|
+
"client.d.ts",
|
|
45
|
+
"README.md",
|
|
46
|
+
"LICENSE"
|
|
47
|
+
],
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@spillway-cms/client": "0.1.1",
|
|
50
|
+
"@spillway-cms/shared": "0.1.1"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@puckeditor/core": "^0.24.0",
|
|
54
|
+
"astro": ">=5.0.0",
|
|
55
|
+
"react": "^19.0.0",
|
|
56
|
+
"react-dom": "^19.0.0",
|
|
57
|
+
"@spillway-cms/blocks": "^0.1.1"
|
|
58
|
+
},
|
|
59
|
+
"peerDependenciesMeta": {
|
|
60
|
+
"astro": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@puckeditor/core": "^0.24.0",
|
|
66
|
+
"@types/node": "^22.0.0",
|
|
67
|
+
"@types/react": "^19.0.0",
|
|
68
|
+
"@types/react-dom": "^19.0.0",
|
|
69
|
+
"react": "^19.0.0",
|
|
70
|
+
"react-dom": "^19.0.0",
|
|
71
|
+
"typescript": "^5.9.0",
|
|
72
|
+
"vitest": "^3.0.0",
|
|
73
|
+
"@spillway-cms/blocks": "0.1.1"
|
|
74
|
+
},
|
|
75
|
+
"scripts": {
|
|
76
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
77
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit",
|
|
78
|
+
"test": "vitest run"
|
|
79
|
+
}
|
|
6
80
|
}
|