import { editor, internationalization } from '@intlayer/config/built'; import { conditionPlugin, type DeepTransformContent as DeepTransformContentCore, enumerationPlugin, fallbackPlugin, filePlugin, genderPlugin, getHTML, type IInterpreterPluginState as IInterpreterPluginStateCore, isInterpolableWrapperNode, nestedPlugin, type Plugins, pluralPlugin, resolveInsertedSelector, resolveInterpreterLocale, selectPlugin, splitInsertionTemplate, transformInterpolableNode, translationPlugin, } from '@intlayer/core/interpreter'; import { compileWithOptions, 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 type { HTMLComponents } from './html/types'; import { type IntlayerNode as IntlayerNodeCore, renderIntlayerNode, } from './renderIntlayerNode'; /** --------------------------------------------- * INTLAYER NODE PLUGIN * --------------------------------------------- */ export type IntlayerNodeCond = T extends number | string ? IntlayerNode : never; export type IntlayerNode = IntlayerNodeCore & P; const escapeHtmlAttr = (str: string): string => str.replace(/&/g, '&').replace(/"/g, '"'); const escapeHtmlText = (str: string): string => str.replace(/&/g, '&').replace(//g, '>'); export const intlayerNodePlugins: Plugins = { id: 'intlayer-node-plugin', canHandle: (node) => typeof node === 'bigint' || typeof node === 'string' || typeof node === 'number', transform: (_node, { children, keyPath, dictionaryKey }) => { if (process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled) { const rawStr = String(children ?? ''); const keyPathJson = JSON.stringify(keyPath ?? []); const dictKey = String(dictionaryKey ?? ''); /** * In editor mode, string coercion returns the wrapper HTML so that * `element.innerHTML = content.title` automatically inserts the * `` into the DOM. */ const htmlStr = `${escapeHtmlText(rawStr)}`; return renderIntlayerNode({ value: children as string, children: htmlStr, keyPath, dictionaryKey, additionalProps: { toElement: (): HTMLElement => { const wrapper = document.createElement( 'intlayer-content-selector-wrapper' ); wrapper.setAttribute('key-path', keyPathJson); wrapper.setAttribute('dictionary-key', dictKey); wrapper.textContent = rawStr; return wrapper; }, }, }); } return renderIntlayerNode({ value: children as string, children, }); }, }; /** --------------------------------------------- * INSERTION PLUGIN * --------------------------------------------- */ export type InsertionCond = T extends { nodeType: NodeType | string; [NodeTypes.INSERTION]: infer I; fields: readonly (infer F)[]; } ? ]: string | number }>( values: V ) => I extends string ? IntlayerNode : DeepTransformContent : never; 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]; 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. 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; }) => { const result = splitInsertionTemplate( transformedResult, values ); const resultStr = result.isSimple ? (result.parts as string) : (result.parts as string[]).join(''); return deepTransformNode(resultStr, { ...subProps, plugins: props.plugins, children: resultStr, }); }; }, }; const transformed = deepTransformNode(children, { ...props, children, keyPath: newKeyPath, plugins: [insertionStringPlugin, ...(props.plugins ?? [])], }); return resolveInsertedSelector(children, transformed); }, }; /** --------------------------------------------- * MARKDOWN PLUGIN * --------------------------------------------- */ const vanillaRuntime = { createElement: (tag: string, props: any, ...children: any[]) => { const attrs = Object.entries(props || {}) .filter( ([key]) => key !== 'children' && key !== 'key' && key !== '_innerHTML' ) .map(([key, value]) => `${key}="${value}"`) .join(' '); const innerHTML = props?._innerHTML; const childrenStr = innerHTML !== undefined ? innerHTML : children.flat(Infinity).join(''); return `<${tag}${attrs ? ` ${attrs}` : ''}>${childrenStr}`; }, normalizeProps: (_tag: string, props: Record) => { const normalized: Record = {}; for (const [key, value] of Object.entries(props)) { if (key === 'className') { normalized.class = value; } else if (key === 'htmlFor') { normalized.for = value; } else if (key === 'dangerouslySetInnerHTML' && (value as any)?.__html) { normalized._innerHTML = (value as any).__html; } else { normalized[key] = value; } } return normalized; }, }; export type MarkdownStringCond = T extends string ? IntlayerNode< string, { metadata: DeepTransformContent; /** Returns the rendered markdown; render with .use(). */ use: (components?: HTMLComponents<'permissive', {}>) => string; } > : never; 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: _plugins, ...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, subProps) => renderIntlayerNode({ ...subProps, value: metadataNode, children: node, }), }; const metadataNodes = deepTransformNode(metadata, { plugins: [metadataPlugins], dictionaryKey: rest.dictionaryKey, keyPath: [], }); const compile = (components: any = {}) => compileWithOptions( node, { ...vanillaRuntime, createElement: (tag: any, props: any, ...children: any[]) => { const override = components[tag]; if (typeof override === 'function') { return override({ ...props, children: children.join('') }); } return vanillaRuntime.createElement(tag, props, ...children); }, }, {} ) as any; const value = compile(); let children = node; if ( process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled ) { const keyPathJson = JSON.stringify(props.keyPath ?? []); const dictKey = String(props.dictionaryKey ?? ''); children = `${escapeHtmlText(value)}`; } return renderIntlayerNode({ ...props, value, children, additionalProps: { metadata: metadataNodes, use: (components?: any) => compile(components), }, }) as any; }, }; export type MarkdownCond = T extends { nodeType: NodeType | string; [NodeTypes.MARKDOWN]: infer M; metadata?: infer U; tags?: infer U; } ? { use: (components?: HTMLComponents<'permissive', U>) => IntlayerNode; 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 * --------------------------------------------- */ export type HTMLPluginCond = T extends { nodeType: NodeType | string; [NodeTypes.HTML]: infer I; tags?: infer U; } ? { use: (components?: HTMLComponents<'permissive', U>) => IntlayerNode; } : never; 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 htmlStr = node[NodeTypes.HTML]; const use = (components: Record = {}) => { const wrappedComponents = new Proxy(components, { get(target, prop) { if (typeof prop === 'string' && prop in target) { const Component = target[prop]; return (props: any) => { const children = Array.isArray(props.children) ? props.children.join('') : props.children; return Component({ ...props, children }); }; } if (typeof prop === 'string' && /^[a-z][a-z0-9]*$/.test(prop)) { return (props: any) => { const attrs = Object.entries(props) .filter(([key]) => key !== 'children' && key !== 'key') .map(([key, value]) => `${key}="${value}"`) .join(' '); const children = Array.isArray(props.children) ? props.children.join('') : props.children; return `<${prop}${attrs ? ` ${attrs}` : ''}>${children}`; }; } return undefined; }, }); const result = getHTML(htmlStr, wrappedComponents as any); return Array.isArray(result) ? result.join('') : result; }; const value = use(); let children = htmlStr; if ( process.env.INTLAYER_EDITOR_ENABLED !== 'false' && editor.enabled ) { const keyPathJson = JSON.stringify(props.keyPath ?? []); const dictKey = String(props.dictionaryKey ?? ''); children = `${escapeHtmlText(value)}`; } return renderIntlayerNode({ ...props, value, children, additionalProps: { use: (components?: any) => use(components), }, }); }, }; /** --------------------------------------------- * PLUGINS RESULT * --------------------------------------------- */ export interface IInterpreterPluginVanilla { vanillaIntlayerNode: IntlayerNodeCond; vanillaInsertion: InsertionCond; vanillaMarkdown: MarkdownCond; vanillaHtml: HTMLPluginCond; } /** * Insert this type as param of `DeepTransformContent` to avoid `intlayer` package pollution. * * Otherwise the `vanilla-intlayer` plugins will override the types of `intlayer` functions. * Use `getIntlayer` from `vanilla-intlayer` (not from `intlayer`) to get properly typed * `IntlayerNode` wrappers with `.value` access on string/number content. */ export type IInterpreterPluginState = Omit< IInterpreterPluginStateCore, 'insertion' > & { vanillaIntlayerNode: true; vanillaInsertion: true; vanillaMarkdown: true; vanillaHtml: true; }; export type DeepTransformContent< T, L extends LocalesValues = DeclaredLocales, > = DeepTransformContentCore; const pluginsCache = new Map(); /** * Get the plugins array for Vanilla content transformation. * This function is used by both getIntlayer and getDictionary to ensure consistent plugin configuration. */ export const getPlugins = ( locale?: LocalesValues, fallback = 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, translationPlugin( currentLocale, fallback ? internationalization.defaultLocale : undefined ), enumerationPlugin, pluralPlugin(currentLocale), conditionPlugin, nestedPlugin(currentLocale), filePlugin, genderPlugin, selectPlugin, 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; };