import { editor, internationalization } from '@intlayer/config/built'; import { conditionPlugin, type DeepTransformContent as DeepTransformContentCore, enumerationPlugin, fallbackPlugin, filePlugin, genderPlugin, type IInterpreterPluginState as IInterpreterPluginStateCore, isInterpolableWrapperNode, nestedPlugin, type Plugins, pluralPlugin, resolveInsertedSelector, resolveInterpreterLocale, selectPlugin, splitInsertionTemplate, transformInterpolableNode, translationPlugin, } from '@intlayer/core/interpreter'; import { getMarkdownMetadata } from '@intlayer/core/markdown'; import type { HTMLContent, InsertionContent, MarkdownContent, } from '@intlayer/core/transpiler'; import type { KeyPath } from '@intlayer/types/keyPath'; import type { DeclaredLocales, LocalesValues, } from '@intlayer/types/module_augmentation'; import type { NodeType } from '@intlayer/types/nodeType'; import * as NodeTypes from '@intlayer/types/nodeType'; import { createElement, type FC, Fragment, type ReactNode } from 'react'; import { reportExposure } from './analytics/exposureSink'; import { useLoadDynamic } from './client/useLoadDynamic'; import { ContentSelector } from './editor/ContentSelector'; import type { HTMLComponents } from './html/HTMLComponentTypes'; import { type IntlayerNode, renderIntlayerNode } from './IntlayerNode'; import { renderReactElement } from './reactElement/renderReactElement'; const markdownRendererModulePromise = process.env.INTLAYER_NODE_TYPE_MARKDOWN !== 'false' ? import('./markdown/MarkdownRendererPlugin').then( (m) => m.MarkdownRendererPlugin ) : null; const htmlRendererModulePromise = process.env.INTLAYER_NODE_TYPE_HTML !== 'false' ? import('./html/HTMLRendererPlugin').then((m) => m.HTMLRendererPlugin) : null; // These preloads are read later by `useLoadDynamic`, so the rejection must stay // on the promise: a `.catch()` chained into it would resolve with `undefined` // and be mistaken for a loaded module. Handle it on the side instead, so an // aborted chunk load never escapes as an unhandled rejection. markdownRendererModulePromise?.catch(() => {}); htmlRendererModulePromise?.catch(() => {}); /** --------------------------------------------- * INTLAYER NODE PLUGIN * --------------------------------------------- */ export type IntlayerNodeCond = T extends number | string ? IntlayerNode : never; /** Translation plugin. Replaces node with a locale string if nodeType = Translation. */ export const intlayerNodePlugins: Plugins = { id: 'intlayer-node-plugin', canHandle: (node) => typeof node === 'bigint' || typeof node === 'string' || typeof node === 'number', transform: (_node, props) => { // Node-level analytics: record which content is resolved for display. // No-op (and dead-code-eliminated) when analytics is disabled. if (process.env.INTLAYER_ANALYTICS_ENABLED !== 'false') { reportExposure({ dictionaryKey: props.dictionaryKey, keyPath: props.keyPath, locale: props.locale, nodeType: 'text', }); } // Props are only forwarded to the editor selector: copying them for every // leaf would cost an allocation per rendered string if (process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled) { const { plugins, // Removed to avoid next error - Functions cannot be passed directly to Client Components ...rest } = props; return renderIntlayerNode({ value: rest.children, children: {rest.children}, }); } return renderIntlayerNode({ value: props.children, children: props.children, }); }, }; /** --------------------------------------------- * REACT NODE PLUGIN * --------------------------------------------- */ export type ReactNodeCond = T extends { props: any; key: any; } ? ReactNode : never; /** Translation plugin. Replaces node with a locale string if nodeType = Translation. */ export const reactNodePlugins: Plugins = process.env.INTLAYER_NODE_TYPE_REACT_NODE === 'false' ? fallbackPlugin : { id: 'react-node-plugin', canHandle: (node) => typeof node === 'object' && typeof node?.props !== 'undefined' && typeof node.key !== 'undefined', transform: ( node, { plugins, // Removed to avoid next error - Functions cannot be passed directly to Client Components ...rest } ) => { if (process.env.INTLAYER_ANALYTICS_ENABLED !== 'false') { reportExposure({ dictionaryKey: rest.dictionaryKey, keyPath: rest.keyPath, locale: rest.locale, nodeType: 'react-node', }); } return renderIntlayerNode({ ...rest, value: '[[react-element]]', children: process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled ? ( {renderReactElement(node)} ) : ( renderReactElement(node) ), }); }, }; /** --------------------------------------------- * INSERTION PLUGIN * --------------------------------------------- */ export type InsertionCond = T extends { nodeType: NodeType | string; [NodeTypes.INSERTION]: infer I; fields: readonly string[]; } ? ( values: V ) => I extends string ? V[keyof V] extends string | number ? IntlayerNode : IntlayerNode : DeepTransformContent : never; /** * Split insertion string and join with React nodes using shared core logic */ const splitAndJoinInsertion = ( template: string, values: Record ): ReactNode => { const result = splitInsertionTemplate(template, values); if (result.isSimple) { // Simple string replacement return result.parts as string; } // Return as Fragment with proper keys return createElement( Fragment, null, ...(result.parts as any[]).map((part, index) => createElement(Fragment, { key: index }, part) ) ); }; /** Insertion plugin for React. Handles component/node insertion. */ export const insertionPlugin: Plugins = process.env.INTLAYER_NODE_TYPE_INSERTION === 'false' ? fallbackPlugin : { id: 'insertion-plugin', canHandle: (node) => typeof node === 'object' && node?.nodeType === NodeTypes.INSERTION, transform: (node: InsertionContent, props, deepTransformNode) => { const newKeyPath: KeyPath[] = [ ...props.keyPath, { type: NodeTypes.INSERTION, }, ]; const children = node[NodeTypes.INSERTION]; /** Insertion string plugin. Replaces string node with a component that render the insertion. */ const insertionStringPlugin: Plugins = { id: 'insertion-string-plugin', canHandle: (node) => typeof node === 'string' || isInterpolableWrapperNode(node), transform: (node, subProps, deepTransformNode) => { // `html()`/`markdown()` nodes carry their `{{ … }}` placeholders // inside a raw string. Interpolate into that string, then re-run // the transform so the html/markdown renderer applies afterwards. // React nodes cannot be injected into a raw markup string, so // string interpolation (not `splitAndJoinInsertion`) is used here. if (isInterpolableWrapperNode(node)) { return (values: { [K in InsertionContent['fields'][number]]: string | number; }) => transformInterpolableNode( node, values, subProps, props.plugins, deepTransformNode ); } const transformedResult = deepTransformNode(node, { ...subProps, children: node, plugins: (props.plugins ?? ([] as Plugins[])).filter( (plugin) => plugin.id !== 'intlayer-node-plugin' ), }); return (values: { [K in InsertionContent['fields'][number]]: | string | number | ReactNode; }) => { const result = splitAndJoinInsertion(transformedResult, values); return deepTransformNode(result, { ...subProps, plugins: props.plugins, children: result, }); }; }, }; const result = deepTransformNode(children, { ...props, children, keyPath: newKeyPath, plugins: [insertionStringPlugin, ...(props.plugins ?? [])], }); return resolveInsertedSelector(children, result); }, }; /** * MARKDOWN PLUGIN */ export type MarkdownStringCond = T extends string ? IntlayerNode< string, { metadata: DeepTransformContent; use: (components: HTMLComponents<'permissive', {}>) => ReactNode; } > : never; const MarkdownSuspenseRenderer: FC> = ({ children, ...props }) => { const MarkdownRendererPlugin = useLoadDynamic( 'markdown-renderer-plugin', markdownRendererModulePromise! ); return createElement(MarkdownRendererPlugin as any, { ...props, children }); }; /** Markdown string plugin. Replaces string node with a component that render the markdown. */ export const markdownStringPlugin: Plugins = process.env.INTLAYER_NODE_TYPE_MARKDOWN === 'false' ? fallbackPlugin : { id: 'markdown-string-plugin', canHandle: (node) => typeof node === 'string', transform: (node: string, props, deepTransformNode) => { const { plugins, // Removed to avoid next error - Functions cannot be passed directly to Client Components ...rest } = props; const metadata = getMarkdownMetadata(node) ?? {}; const metadataPlugins: Plugins = { id: 'markdown-metadata-plugin', canHandle: (metadataNode) => typeof metadataNode === 'string' || typeof metadataNode === 'number' || typeof metadataNode === 'boolean' || !metadataNode, transform: (metadataNode, props) => renderIntlayerNode({ ...props, value: metadataNode, children: process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled ? ( {node} ) : ( node ), }), }; // Transform metadata while keeping the same structure const metadataNodes = deepTransformNode(metadata, { plugins: [metadataPlugins], dictionaryKey: rest.dictionaryKey, keyPath: [], }); const render = (components?: HTMLComponents) => renderIntlayerNode({ ...props, value: node, children: process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled ? ( {node} ) : ( {node} ), additionalProps: { metadata: metadataNodes, use: render, }, }); return render(); }, }; export type MarkdownCond = T extends { nodeType: NodeType | string; [NodeTypes.MARKDOWN]: infer M; tags?: infer U; metadata?: infer V; } ? IntlayerNode< M, { use: (components?: HTMLComponents<'permissive', U>) => ReactNode; metadata: DeepTransformContent; } > : never; export const markdownPlugin: Plugins = process.env.INTLAYER_NODE_TYPE_MARKDOWN === 'false' ? fallbackPlugin : { id: 'markdown-plugin', canHandle: (node) => typeof node === 'object' && node?.nodeType === NodeTypes.MARKDOWN, transform: (node: MarkdownContent, props, deepTransformNode) => { const newKeyPath: KeyPath[] = [ ...props.keyPath, { type: NodeTypes.MARKDOWN, }, ]; const children = node[NodeTypes.MARKDOWN]; return deepTransformNode(children, { ...props, children, keyPath: newKeyPath, plugins: [markdownStringPlugin, ...(props.plugins ?? [])], }); }, }; /** --------------------------------------------- * HTML PLUGIN * --------------------------------------------- */ /** * HTML conditional type that enforces: * - All components (Standard or Custom) are OPTIONAL in the `use()` method. * - Custom components props are strictly inferred from the dictionary definition. * * This ensures type safety: * - `html('
Hello
').use({ CustomComponent: ... })` - optional but typed */ export type HTMLPluginCond = T extends { nodeType: NodeType | string; [NodeTypes.HTML]: infer I; tags?: infer U; } ? IntlayerNode< I, { use: (components?: HTMLComponents<'permissive', U>) => ReactNode; } > : never; const HTMLSuspenseRenderer: FC> = (props) => { const HTMLRendererPlugin = useLoadDynamic( 'html-renderer-plugin', htmlRendererModulePromise! ); return createElement(HTMLRendererPlugin as any, props); }; /** HTML plugin. Replaces node with a function that takes components => ReactNode. */ export const htmlPlugin: Plugins = process.env.INTLAYER_NODE_TYPE_HTML === 'false' ? fallbackPlugin : { id: 'html-plugin', canHandle: (node) => typeof node === 'object' && node?.nodeType === NodeTypes.HTML, transform: (node: HTMLContent, props) => { const html = node[NodeTypes.HTML]; const { plugins, ...rest } = props; // Type-safe render function that accepts properly typed components const render = (userComponents?: HTMLComponents): ReactNode => renderIntlayerNode({ ...rest, value: html, children: process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled ? ( ) : ( ), additionalProps: { use: render }, }); return render(); }, }; /** --------------------------------------------- * PLUGINS RESULT * --------------------------------------------- */ export type IInterpreterPluginReact = { reactNode: ReactNodeCond; reactIntlayerNode: IntlayerNodeCond; reactInsertion: InsertionCond; reactMarkdown: MarkdownCond; reactHtml: HTMLPluginCond; }; /** * Insert this type as param of `DeepTransformContent` to avoid `intlayer` package pollution. * * Otherwise the the `react-intlayer` plugins will override the types of `intlayer` functions. */ export type IInterpreterPluginState = Omit< IInterpreterPluginStateCore, 'insertion' // Remove insertion type from core package > & { reactNode: true; reactIntlayerNode: true; reactMarkdown: true; reactHtml: true; reactInsertion: true; }; export type DeepTransformContent< T, L extends LocalesValues = DeclaredLocales, > = DeepTransformContentCore; const pluginsCache = new Map(); /** * Get the plugins array for React content transformation. * This function is used by both getIntlayer and getDictionary to ensure consistent plugin configuration. */ export const getPlugins = ( locale?: LocalesValues, fallback: boolean = true ): Plugins[] => { const currentLocale = resolveInterpreterLocale(locale); const cacheKey = `${currentLocale}_${fallback}`; if (pluginsCache.has(cacheKey)) { return pluginsCache.get(cacheKey)!; } const plugins = [ // First: most nodes are plain strings, which every other plugin rejects intlayerNodePlugins, // Env var allows the bundler to to remove the plugin if not used to make the bundle smaller translationPlugin( currentLocale, fallback ? internationalization.defaultLocale : undefined ), enumerationPlugin, pluralPlugin(currentLocale), conditionPlugin, nestedPlugin(currentLocale), filePlugin, genderPlugin, selectPlugin, // Always include: handle React elements reactNodePlugins, insertionPlugin, markdownPlugin, htmlPlugin, ] as Plugins[]; // Plugins disabled at build time never match: skip them on every node const enabledPlugins = plugins.filter((plugin) => plugin !== fallbackPlugin); pluginsCache.set(cacheKey, enabledPlugins); return enabledPlugins; };