@amplifyup/sdk 0.1.23 → 0.1.24

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/dist/react.d.mts CHANGED
@@ -88,6 +88,8 @@ declare function Slot({ name, className, }: {
88
88
  * where field values should appear in their component, with full control
89
89
  * over styling and formatting.
90
90
  *
91
+ * For markdown / rich text fields, use {@link RichText} instead.
92
+ *
91
93
  * @example
92
94
  * ```tsx
93
95
  * function Hero({ title, description }) {
@@ -110,6 +112,23 @@ declare function Field({ name, fallback, className, render, }: {
110
112
  className?: string;
111
113
  render?: (value: any) => ReactNode;
112
114
  }): react_jsx_runtime.JSX.Element | null;
115
+ /**
116
+ * RichText - Renders a markdown (or HTML string) field as formatted HTML.
117
+ *
118
+ * Use for config/data fields of kind `richtext`. Values are stored as Markdown
119
+ * across Composer for portability across CMS platforms.
120
+ *
121
+ * @example
122
+ * ```tsx
123
+ * <RichText name="body" className="prose" />
124
+ * ```
125
+ */
126
+ declare function RichText({ name, fallback, className, as: Tag, }: {
127
+ name: string;
128
+ fallback?: ReactNode;
129
+ className?: string;
130
+ as?: keyof JSX.IntrinsicElements;
131
+ }): react_jsx_runtime.JSX.Element | null;
113
132
 
114
133
  interface AmplifyUpContextType {
115
134
  pageConfig: PageConfig | null;
@@ -125,4 +144,4 @@ interface AmplifyUpProviderProps {
125
144
  declare function AmplifyUpProvider({ children, config }: AmplifyUpProviderProps): react_jsx_runtime.JSX.Element;
126
145
  declare function useAmplifyUp(): AmplifyUpContextType;
127
146
 
128
- export { AmplifyContentMeta, AmplifyPageContent, AmplifyUpProvider, ComponentContextProvider, ContentSource, Field, PageConfig, Slot, useAmplifyUp };
147
+ export { AmplifyContentMeta, AmplifyPageContent, AmplifyUpProvider, ComponentContextProvider, ContentSource, Field, PageConfig, RichText, Slot, useAmplifyUp };
package/dist/react.d.ts CHANGED
@@ -88,6 +88,8 @@ declare function Slot({ name, className, }: {
88
88
  * where field values should appear in their component, with full control
89
89
  * over styling and formatting.
90
90
  *
91
+ * For markdown / rich text fields, use {@link RichText} instead.
92
+ *
91
93
  * @example
92
94
  * ```tsx
93
95
  * function Hero({ title, description }) {
@@ -110,6 +112,23 @@ declare function Field({ name, fallback, className, render, }: {
110
112
  className?: string;
111
113
  render?: (value: any) => ReactNode;
112
114
  }): react_jsx_runtime.JSX.Element | null;
115
+ /**
116
+ * RichText - Renders a markdown (or HTML string) field as formatted HTML.
117
+ *
118
+ * Use for config/data fields of kind `richtext`. Values are stored as Markdown
119
+ * across Composer for portability across CMS platforms.
120
+ *
121
+ * @example
122
+ * ```tsx
123
+ * <RichText name="body" className="prose" />
124
+ * ```
125
+ */
126
+ declare function RichText({ name, fallback, className, as: Tag, }: {
127
+ name: string;
128
+ fallback?: ReactNode;
129
+ className?: string;
130
+ as?: keyof JSX.IntrinsicElements;
131
+ }): react_jsx_runtime.JSX.Element | null;
113
132
 
114
133
  interface AmplifyUpContextType {
115
134
  pageConfig: PageConfig | null;
@@ -125,4 +144,4 @@ interface AmplifyUpProviderProps {
125
144
  declare function AmplifyUpProvider({ children, config }: AmplifyUpProviderProps): react_jsx_runtime.JSX.Element;
126
145
  declare function useAmplifyUp(): AmplifyUpContextType;
127
146
 
128
- export { AmplifyContentMeta, AmplifyPageContent, AmplifyUpProvider, ComponentContextProvider, ContentSource, Field, PageConfig, Slot, useAmplifyUp };
147
+ export { AmplifyContentMeta, AmplifyPageContent, AmplifyUpProvider, ComponentContextProvider, ContentSource, Field, PageConfig, RichText, Slot, useAmplifyUp };
package/dist/react.js CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  var react = require('react');
5
5
  var jsxRuntime = require('react/jsx-runtime');
6
+ var marked = require('marked');
6
7
 
7
8
  /* @amplifyup/sdk - https://amplifyup.ai */
8
9
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
@@ -2661,6 +2662,53 @@ function getSDK(adapter) {
2661
2662
  var trackContentView = (contentId, contentType, properties) => getSDK().trackContentView(contentId, contentType, properties);
2662
2663
  var refreshEventTracking = () => getSDK().refreshEventTracking();
2663
2664
  getSDK();
2665
+ var ComponentRenderBoundary = class extends react.Component {
2666
+ constructor() {
2667
+ super(...arguments);
2668
+ this.state = { error: null };
2669
+ }
2670
+ static getDerivedStateFromError(error) {
2671
+ return { error };
2672
+ }
2673
+ componentDidCatch(error) {
2674
+ console.error(
2675
+ `[AmplifyRenderer] "${this.props.componentId}" (${this.props.nodeId}) threw while rendering:`,
2676
+ error
2677
+ );
2678
+ }
2679
+ render() {
2680
+ if (this.state.error) {
2681
+ if (process.env.NODE_ENV !== "production") {
2682
+ return /* @__PURE__ */ jsxRuntime.jsxs(
2683
+ "div",
2684
+ {
2685
+ "data-amplifyup-render-error": "",
2686
+ "data-component-id": this.props.componentId,
2687
+ "data-node-id": this.props.nodeId,
2688
+ style: {
2689
+ margin: "1rem 0",
2690
+ padding: "0.75rem 1rem",
2691
+ border: "1px solid #fca5a5",
2692
+ borderRadius: "0.5rem",
2693
+ background: "#fef2f2",
2694
+ color: "#991b1b",
2695
+ fontSize: "0.875rem"
2696
+ },
2697
+ children: [
2698
+ 'Component "',
2699
+ this.props.componentId,
2700
+ '" failed to render:',
2701
+ " ",
2702
+ this.state.error.message
2703
+ ]
2704
+ }
2705
+ );
2706
+ }
2707
+ return null;
2708
+ }
2709
+ return this.props.children;
2710
+ }
2711
+ };
2664
2712
  function AmplifyRenderer({
2665
2713
  renderComponent,
2666
2714
  pageConfig: serverPageConfig,
@@ -3017,11 +3065,20 @@ function AmplifyRenderer({
3017
3065
  const slotsToPass = Object.keys(slotChildren).length > 0 ? slotChildren : void 0;
3018
3066
  console.log(`[AmplifyRenderer] Calling renderComponent for ${node.componentId}`, {
3019
3067
  hasSlots: !!slotsToPass,
3020
- slotKeys: slotsToPass ? Object.keys(slotsToPass) : []
3068
+ slotKeys: slotsToPass ? Object.keys(slotsToPass) : [],
3069
+ propKeys: Object.keys(node.props || {}),
3070
+ props: node.props || {}
3021
3071
  });
3022
- return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-node-id": node.id, "data-amplifyup-node": "", children: renderComponent(node.componentId, node.props || {}, slotsToPass, {
3023
- layoutNodeId: node.id
3024
- }) });
3072
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-node-id": node.id, "data-amplifyup-node": "", children: /* @__PURE__ */ jsxRuntime.jsx(
3073
+ ComponentRenderBoundary,
3074
+ {
3075
+ componentId: node.componentId,
3076
+ nodeId: node.id,
3077
+ children: renderComponent(node.componentId, node.props || {}, slotsToPass, {
3078
+ layoutNodeId: node.id
3079
+ })
3080
+ }
3081
+ ) });
3025
3082
  };
3026
3083
  const rawTree = pageConfig.layoutTree;
3027
3084
  const layoutTree = Array.isArray(rawTree) ? rawTree : rawTree && typeof rawTree === "object" && rawTree.componentId ? [rawTree] : [];
@@ -3267,6 +3324,25 @@ function Slot({
3267
3324
  }
3268
3325
  );
3269
3326
  }
3327
+ function formatFieldValue(value) {
3328
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
3329
+ return String(value);
3330
+ }
3331
+ if (Array.isArray(value)) {
3332
+ const parts = value.map((item) => {
3333
+ if (typeof item === "string") return item;
3334
+ if (item && typeof item === "object" && "children" in item && Array.isArray(item.children)) {
3335
+ return item.children.map((c) => typeof c?.text === "string" ? c.text : "").join("");
3336
+ }
3337
+ return "";
3338
+ }).filter(Boolean);
3339
+ if (parts.length > 0) return parts.join(" ");
3340
+ }
3341
+ if (value && typeof value === "object") {
3342
+ return "";
3343
+ }
3344
+ return "";
3345
+ }
3270
3346
  function Field({
3271
3347
  name,
3272
3348
  fallback,
@@ -3285,7 +3361,51 @@ function Field({
3285
3361
  if (render) {
3286
3362
  return /* @__PURE__ */ jsxRuntime.jsx("span", { className, children: render(fieldValue) });
3287
3363
  }
3288
- return /* @__PURE__ */ jsxRuntime.jsx("span", { className, children: String(fieldValue) });
3364
+ const text = formatFieldValue(fieldValue);
3365
+ if (!text) {
3366
+ return fallback ? /* @__PURE__ */ jsxRuntime.jsx("span", { className, children: fallback }) : null;
3367
+ }
3368
+ return /* @__PURE__ */ jsxRuntime.jsx("span", { className, children: text });
3369
+ }
3370
+ function markdownToHtml(markdown) {
3371
+ try {
3372
+ const result = marked.marked.parse(markdown, { async: false });
3373
+ return typeof result === "string" ? result : "";
3374
+ } catch {
3375
+ return markdown;
3376
+ }
3377
+ }
3378
+ function RichText({
3379
+ name,
3380
+ fallback,
3381
+ className,
3382
+ as: Tag = "div"
3383
+ }) {
3384
+ const context = react.useContext(ComponentContext);
3385
+ if (!context) {
3386
+ console.warn(`[AmplifyUp SDK] RichText "${name}" used outside ComponentContextProvider`);
3387
+ return fallback ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fallback }) : null;
3388
+ }
3389
+ const fieldValue = context.props?.[name];
3390
+ if (fieldValue === void 0 || fieldValue === null || fieldValue === "") {
3391
+ return fallback ? /* @__PURE__ */ jsxRuntime.jsx(Tag, { className, children: fallback }) : null;
3392
+ }
3393
+ let html = "";
3394
+ if (typeof fieldValue === "string") {
3395
+ const trimmed = fieldValue.trim();
3396
+ if (trimmed.startsWith("<") && trimmed.includes(">")) {
3397
+ html = fieldValue;
3398
+ } else {
3399
+ html = markdownToHtml(fieldValue);
3400
+ }
3401
+ } else {
3402
+ const text = formatFieldValue(fieldValue);
3403
+ html = text ? markdownToHtml(text) : "";
3404
+ }
3405
+ if (!html) {
3406
+ return fallback ? /* @__PURE__ */ jsxRuntime.jsx(Tag, { className, children: fallback }) : null;
3407
+ }
3408
+ return /* @__PURE__ */ jsxRuntime.jsx(Tag, { className, dangerouslySetInnerHTML: { __html: html } });
3289
3409
  }
3290
3410
  var usePathname = null;
3291
3411
  if (typeof window !== "undefined") {
@@ -3449,6 +3569,7 @@ exports.AmplifyRenderer = AmplifyRenderer;
3449
3569
  exports.AmplifyUpProvider = AmplifyUpProvider;
3450
3570
  exports.ComponentContextProvider = ComponentContextProvider;
3451
3571
  exports.Field = Field;
3572
+ exports.RichText = RichText;
3452
3573
  exports.Slot = Slot;
3453
3574
  exports.fetchPageConfigServer = fetchPageConfigServer;
3454
3575
  exports.isComposerPreview = isComposerPreview;