UNPKG

react-intlayer

Version:

Easily internationalize i18n your React applications with type-safe multilingual content management.

1 lines 8.22 kB
{"version":3,"sources":["../../src/plugins.tsx"],"sourcesContent":["import {\n KeyPath,\n NodeType,\n getMarkdownMetadata,\n type DeepTransformContent as DeepTransformContentCore,\n type IInterpreterPluginState as IInterpreterPluginStateCore,\n type MarkdownContent,\n type Plugins,\n} from '@intlayer/core';\nimport type { ReactNode } from 'react';\nimport { renderIntlayerNode, type IntlayerNode } from './IntlayerNode';\nimport { ContentSelectorRenderer } from './editor';\nimport { EditedContentRenderer } from './editor/useEditedContentRenderer';\nimport { MarkdownMetadataRenderer, MarkdownRenderer } from './markdown';\nimport { renderReactElement } from './reactElement/renderReactElement';\n\n/** ---------------------------------------------\n * INTLAYER NODE PLUGIN\n * --------------------------------------------- */\n\nexport type IntlayerNodeCond<T> = T extends number | string\n ? IntlayerNode<T>\n : never;\n\n/** Translation plugin. Replaces node with a locale string if nodeType = Translation. */\nexport const intlayerNodePlugins: Plugins = {\n id: 'intlayer-node-plugin',\n canHandle: (node) =>\n typeof node === 'bigint' ||\n typeof node === 'string' ||\n typeof node === 'number',\n transform: (\n _node,\n {\n plugins, // Removed to avoid next error - Functions cannot be passed directly to Client Components\n ...rest\n }\n ) =>\n renderIntlayerNode({\n ...rest,\n value: rest.children,\n children: (\n <ContentSelectorRenderer {...rest} key={rest.children}>\n <EditedContentRenderer {...rest}>\n {rest.children}\n </EditedContentRenderer>\n </ContentSelectorRenderer>\n ),\n }),\n};\n\n/** ---------------------------------------------\n * REACT NODE PLUGIN\n * --------------------------------------------- */\n\nexport type ReactNodeCond<T> = T extends {\n props: any;\n key: any;\n}\n ? ReactNode\n : never;\n\n/** Translation plugin. Replaces node with a locale string if nodeType = Translation. */\nexport const reactNodePlugins: Plugins = {\n id: 'react-node-plugin',\n canHandle: (node) =>\n typeof node === 'object' &&\n typeof node.props !== 'undefined' &&\n typeof node.key !== 'undefined',\n\n transform: (\n node,\n {\n plugins, // Removed to avoid next error - Functions cannot be passed directly to Client Components\n ...rest\n }\n ) =>\n renderIntlayerNode({\n ...rest,\n value: '[[react-element]]',\n children: (\n <ContentSelectorRenderer {...rest}>\n {renderReactElement(node)}\n </ContentSelectorRenderer>\n ),\n }),\n};\n\n/**\n * MARKDOWN PLUGIN\n */\n\nexport type MarkdownStringCond<T> = T extends string\n ? IntlayerNode<string, { metadata: DeepTransformContent<string> }>\n : never;\n\n/** Markdown string plugin. Replaces string node with a component that render the markdown. */\nexport const markdownStringPlugin: Plugins = {\n id: 'markdown-string-plugin',\n canHandle: (node) => typeof node === 'string',\n transform: (node: string, props, deepTransformNode) => {\n const {\n plugins, // Removed to avoid next error - Functions cannot be passed directly to Client Components\n ...rest\n } = props;\n\n const metadata = getMarkdownMetadata(node);\n\n const metadataPlugins: Plugins = {\n id: 'markdown-metadata-plugin',\n canHandle: (metadataNode) =>\n typeof metadataNode === 'string' ||\n typeof metadataNode === 'number' ||\n typeof metadataNode === 'boolean' ||\n !metadataNode,\n transform: (metadataNode, props) =>\n renderIntlayerNode({\n ...props,\n value: metadataNode,\n children: (\n <ContentSelectorRenderer {...rest}>\n <MarkdownMetadataRenderer\n {...rest}\n metadataKeyPath={props.keyPath}\n >\n {node}\n </MarkdownMetadataRenderer>\n </ContentSelectorRenderer>\n ),\n }),\n };\n\n // Transform metadata while keeping the same structure\n const metadataNodes = deepTransformNode(metadata, {\n plugins: [metadataPlugins],\n dictionaryKey: rest.dictionaryKey,\n keyPath: [],\n });\n\n return renderIntlayerNode({\n ...props,\n value: node,\n children: (\n <ContentSelectorRenderer {...rest}>\n <MarkdownRenderer {...rest}>{node}</MarkdownRenderer>\n </ContentSelectorRenderer>\n ),\n additionalProps: {\n metadata: metadataNodes,\n },\n });\n },\n};\n\nexport type MarkdownCond<T> = T extends {\n nodeType: NodeType | string;\n [NodeType.Markdown]: infer M;\n metadata?: infer U;\n}\n ? IntlayerNode<DeepTransformContent<M>, { metadata: DeepTransformContent<U> }>\n : never;\n\nexport const markdownPlugin: Plugins = {\n id: 'markdown-plugin',\n canHandle: (node) =>\n typeof node === 'object' && node?.nodeType === NodeType.Markdown,\n transform: (node: MarkdownContent, props, deepTransformNode) => {\n const newKeyPath: KeyPath[] = [\n ...props.keyPath,\n {\n type: NodeType.Markdown,\n },\n ];\n\n const children = node[NodeType.Markdown];\n\n return deepTransformNode(children, {\n ...props,\n children,\n keyPath: newKeyPath,\n plugins: [markdownStringPlugin, ...(props.plugins ?? [])],\n });\n },\n};\n/** ---------------------------------------------\n * PLUGINS RESULT\n * --------------------------------------------- */\n\nexport interface IInterpreterPluginReact<T> {\n reactNode: ReactNodeCond<T>;\n intlayerNode: IntlayerNodeCond<T>;\n markdown: MarkdownCond<T>;\n}\n\n/**\n * Insert this type as param of `DeepTransformContent` to avoid `intlayer` package pollution.\n *\n * Otherwise the the `react-intlayer` plugins will override the types of `intlayer` functions.\n */\nexport type IInterpreterPluginState = IInterpreterPluginStateCore & {\n reactNode: true;\n intlayerNode: true;\n markdown: true;\n};\n\nexport type DeepTransformContent<T> = DeepTransformContentCore<\n T,\n IInterpreterPluginState\n>;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2CU;AADF;AA1CR,kBAQO;AAEP,0BAAsD;AACtD,oBAAwC;AACxC,sCAAsC;AACtC,sBAA2D;AAC3D,gCAAmC;AAW5B,MAAM,sBAA+B;AAAA,EAC1C,IAAI;AAAA,EACJ,WAAW,CAAC,SACV,OAAO,SAAS,YAChB,OAAO,SAAS,YAChB,OAAO,SAAS;AAAA,EAClB,WAAW,CACT,OACA;AAAA,IACE;AAAA;AAAA,IACA,GAAG;AAAA,EACL,UAEA,wCAAmB;AAAA,IACjB,GAAG;AAAA,IACH,OAAO,KAAK;AAAA,IACZ,UACE,gDAAC,yCAAyB,GAAG,MAAM,KAAK,KAAK,YAC3C,4CAAC,yDAAuB,GAAG,MACxB,eAAK,UACR,CACF;AAAA,EAEJ,CAAC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