import { Block, BlockType, ColumnLayout, ContentDirection, CountdownBlock, CustomBlock, CustomBlockDefinition, CustomBlockLiquidModule, CustomFont, ImageBlock, SpacingValue, TemplateContent, convertMergeTagsToValues } from "@templatical/types"; /** * A function type that renders a single block to MJML markup. */ type BlockRenderer = (block: Block, context: RenderContext) => string; /** * Per-block-type renderer overrides, keyed by `block.type`. An entry replaces the * built-in renderer for that type wholesale — including its hidden-on-all- * viewports check, which becomes the override's responsibility. * * Generalises `renderCustomBlock`, which handles one block type. A backend can * inject renderers for server-generated countdown GIFs or composited video * thumbnails while retaining the built-in output for every other block type. */ type BlockRendererMap = Partial>; export declare const DEFAULT_SOCIAL_ICONS_BASE_URL: string; /** * Immutable context passed through the block rendering chain. */ export declare class RenderContext { /** * The column's share of the body width, ignoring any padding or border * around it. Emitted as the pixel width of a `"full"` image or video, which * MJML then caps at the real box ({@link contentWidth}). */ readonly containerWidth: number; readonly customFonts: CustomFont[]; readonly defaultFallbackFont: string; readonly allowHtmlBlocks: boolean; /** * Map of custom block id → pre-rendered HTML, populated by `renderToMjml` * before the synchronous render pass. Set when the consumer provides a * `renderCustomBlock` option. Empty by default. */ readonly customBlockHtml: ReadonlyMap; /** * Base URL (no trailing slash) for the social icon PNG assets. Resolved to * `${baseUrl}/${style}/${platform}.png`. Outlook desktop has no SVG support * and rejects base64 data URIs in ``, so PNGs must be served over * HTTP. Default points at the version-pinned jsDelivr mirror of this * package; consumers can override to self-host. */ readonly socialIconsBaseUrl: string; /** * Per-block-type renderer overrides from `RenderOptions.blockRenderers`. * Consulted before every built-in renderer — see {@link BlockRendererMap}. */ readonly blockRenderers: BlockRendererMap; /** * Writing direction of the email, resolved once in `renderToMjml`. * Nested columns copy it via `withContainerWidth` — dropping it there * would leave a two-column RTL section rendering LTR inside the columns. */ readonly contentDirection: ContentDirection; /** * The whole pixels MJML gives this container's content: the body width less * the side padding and border of every wrapper and section around it, then * the column's own percentage of what is left, floored as MJML floors it. * Width-aware decisions read this one, since it is the box MJML caps a block * at. Defaults to `containerWidth`. */ readonly contentWidth: number; constructor( /** * The column's share of the body width, ignoring any padding or border * around it. Emitted as the pixel width of a `"full"` image or video, which * MJML then caps at the real box ({@link contentWidth}). */ containerWidth: number, customFonts: CustomFont[], defaultFallbackFont: string, allowHtmlBlocks: boolean, /** * Map of custom block id → pre-rendered HTML, populated by `renderToMjml` * before the synchronous render pass. Set when the consumer provides a * `renderCustomBlock` option. Empty by default. */ customBlockHtml?: ReadonlyMap, /** * Base URL (no trailing slash) for the social icon PNG assets. Resolved to * `${baseUrl}/${style}/${platform}.png`. Outlook desktop has no SVG support * and rejects base64 data URIs in ``, so PNGs must be served over * HTTP. Default points at the version-pinned jsDelivr mirror of this * package; consumers can override to self-host. */ socialIconsBaseUrl?: string, /** * Per-block-type renderer overrides from `RenderOptions.blockRenderers`. * Consulted before every built-in renderer — see {@link BlockRendererMap}. */ blockRenderers?: BlockRendererMap, /** * Writing direction of the email, resolved once in `renderToMjml`. * Nested columns copy it via `withContainerWidth` — dropping it there * would leave a two-column RTL section rendering LTR inside the columns. */ contentDirection?: ContentDirection, contentWidth?: number); /** * Create a new context with a different container width. Used when rendering * columns, and wrappers, whose content is narrower. `contentWidth` defaults * to `width`, a container with nothing around it. */ withContainerWidth(width: number, contentWidth?: number): RenderContext; /** * The stack to emit for a block's or the template's `fontFamily`. A stack is * the author's own fallback chain and is emitted as written, as is anything * that doesn't parse as a font. A lone family gets a fallback: a custom * font's own `fallback` (or `defaultFallbackFont`), a built-in's fixed stack, * or, for any other family, `defaultFallbackFont`, so a client without the * face falls back to it rather than to its own default (Times New Roman in * Outlook on Windows). A generic family needs nothing appended. * * The rule is {@link resolveFontStack}, shared with the editor canvas so a * font previews with the fallbacks the recipient gets. */ resolveFontFamily(fontFamily: string): string; } /** * Escape element text (content between tags). Escapes `&`, `<` and `>` * (`&`, `<`, `>`). A `'` and a `"` stay raw: element text never * needs quote escaping, and a merge tag with a quoted argument * (`{{ x|default('y') }}`) must reach a send-time template engine intact. * Never for an attribute value: `escapeAttr` is the one to use there. */ export declare function escapeHtml(text: string | null | undefined): string; /** * Escape a value for a double-quoted attribute, which is how the renderer * writes every attribute. Escapes `&`, `<`, `>` and `"` (`&`, `<`, * `>`, `"`). A `'` stays raw: inside double quotes it cannot end the * value, and a quoted argument in a merge tag (`{{ x|default('y') }}`) must * reach a send-time template engine intact. */ export declare function escapeAttr(text: string | null | undefined): string; /** * Check if a block is hidden on all viewports. */ export declare function isHiddenOnAll(block: Block): boolean; /** * Get the MJML css-class attribute string for a block. * Returns a string like ` css-class="tpl-hide-desktop"` or empty string. * * `extraClasses` are appended to the visibility classes on the *same* * attribute. MJML keeps only one `css-class` per element, so a caller needing * both must merge here rather than emitting a second attribute. */ export declare function getCssClassAttr(block: Block, extraClasses?: string[]): string; /** * Get the CSS classes for visibility hiding. */ export declare function getCssClasses(block: Block): string; /** * Marks the `mj-text` of a block whose content is editor-authored rich text * (paragraph and title). MJML puts a `css-class` on the wrapping ``, which * is what {@link richTextStylesheet} scopes its selectors to. * * The scope is the point. A bare `p { … }` rule would also reach an `html` * block — the renderer puts consumer markup in `mj-text` too — and the editor * previews those in a sandboxed `srcdoc` iframe on UA defaults. Styling them * would trade this mismatch for another one, and an inlined declaration would * beat the consumer's own stylesheet. */ export declare const RICH_TEXT_CSS_CLASS = "tpl-rich-text"; /** * The class carrying one specific paragraph gap, e.g. `tpl-rich-text-8`. * * Every rich-text block gets one of these *instead of* a shared paragraph rule. * That is a hard constraint, not a preference: `.tpl-rich-text p` and * `.tpl-rich-text-4 p` have identical specificity (one class, one type), so a * surviving base rule would win on source order and flatten every per-block gap * back to the default. */ export declare function richTextGapClass(gap: number): string; /** * The paragraph gap a block asks for, or the built-in default. * * The fallback must produce a *rule* rather than nothing: silence hands the * paragraph back to MJML's `p { margin: 13px 0 }` skeleton default and reopens * the canvas/export mismatch. `0` is a legitimate gap, so this tests for * absence rather than falsiness. * * Mirrored on the canvas side by `getBlockWrapperStyle` in * `@templatical/editor`. */ export declare function resolveParagraphGap(spacing: number | undefined): number; /** * Marks the `mj-button` of a button whose `` MJML leaves at * `display:inline-block`: `width: "full"` and a percentage `width`. MJML * gives the `` a width only for a px `width`, so only the label would be * clickable. `renderToMjml` scopes the head rule that makes the `` fill * the button to this class. */ export declare const BUTTON_FULL_WIDTH_CSS_CLASS = "tpl-button-full"; /** * `fluid-on-mobile` for an `mj-image`. An image fills its column on desktop * when its width is `"full"`, or a pixel width MJML caps at the room it gives * the image (the column less the image's own side padding and border). On a * phone the attribute widens the image to the stacked column, so an image set * narrower must not carry it: a 120px logo would grow to the whole column, and * one with a set height would stretch out of shape. An image wider than the * phone shrinks to fit without it. * * `fluidOnMobile: true` always emits the attribute and `false` never does. * Omitted keeps the rule above. Video thumbnails have no such field and stay * on the rule. * * Exported for `blockRenderers` entries that emit their own `mj-image`. Pass * the context the renderer received, whose `contentWidth` already excludes the * wrappers and sections around the column. */ export declare function fluidOnMobileAttr(block: Pick & { fluidOnMobile?: boolean; }, context: RenderContext): string; /** * Get width percentages for each column in a layout. */ export declare function getWidthPercentages(layout: ColumnLayout): string[]; /** * Get width in pixels for each column in a layout. * * `"3"`, `"1-2"` and `"2-1"` divide by 3. `33.33%` of the container is a * different number, and a full-width image in those columns uses this width. */ export declare function getWidthPixels(layout: ColumnLayout, containerWidth: number): number[]; /** * Convert a SpacingValue to a CSS padding string like "10px 10px 10px 10px". */ export declare function toPaddingString(padding: SpacingValue): string; /** * Render a single block to MJML markup. * Dispatches to the appropriate block-type renderer. * * A `blockRenderers` override for the block's type wins over the built-in * renderer — see {@link RenderContext.blockRenderers}. A type with neither gets a * placeholder marker plus a warning rather than disappearing. */ export declare function renderBlock(block: Block, context: RenderContext): string; /** Render a host-provided countdown image with recipient-facing alt text. */ export declare function renderCountdownImage(block: CountdownBlock, src: string, options?: { locale?: string; }): string; /** * Prefix every unrenderable-block marker carries. Stable and greppable on * purpose: it is what a consumer searches for after noticing a gap in a rendered * email, and what a send pipeline can scan for before shipping. */ export declare const UNRENDERABLE_MARKER_PREFIX = "templatical:unrenderable-block"; export declare function renderUnrenderableBlock(block: Block): string; export interface RenderOptions { /** Template language to render. Defaults to `content.settings.locale`. */ locale?: string; customFonts?: CustomFont[]; defaultFallbackFont?: string; allowHtmlBlocks?: boolean; /** * Headless custom block definitions. The last definition per type wins. * Rendering a custom block from a definition needs `liquidjs` as well. */ customBlocks?: CustomBlockDefinition[]; /** * The `liquidjs` module (`import * as liquidjs from "liquidjs"`), which * renders `customBlocks` templates. Required when `customBlocks` is set and * the content holds a custom block; ignored otherwise. * * The renderer never imports `liquidjs` itself: a bundler resolves every * literal `import()`, so a renderer that imported it would warn in every * Webpack build that installs the renderer without `liquidjs`, the editor's * `toMjml()` path included. */ liquidjs?: CustomBlockLiquidModule; /** * Resolves custom blocks to their HTML representation. Called once per * custom block in the content tree before MJML rendering. The renderer * accepts `customBlocks` for headless rendering; this callback is the * editor's integration path. * * Editor consumers: pass `editor.renderCustomBlock`. * * With neither this callback nor `customBlocks`, each custom block uses * `block.renderedHtml` (if present) and is otherwise omitted. */ renderCustomBlock?: (block: CustomBlock) => Promise; /** * Per-block-type renderer overrides, keyed by `block.type`. An entry replaces * the built-in renderer for that type wholesale — including its * hidden-on-all-viewports check, which becomes the override's responsibility. * * ```ts * renderToMjml(content, { * blockRenderers: { * countdown: (block) => ``, * video: (block, ctx) => renderVideoWithPlayButton(block, ctx), * }, * }); * ``` * * This generalises {@link renderCustomBlock}, which is the same idea for a * single block type. It exists so a backend whose render output is a *superset* * of the browser's — a server-generated countdown GIF, a composited video * thumbnail — can inject exactly that delta instead of forking the renderer: * parity for every other block type then holds by construction. * * A block type with neither a built-in renderer nor an override emits an * `mj-raw` placeholder comment and warns, rather than vanishing from the * output. */ blockRenderers?: BlockRendererMap; /** * Resolves the definition-level CSS for a custom block type. Called once * per unique `customType` present in the content tree (not per instance). * The non-empty results are deduped by content and emitted as additional * `` blocks inside `` alongside the built-in visibility * media queries. * * Editor consumers: pass a function that reads * `blockRegistry.getDefinition(customType)?.stylesheet`. * * Headless consumers can pass `customBlocks` instead. A listed definition * decides its type's CSS; no stylesheet means none is emitted. * * Without a resolver or matching definition, each type's saved * `block.renderedStylesheet` is emitted. A resolver decides every type and * ignores saved copies, so a stale one never overrides a live definition. */ getCustomBlockStylesheet?: (customType: string) => string | undefined | null; /** * Base URL (no trailing slash) for the social icon PNG assets. Resolved to * `${baseUrl}/${style}/${platform}.png` per icon. Defaults to the * version-pinned jsDelivr mirror of this package. Override to self-host * (e.g., behind your own CDN or for air-gapped environments). * * Why PNGs: Outlook desktop (Word rendering engine) does not support SVG * and rejects base64 data URIs in ``, so social icons must be * served as raster images over HTTP for cross-client compatibility. */ socialIconsBaseUrl?: string; /** * Embedder shell applied at render. Cloned and spliced around `content` * via `applyLayout`; `content` is not mutated. Omitted, the document is * rendered as authored and must not contain `slot` or `wrapper`. */ layout?: TemplateContent; } /** * Render template content to an MJML string. * * With `customBlocks`, pass the `liquidjs` module as `liquidjs` too; it is * needed only when the content holds a custom block. The editor uses * `renderCustomBlock` via `toMjml()`; headless callers can pass definitions * directly. The function always returns a Promise. */ export declare function renderToMjml(content: TemplateContent, options?: RenderOptions): Promise; export { type BlockRenderer, type BlockRendererMap, convertMergeTagsToValues };