UNPKG

react-intlayer

Version:

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

94 lines (92 loc) 3.38 kB
import { renderIntlayerNode } from "./IntlayerNode.mjs"; import { ContentSelectorRenderer } from "./editor/ContentSelectorWrapper.mjs"; import { EditedContentRenderer } from "./editor/useEditedContentRenderer.mjs"; import { MarkdownMetadataRenderer, MarkdownRenderer } from "./markdown/MarkdownRenderer.mjs"; import { renderReactElement } from "./reactElement/renderReactElement.mjs"; import { jsx } from "react/jsx-runtime"; import { getMarkdownMetadata } from "@intlayer/core"; import { NodeType } from "@intlayer/types"; //#region src/plugins.tsx /** Translation plugin. Replaces node with a locale string if nodeType = Translation. */ const intlayerNodePlugins = { id: "intlayer-node-plugin", canHandle: (node) => typeof node === "bigint" || typeof node === "string" || typeof node === "number", transform: (_node, { plugins, ...rest }) => renderIntlayerNode({ ...rest, value: rest.children, children: /* @__PURE__ */ jsx(EditedContentRenderer, { ...rest, children: rest.children }) }) }; /** Translation plugin. Replaces node with a locale string if nodeType = Translation. */ const reactNodePlugins = { id: "react-node-plugin", canHandle: (node) => typeof node === "object" && typeof node?.props !== "undefined" && typeof node.key !== "undefined", transform: (node, { plugins, ...rest }) => renderIntlayerNode({ ...rest, value: "[[react-element]]", children: /* @__PURE__ */ jsx(ContentSelectorRenderer, { ...rest, children: renderReactElement(node) }) }) }; /** Markdown string plugin. Replaces string node with a component that render the markdown. */ const markdownStringPlugin = { id: "markdown-string-plugin", canHandle: (node) => typeof node === "string", transform: (node, props, deepTransformNode) => { const { plugins, ...rest } = props; const metadataNodes = deepTransformNode(getMarkdownMetadata(node), { plugins: [{ id: "markdown-metadata-plugin", canHandle: (metadataNode) => typeof metadataNode === "string" || typeof metadataNode === "number" || typeof metadataNode === "boolean" || !metadataNode, transform: (metadataNode, props$1) => renderIntlayerNode({ ...props$1, value: metadataNode, children: /* @__PURE__ */ jsx(ContentSelectorRenderer, { ...rest, children: /* @__PURE__ */ jsx(MarkdownMetadataRenderer, { ...rest, metadataKeyPath: props$1.keyPath, children: node }) }) }) }], dictionaryKey: rest.dictionaryKey, keyPath: [] }); return renderIntlayerNode({ ...props, value: node, children: /* @__PURE__ */ jsx(ContentSelectorRenderer, { ...rest, children: /* @__PURE__ */ jsx(MarkdownRenderer, { ...rest, children: node }) }), additionalProps: { metadata: metadataNodes } }); } }; const markdownPlugin = { id: "markdown-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === NodeType.Markdown, transform: (node, props, deepTransformNode) => { const newKeyPath = [...props.keyPath, { type: NodeType.Markdown }]; const children = node[NodeType.Markdown]; return deepTransformNode(children, { ...props, children, keyPath: newKeyPath, plugins: [markdownStringPlugin, ...props.plugins ?? []] }); } }; //#endregion export { intlayerNodePlugins, markdownPlugin, markdownStringPlugin, reactNodePlugins }; //# sourceMappingURL=plugins.mjs.map