@conloca/astro-cms 0.1.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 +305 -0
- package/dist/cms-handler.d.mts +23 -0
- package/dist/cms-handler.d.mts.map +1 -0
- package/dist/cms-handler.mjs +221 -0
- package/dist/cms-handler.mjs.map +1 -0
- package/dist/collections.d.mts +36 -0
- package/dist/collections.d.mts.map +1 -0
- package/dist/collections.mjs +56 -0
- package/dist/collections.mjs.map +1 -0
- package/dist/components.d.mts +46 -0
- package/dist/components.d.mts.map +1 -0
- package/dist/components.mjs +167 -0
- package/dist/components.mjs.map +1 -0
- package/dist/hydration-script-CKamSyIx.d.mts +461 -0
- package/dist/hydration-script-CKamSyIx.d.mts.map +1 -0
- package/dist/hydration.d.mts +3 -0
- package/dist/hydration.mjs +3 -0
- package/dist/index.d.mts +148 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +770 -0
- package/dist/index.mjs.map +1 -0
- package/dist/lib/hydration-script.d.mts +2 -0
- package/dist/lib/hydration-script.mjs +85 -0
- package/dist/lib/hydration-script.mjs.map +1 -0
- package/dist/loader-C6JF2toW.mjs +61 -0
- package/dist/loader-C6JF2toW.mjs.map +1 -0
- package/dist/serialize-props-BryTdVgJ.mjs +73 -0
- package/dist/serialize-props-BryTdVgJ.mjs.map +1 -0
- package/dist/withHydration-C6l6XnF2.d.mts +52 -0
- package/dist/withHydration-C6l6XnF2.d.mts.map +1 -0
- package/dist/withHydration-DAkXCF9z.mjs +45 -0
- package/dist/withHydration-DAkXCF9z.mjs.map +1 -0
- package/package.json +102 -0
- package/src/handlers/page-handler.astro +298 -0
- package/src/virtual-modules.d.ts +144 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { t as conlocaLoader } from "./loader-C6JF2toW.mjs";
|
|
2
|
+
import { blockMetaSchema, dataMetaSchema, pageMetaSchema } from "@conloca/content-api/schemas";
|
|
3
|
+
import { createContentAPI } from "@conloca/content-api/node";
|
|
4
|
+
import { defineCollection } from "astro:content";
|
|
5
|
+
|
|
6
|
+
//#region src/collections.ts
|
|
7
|
+
/**
|
|
8
|
+
* Creates Astro Content Collections by discovering from indexed content.
|
|
9
|
+
* Uses content-api to find collections that have content.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // src/content.config.ts
|
|
14
|
+
* import { createConlocaCollections } from '@conloca/astro-cms/collections';
|
|
15
|
+
*
|
|
16
|
+
* export const { collections } = await createConlocaCollections();
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
async function createConlocaCollections(options = {}) {
|
|
20
|
+
const { contentRoot = "./content", site = "default" } = options;
|
|
21
|
+
const api = await createContentAPI({ contentRoot });
|
|
22
|
+
const collections = {};
|
|
23
|
+
for (const name of api.blocks.collections) collections[name] = defineCollection({
|
|
24
|
+
loader: conlocaLoader({
|
|
25
|
+
collection: name,
|
|
26
|
+
contentRoot,
|
|
27
|
+
site,
|
|
28
|
+
kind: "block"
|
|
29
|
+
}),
|
|
30
|
+
schema: blockMetaSchema
|
|
31
|
+
});
|
|
32
|
+
for (const name of api.data.collections) collections[name] = defineCollection({
|
|
33
|
+
loader: conlocaLoader({
|
|
34
|
+
collection: name,
|
|
35
|
+
contentRoot,
|
|
36
|
+
site,
|
|
37
|
+
kind: "data"
|
|
38
|
+
}),
|
|
39
|
+
schema: dataMetaSchema
|
|
40
|
+
});
|
|
41
|
+
const siteCollections = api.getSite(site)?.collections ?? /* @__PURE__ */ new Set();
|
|
42
|
+
for (const name of siteCollections) collections[name] = defineCollection({
|
|
43
|
+
loader: conlocaLoader({
|
|
44
|
+
collection: name,
|
|
45
|
+
contentRoot,
|
|
46
|
+
site,
|
|
47
|
+
kind: "page"
|
|
48
|
+
}),
|
|
49
|
+
schema: pageMetaSchema.passthrough()
|
|
50
|
+
});
|
|
51
|
+
return { collections };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
export { createConlocaCollections };
|
|
56
|
+
//# sourceMappingURL=collections.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.mjs","names":["collections: Record<string, ReturnType<typeof defineCollection>>"],"sources":["../src/collections.ts"],"sourcesContent":["import { defineCollection } from 'astro:content';\nimport { createContentAPI } from '@conloca/content-api/node';\nimport { blockMetaSchema, dataMetaSchema, pageMetaSchema } from '@conloca/content-api/schemas';\nimport { conlocaLoader } from './loader';\n\n/**\n * Options for createConlocaCollections().\n */\nexport interface CreateConlocaCollectionsOptions {\n /** Path to content root directory. @default './content' */\n contentRoot?: string;\n /** Site name for multi-site setups. @default 'default' */\n site?: string;\n}\n\n/**\n * Return type of createConlocaCollections().\n */\nexport interface ConlocaCollectionsResult {\n /** Astro Content Collections definitions - export this as `collections` */\n collections: Record<string, ReturnType<typeof defineCollection>>;\n}\n\n/**\n * Creates Astro Content Collections by discovering from indexed content.\n * Uses content-api to find collections that have content.\n *\n * @example\n * ```typescript\n * // src/content.config.ts\n * import { createConlocaCollections } from '@conloca/astro-cms/collections';\n *\n * export const { collections } = await createConlocaCollections();\n * ```\n */\nexport async function createConlocaCollections(\n options: CreateConlocaCollectionsOptions = {},\n): Promise<ConlocaCollectionsResult> {\n const { contentRoot = './content', site = 'default' } = options;\n\n const api = await createContentAPI({ contentRoot });\n const collections: Record<string, ReturnType<typeof defineCollection>> = {};\n\n // Add block collections\n for (const name of api.blocks.collections) {\n collections[name] = defineCollection({\n loader: conlocaLoader({ collection: name, contentRoot, site, kind: 'block' }),\n schema: blockMetaSchema,\n });\n }\n\n // Add data collections\n for (const name of api.data.collections) {\n collections[name] = defineCollection({\n loader: conlocaLoader({ collection: name, contentRoot, site, kind: 'data' }),\n schema: dataMetaSchema,\n });\n }\n\n // Add page collections for this site\n const siteCollections = api.getSite(site)?.collections ?? new Set<string>();\n for (const name of siteCollections) {\n collections[name] = defineCollection({\n loader: conlocaLoader({ collection: name, contentRoot, site, kind: 'page' }),\n schema: pageMetaSchema.passthrough(),\n });\n }\n\n return { collections };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmCA,eAAsB,yBACpB,UAA2C,EAAE,EACV;CACnC,MAAM,EAAE,cAAc,aAAa,OAAO,cAAc;CAExD,MAAM,MAAM,MAAM,iBAAiB,EAAE,aAAa,CAAC;CACnD,MAAMA,cAAmE,EAAE;AAG3E,MAAK,MAAM,QAAQ,IAAI,OAAO,YAC5B,aAAY,QAAQ,iBAAiB;EACnC,QAAQ,cAAc;GAAE,YAAY;GAAM;GAAa;GAAM,MAAM;GAAS,CAAC;EAC7E,QAAQ;EACT,CAAC;AAIJ,MAAK,MAAM,QAAQ,IAAI,KAAK,YAC1B,aAAY,QAAQ,iBAAiB;EACnC,QAAQ,cAAc;GAAE,YAAY;GAAM;GAAa;GAAM,MAAM;GAAQ,CAAC;EAC5E,QAAQ;EACT,CAAC;CAIJ,MAAM,kBAAkB,IAAI,QAAQ,KAAK,EAAE,+BAAe,IAAI,KAAa;AAC3E,MAAK,MAAM,QAAQ,gBACjB,aAAY,QAAQ,iBAAiB;EACnC,QAAQ,cAAc;GAAE,YAAY;GAAM;GAAa;GAAM,MAAM;GAAQ,CAAC;EAC5E,QAAQ,eAAe,aAAa;EACrC,CAAC;AAGJ,QAAO,EAAE,aAAa"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Config, Data } from "@measured/puck";
|
|
2
|
+
import { ComponentType } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/components/RenderWithBlocks.d.ts
|
|
5
|
+
interface MDXComponent {
|
|
6
|
+
id: string;
|
|
7
|
+
title: string;
|
|
8
|
+
Component: ComponentType;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Factory function to create a PageRenderer with MDX components baked in via closure.
|
|
12
|
+
*
|
|
13
|
+
* This approach avoids dangerouslySetInnerHTML by keeping React components as components
|
|
14
|
+
* throughout the rendering pipeline. Components are captured in the closure and don't
|
|
15
|
+
* need to be passed as props (which Astro can't serialize).
|
|
16
|
+
*
|
|
17
|
+
* @param config - The Puck configuration with component definitions
|
|
18
|
+
* @param data - Puck page data containing component tree
|
|
19
|
+
* @param mdxComponents - Array of evaluated MDX components
|
|
20
|
+
* @returns A PageRenderer component with no props needed (components in closure)
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```astro
|
|
24
|
+
* ---
|
|
25
|
+
* import { createPageRendererWithBlocks } from '@conloca/astro-cms/components';
|
|
26
|
+
* import { evaluateMDXBlocks } from '@conloca/mdx/node';
|
|
27
|
+
* import { createContentAPI } from '@conloca/content-api/node';
|
|
28
|
+
* import config from '../puck.config';
|
|
29
|
+
*
|
|
30
|
+
* const api = await createContentAPI({ contentRoot: './content' });
|
|
31
|
+
* const mdxComponents = await evaluateMDXBlocks(api, 'en');
|
|
32
|
+
* const PageRenderer = createPageRendererWithBlocks(config, puckData, mdxComponents);
|
|
33
|
+
* ---
|
|
34
|
+
* <PageRenderer />
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* Benefits:
|
|
38
|
+
* - No dangerouslySetInnerHTML (safer, no XSS risk from HTML strings)
|
|
39
|
+
* - No string conversion round-trip (more efficient)
|
|
40
|
+
* - Type safe React components throughout
|
|
41
|
+
* - Normal React rendering with proper escaping
|
|
42
|
+
*/
|
|
43
|
+
declare function createPageRendererWithBlocks(config: Config, data: Data, mdxComponents: MDXComponent[]): ComponentType;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { createPageRendererWithBlocks };
|
|
46
|
+
//# sourceMappingURL=components.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.d.mts","names":[],"sources":["../src/components/RenderWithBlocks.tsx"],"sourcesContent":[],"mappings":";;;;UAMU,YAAA;;EAAA,KAAA,EAAA,MAAA;EA8HM,SAAA,EA3HH,aA2HG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAA,4BAAA,SAAqC,cAAc,qBAAqB,iBAAiB"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { i as isHydratable, r as hasHydratableComponents, t as serializeProps } from "./serialize-props-BryTdVgJ.mjs";
|
|
2
|
+
import { Render } from "@measured/puck";
|
|
3
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
4
|
+
|
|
5
|
+
//#region src/components/HydrationWrapper.tsx
|
|
6
|
+
/**
|
|
7
|
+
* Wraps hydratable components with data attributes for browser hydration.
|
|
8
|
+
*
|
|
9
|
+
* When strategy is 'none', renders children directly without wrapper overhead.
|
|
10
|
+
* Otherwise, wraps in a div with data-hydrate attributes that the browser
|
|
11
|
+
* hydration script uses to find and hydrate components.
|
|
12
|
+
*/
|
|
13
|
+
function HydrationWrapper({ componentName, strategy, props, children }) {
|
|
14
|
+
if (strategy === "none") return /* @__PURE__ */ jsx(Fragment, { children });
|
|
15
|
+
return /* @__PURE__ */ jsx("div", {
|
|
16
|
+
"data-hydrate": componentName,
|
|
17
|
+
"data-hydrate-strategy": strategy,
|
|
18
|
+
"data-props": serializeProps(props),
|
|
19
|
+
children
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/components/RenderWithHydration.tsx
|
|
25
|
+
/**
|
|
26
|
+
* Wrapper around Puck's Render component that adds hydration support.
|
|
27
|
+
*
|
|
28
|
+
* For each component with a hydration strategy configured, wraps the
|
|
29
|
+
* component's render output with HydrationWrapper to add data-hydrate
|
|
30
|
+
* attributes for browser-side hydration.
|
|
31
|
+
*
|
|
32
|
+
* Non-hydratable components render without any wrapper overhead.
|
|
33
|
+
*/
|
|
34
|
+
function RenderWithHydration({ config, data }) {
|
|
35
|
+
return /* @__PURE__ */ jsx(Render, {
|
|
36
|
+
config: {
|
|
37
|
+
...config,
|
|
38
|
+
components: Object.fromEntries(Object.entries(config.components).map(([name, componentConfig]) => {
|
|
39
|
+
const strategy = isHydratable(name, config);
|
|
40
|
+
if (strategy === "none") return [name, componentConfig];
|
|
41
|
+
const originalRender = componentConfig.render;
|
|
42
|
+
return [name, {
|
|
43
|
+
...componentConfig,
|
|
44
|
+
render: (props) => /* @__PURE__ */ jsx(HydrationWrapper, {
|
|
45
|
+
componentName: name,
|
|
46
|
+
strategy,
|
|
47
|
+
props,
|
|
48
|
+
children: originalRender(props)
|
|
49
|
+
})
|
|
50
|
+
}];
|
|
51
|
+
}))
|
|
52
|
+
},
|
|
53
|
+
data
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/components/RenderWithBlocks.tsx
|
|
59
|
+
/**
|
|
60
|
+
* Internal component for rendering Puck pages with MDX blocks.
|
|
61
|
+
*
|
|
62
|
+
* Uses Puck's Render component for static rendering in Astro.
|
|
63
|
+
* All content is pre-rendered at build time with no client-side JavaScript.
|
|
64
|
+
*
|
|
65
|
+
* MDX blocks are passed as React components (not HTML strings), which:
|
|
66
|
+
* - Avoids dangerouslySetInnerHTML security risks
|
|
67
|
+
* - More efficient (no HTML conversion round-trip)
|
|
68
|
+
* - Type-safe with proper React component types
|
|
69
|
+
*
|
|
70
|
+
* **Note:** This function is internal. Use `createPageRendererWithBlocks` instead,
|
|
71
|
+
* which provides a factory pattern to handle the closure correctly.
|
|
72
|
+
*
|
|
73
|
+
* @internal
|
|
74
|
+
* @param props - Component props
|
|
75
|
+
* @param props.config - Base Puck configuration
|
|
76
|
+
* @param props.data - Puck page data containing component tree
|
|
77
|
+
* @param props.mdxComponents - Array of evaluated MDX React components
|
|
78
|
+
*/
|
|
79
|
+
function RenderWithBlocks({ config, data, mdxComponents }) {
|
|
80
|
+
const blockComponents = {};
|
|
81
|
+
const blockCategoryList = [];
|
|
82
|
+
mdxComponents.forEach((block) => {
|
|
83
|
+
const componentKey = `Block_${block.id}`;
|
|
84
|
+
const BlockComponent = block.Component;
|
|
85
|
+
blockComponents[componentKey] = {
|
|
86
|
+
label: block.title,
|
|
87
|
+
fields: {},
|
|
88
|
+
defaultProps: {},
|
|
89
|
+
render: () => {
|
|
90
|
+
return /* @__PURE__ */ jsx("div", {
|
|
91
|
+
className: "mdx-content prose prose-sm max-w-none",
|
|
92
|
+
children: /* @__PURE__ */ jsx(BlockComponent, {})
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
blockCategoryList.push(componentKey);
|
|
97
|
+
});
|
|
98
|
+
const existingCategories = config.categories || {};
|
|
99
|
+
const mergedComponents = {
|
|
100
|
+
...config.components || {},
|
|
101
|
+
...blockComponents
|
|
102
|
+
};
|
|
103
|
+
const enhancedConfig = {
|
|
104
|
+
...config,
|
|
105
|
+
categories: {
|
|
106
|
+
...existingCategories,
|
|
107
|
+
blocks: {
|
|
108
|
+
title: "Content Blocks",
|
|
109
|
+
components: blockCategoryList
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
components: mergedComponents
|
|
113
|
+
};
|
|
114
|
+
return hasHydratableComponents(data, enhancedConfig) ? /* @__PURE__ */ jsx(RenderWithHydration, {
|
|
115
|
+
config: enhancedConfig,
|
|
116
|
+
data
|
|
117
|
+
}) : /* @__PURE__ */ jsx(Render, {
|
|
118
|
+
config: enhancedConfig,
|
|
119
|
+
data
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Factory function to create a PageRenderer with MDX components baked in via closure.
|
|
124
|
+
*
|
|
125
|
+
* This approach avoids dangerouslySetInnerHTML by keeping React components as components
|
|
126
|
+
* throughout the rendering pipeline. Components are captured in the closure and don't
|
|
127
|
+
* need to be passed as props (which Astro can't serialize).
|
|
128
|
+
*
|
|
129
|
+
* @param config - The Puck configuration with component definitions
|
|
130
|
+
* @param data - Puck page data containing component tree
|
|
131
|
+
* @param mdxComponents - Array of evaluated MDX components
|
|
132
|
+
* @returns A PageRenderer component with no props needed (components in closure)
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```astro
|
|
136
|
+
* ---
|
|
137
|
+
* import { createPageRendererWithBlocks } from '@conloca/astro-cms/components';
|
|
138
|
+
* import { evaluateMDXBlocks } from '@conloca/mdx/node';
|
|
139
|
+
* import { createContentAPI } from '@conloca/content-api/node';
|
|
140
|
+
* import config from '../puck.config';
|
|
141
|
+
*
|
|
142
|
+
* const api = await createContentAPI({ contentRoot: './content' });
|
|
143
|
+
* const mdxComponents = await evaluateMDXBlocks(api, 'en');
|
|
144
|
+
* const PageRenderer = createPageRendererWithBlocks(config, puckData, mdxComponents);
|
|
145
|
+
* ---
|
|
146
|
+
* <PageRenderer />
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* Benefits:
|
|
150
|
+
* - No dangerouslySetInnerHTML (safer, no XSS risk from HTML strings)
|
|
151
|
+
* - No string conversion round-trip (more efficient)
|
|
152
|
+
* - Type safe React components throughout
|
|
153
|
+
* - Normal React rendering with proper escaping
|
|
154
|
+
*/
|
|
155
|
+
function createPageRendererWithBlocks(config, data, mdxComponents) {
|
|
156
|
+
return function PageRendererWithBlocks() {
|
|
157
|
+
return /* @__PURE__ */ jsx(RenderWithBlocks, {
|
|
158
|
+
config,
|
|
159
|
+
data,
|
|
160
|
+
mdxComponents
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
export { createPageRendererWithBlocks };
|
|
167
|
+
//# sourceMappingURL=components.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.mjs","names":["blockComponents: Record<string, ComponentConfig>","blockCategoryList: string[]","enhancedConfig: Config"],"sources":["../src/components/HydrationWrapper.tsx","../src/components/RenderWithHydration.tsx","../src/components/RenderWithBlocks.tsx"],"sourcesContent":["import type { ReactNode } from 'react'\nimport type { HydrationStrategy } from '../types.js'\nimport { serializeProps } from '../lib/serialize-props.js'\n\ninterface HydrationWrapperProps {\n componentName: string\n strategy: HydrationStrategy\n props: Record<string, unknown>\n children: ReactNode\n}\n\n/**\n * Wraps hydratable components with data attributes for browser hydration.\n *\n * When strategy is 'none', renders children directly without wrapper overhead.\n * Otherwise, wraps in a div with data-hydrate attributes that the browser\n * hydration script uses to find and hydrate components.\n */\nexport function HydrationWrapper({\n componentName,\n strategy,\n props,\n children,\n}: HydrationWrapperProps) {\n // No hydration needed - render children directly without wrapper\n if (strategy === 'none') {\n return <>{children}</>\n }\n\n // Wrap with hydration markers for the browser script to find\n return (\n <div\n data-hydrate={componentName}\n data-hydrate-strategy={strategy}\n data-props={serializeProps(props)}\n >\n {children}\n </div>\n )\n}\n","import { Render } from '@measured/puck'\nimport type { Config, Data } from '@measured/puck'\nimport { HydrationWrapper } from './HydrationWrapper.js'\nimport { isHydratable } from '../lib/hydration-utils.js'\n\ninterface RenderWithHydrationProps {\n config: Config\n data: Data\n}\n\n/**\n * Wrapper around Puck's Render component that adds hydration support.\n *\n * For each component with a hydration strategy configured, wraps the\n * component's render output with HydrationWrapper to add data-hydrate\n * attributes for browser-side hydration.\n *\n * Non-hydratable components render without any wrapper overhead.\n */\nexport function RenderWithHydration({ config, data }: RenderWithHydrationProps) {\n // Create wrapped config with hydration-aware render functions\n const wrappedConfig: Config = {\n ...config,\n components: Object.fromEntries(\n Object.entries(config.components).map(([name, componentConfig]) => {\n const strategy = isHydratable(name, config)\n\n if (strategy === 'none') {\n // No wrapping needed - component stays static\n return [name, componentConfig]\n }\n\n // Wrap the render function so its output becomes HydrationWrapper children\n const originalRender = componentConfig.render\n return [\n name,\n {\n ...componentConfig,\n render: (props) => (\n <HydrationWrapper\n componentName={name}\n strategy={strategy}\n props={props}\n >\n {originalRender(props)}\n </HydrationWrapper>\n ),\n },\n ]\n })\n ),\n }\n\n return <Render config={wrappedConfig} data={data} />\n}\n","import type { ComponentConfig, Config, Data } from '@measured/puck';\nimport { Render } from '@measured/puck';\nimport type { ComponentType } from 'react';\nimport { RenderWithHydration } from './RenderWithHydration.js';\nimport { hasHydratableComponents } from '../lib/hydration-utils.js';\n\ninterface MDXComponent {\n id: string;\n title: string;\n Component: ComponentType;\n}\n\ninterface RenderWithBlocksProps {\n config: Config;\n data: Data;\n mdxComponents: MDXComponent[];\n}\n\n/**\n * Internal component for rendering Puck pages with MDX blocks.\n *\n * Uses Puck's Render component for static rendering in Astro.\n * All content is pre-rendered at build time with no client-side JavaScript.\n *\n * MDX blocks are passed as React components (not HTML strings), which:\n * - Avoids dangerouslySetInnerHTML security risks\n * - More efficient (no HTML conversion round-trip)\n * - Type-safe with proper React component types\n *\n * **Note:** This function is internal. Use `createPageRendererWithBlocks` instead,\n * which provides a factory pattern to handle the closure correctly.\n *\n * @internal\n * @param props - Component props\n * @param props.config - Base Puck configuration\n * @param props.data - Puck page data containing component tree\n * @param props.mdxComponents - Array of evaluated MDX React components\n */\nfunction RenderWithBlocks({ config, data, mdxComponents }: RenderWithBlocksProps) {\n // Build enhanced config with MDX blocks\n const blockComponents: Record<string, ComponentConfig> = {};\n const blockCategoryList: string[] = [];\n\n // Create component configs for each block\n mdxComponents.forEach((block) => {\n const componentKey = `Block_${block.id}`;\n\n // Capture component in closure - Puck doesn't persist defaultProps reliably\n const BlockComponent = block.Component;\n\n blockComponents[componentKey] = {\n label: block.title,\n fields: {},\n defaultProps: {},\n render: () => {\n // Use blockId and BlockComponent from closure - no prop dependency\n return (\n <div className=\"mdx-content prose prose-sm max-w-none\">\n <BlockComponent />\n </div>\n );\n },\n };\n blockCategoryList.push(componentKey);\n });\n\n // Merge with existing config\n const existingCategories = config.categories || {};\n const existingComponents = config.components || {};\n\n // Merge block components with existing config components\n const mergedComponents = {\n ...existingComponents,\n ...blockComponents,\n } as Config['components'];\n\n const enhancedConfig: Config = {\n ...config,\n categories: {\n ...existingCategories,\n blocks: {\n title: 'Content Blocks',\n components: blockCategoryList,\n },\n },\n components: mergedComponents,\n };\n\n // Check if page has hydratable components\n const needsHydration = hasHydratableComponents(data, enhancedConfig);\n\n // Render with hydration support if needed, otherwise static\n return needsHydration ? (\n <RenderWithHydration config={enhancedConfig} data={data} />\n ) : (\n <Render config={enhancedConfig} data={data} />\n );\n}\n\n/**\n * Factory function to create a PageRenderer with MDX components baked in via closure.\n *\n * This approach avoids dangerouslySetInnerHTML by keeping React components as components\n * throughout the rendering pipeline. Components are captured in the closure and don't\n * need to be passed as props (which Astro can't serialize).\n *\n * @param config - The Puck configuration with component definitions\n * @param data - Puck page data containing component tree\n * @param mdxComponents - Array of evaluated MDX components\n * @returns A PageRenderer component with no props needed (components in closure)\n *\n * @example\n * ```astro\n * ---\n * import { createPageRendererWithBlocks } from '@conloca/astro-cms/components';\n * import { evaluateMDXBlocks } from '@conloca/mdx/node';\n * import { createContentAPI } from '@conloca/content-api/node';\n * import config from '../puck.config';\n *\n * const api = await createContentAPI({ contentRoot: './content' });\n * const mdxComponents = await evaluateMDXBlocks(api, 'en');\n * const PageRenderer = createPageRendererWithBlocks(config, puckData, mdxComponents);\n * ---\n * <PageRenderer />\n * ```\n *\n * Benefits:\n * - No dangerouslySetInnerHTML (safer, no XSS risk from HTML strings)\n * - No string conversion round-trip (more efficient)\n * - Type safe React components throughout\n * - Normal React rendering with proper escaping\n */\nexport function createPageRendererWithBlocks(config: Config, data: Data, mdxComponents: MDXComponent[]): ComponentType {\n // Capture components in closure - no need to pass as props\n return function PageRendererWithBlocks() {\n return <RenderWithBlocks config={config} data={data} mdxComponents={mdxComponents} />;\n };\n}\n"],"mappings":";;;;;;;;;;;;AAkBA,SAAgB,iBAAiB,EAC/B,eACA,UACA,OACA,YACwB;AAExB,KAAI,aAAa,OACf,QAAO,gCAAG,WAAY;AAIxB,QACE,oBAAC;EACC,gBAAc;EACd,yBAAuB;EACvB,cAAY,eAAe,MAAM;EAEhC;GACG;;;;;;;;;;;;;;AClBV,SAAgB,oBAAoB,EAAE,QAAQ,QAAkC;AAkC9E,QAAO,oBAAC;EAAO,QAhCe;GAC5B,GAAG;GACH,YAAY,OAAO,YACjB,OAAO,QAAQ,OAAO,WAAW,CAAC,KAAK,CAAC,MAAM,qBAAqB;IACjE,MAAM,WAAW,aAAa,MAAM,OAAO;AAE3C,QAAI,aAAa,OAEf,QAAO,CAAC,MAAM,gBAAgB;IAIhC,MAAM,iBAAiB,gBAAgB;AACvC,WAAO,CACL,MACA;KACE,GAAG;KACH,SAAS,UACP,oBAAC;MACC,eAAe;MACL;MACH;gBAEN,eAAe,MAAM;OACL;KAEtB,CACF;KACD,CACH;GACF;EAE2C;GAAQ;;;;;;;;;;;;;;;;;;;;;;;;;ACftD,SAAS,iBAAiB,EAAE,QAAQ,MAAM,iBAAwC;CAEhF,MAAMA,kBAAmD,EAAE;CAC3D,MAAMC,oBAA8B,EAAE;AAGtC,eAAc,SAAS,UAAU;EAC/B,MAAM,eAAe,SAAS,MAAM;EAGpC,MAAM,iBAAiB,MAAM;AAE7B,kBAAgB,gBAAgB;GAC9B,OAAO,MAAM;GACb,QAAQ,EAAE;GACV,cAAc,EAAE;GAChB,cAAc;AAEZ,WACE,oBAAC;KAAI,WAAU;eACb,oBAAC,mBAAiB;MACd;;GAGX;AACD,oBAAkB,KAAK,aAAa;GACpC;CAGF,MAAM,qBAAqB,OAAO,cAAc,EAAE;CAIlD,MAAM,mBAAmB;EACvB,GAJyB,OAAO,cAAc,EAAE;EAKhD,GAAG;EACJ;CAED,MAAMC,iBAAyB;EAC7B,GAAG;EACH,YAAY;GACV,GAAG;GACH,QAAQ;IACN,OAAO;IACP,YAAY;IACb;GACF;EACD,YAAY;EACb;AAMD,QAHuB,wBAAwB,MAAM,eAAe,GAIlE,oBAAC;EAAoB,QAAQ;EAAsB;GAAQ,GAE3D,oBAAC;EAAO,QAAQ;EAAsB;GAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqClD,SAAgB,6BAA6B,QAAgB,MAAY,eAA8C;AAErH,QAAO,SAAS,yBAAyB;AACvC,SAAO,oBAAC;GAAyB;GAAc;GAAqB;IAAiB"}
|