react-intlayer 8.0.0-canary.1 → 8.0.0-canary.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/client/useLocale.cjs +6 -6
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/getDictionary.cjs +1 -0
- package/dist/cjs/getDictionary.cjs.map +1 -1
- package/dist/cjs/getIntlayer.cjs +1 -0
- package/dist/cjs/getIntlayer.cjs.map +1 -1
- package/dist/cjs/html/HTMLRenderer.cjs +59 -0
- package/dist/cjs/html/HTMLRenderer.cjs.map +1 -0
- package/dist/cjs/html/HTMLRendererPlugin.cjs +3 -9
- package/dist/cjs/html/HTMLRendererPlugin.cjs.map +1 -1
- package/dist/cjs/html/index.cjs +6 -2
- package/dist/cjs/html/types.cjs +0 -0
- package/dist/cjs/index.cjs +7 -1
- package/dist/cjs/markdown/MarkdownProvider.cjs +62 -23
- package/dist/cjs/markdown/MarkdownProvider.cjs.map +1 -1
- package/dist/cjs/markdown/MarkdownRenderer.cjs +176 -6
- package/dist/cjs/markdown/MarkdownRenderer.cjs.map +1 -1
- package/dist/cjs/markdown/MarkdownRendererPlugin.cjs.map +1 -1
- package/dist/cjs/markdown/index.cjs +3 -1
- package/dist/cjs/markdown/processor.cjs +0 -5
- package/dist/cjs/markdown/processor.cjs.map +1 -1
- package/dist/cjs/plugins.cjs +50 -5
- package/dist/cjs/plugins.cjs.map +1 -1
- package/dist/cjs/reactElement/renderReactElement.cjs +8 -8
- package/dist/cjs/reactElement/renderReactElement.cjs.map +1 -1
- package/dist/cjs/server/IntlayerServerProvider.cjs +2 -2
- package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
- package/dist/esm/client/useLocale.mjs +6 -6
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/getDictionary.mjs +2 -1
- package/dist/esm/getDictionary.mjs.map +1 -1
- package/dist/esm/getIntlayer.mjs +2 -1
- package/dist/esm/getIntlayer.mjs.map +1 -1
- package/dist/esm/html/HTMLRenderer.mjs +55 -0
- package/dist/esm/html/HTMLRenderer.mjs.map +1 -0
- package/dist/esm/html/HTMLRendererPlugin.mjs +4 -9
- package/dist/esm/html/HTMLRendererPlugin.mjs.map +1 -1
- package/dist/esm/html/index.mjs +3 -2
- package/dist/esm/html/types.mjs +0 -0
- package/dist/esm/index.mjs +3 -2
- package/dist/esm/markdown/MarkdownProvider.mjs +62 -23
- package/dist/esm/markdown/MarkdownProvider.mjs.map +1 -1
- package/dist/esm/markdown/MarkdownRenderer.mjs +175 -7
- package/dist/esm/markdown/MarkdownRenderer.mjs.map +1 -1
- package/dist/esm/markdown/MarkdownRendererPlugin.mjs.map +1 -1
- package/dist/esm/markdown/index.mjs +2 -2
- package/dist/esm/markdown/processor.mjs +0 -5
- package/dist/esm/markdown/processor.mjs.map +1 -1
- package/dist/esm/plugins.mjs +52 -8
- package/dist/esm/plugins.mjs.map +1 -1
- package/dist/esm/reactElement/renderReactElement.mjs +8 -8
- package/dist/esm/reactElement/renderReactElement.mjs.map +1 -1
- package/dist/esm/server/IntlayerServerProvider.mjs +2 -2
- package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
- package/dist/types/IntlayerNode.d.ts.map +1 -1
- package/dist/types/UI/ContentSelector.d.ts.map +1 -1
- package/dist/types/client/IntlayerProvider.d.ts.map +1 -1
- package/dist/types/client/format/useCompact.d.ts +2 -3
- package/dist/types/client/format/useCompact.d.ts.map +1 -1
- package/dist/types/client/format/useCurrency.d.ts +2 -3
- package/dist/types/client/format/useCurrency.d.ts.map +1 -1
- package/dist/types/client/format/useDate.d.ts +0 -1
- package/dist/types/client/format/useDate.d.ts.map +1 -1
- package/dist/types/client/format/useIntl.d.ts +0 -1
- package/dist/types/client/format/useIntl.d.ts.map +1 -1
- package/dist/types/client/format/useList.d.ts +2 -3
- package/dist/types/client/format/useList.d.ts.map +1 -1
- package/dist/types/client/format/useNumber.d.ts +2 -3
- package/dist/types/client/format/useNumber.d.ts.map +1 -1
- package/dist/types/client/format/usePercentage.d.ts +2 -3
- package/dist/types/client/format/usePercentage.d.ts.map +1 -1
- package/dist/types/client/format/useRelativeTime.d.ts +2 -3
- package/dist/types/client/format/useRelativeTime.d.ts.map +1 -1
- package/dist/types/client/format/useUnit.d.ts +2 -3
- package/dist/types/client/format/useUnit.d.ts.map +1 -1
- package/dist/types/client/t.d.ts +0 -1
- package/dist/types/client/t.d.ts.map +1 -1
- package/dist/types/client/useDictionary.d.ts +2 -3
- package/dist/types/client/useDictionary.d.ts.map +1 -1
- package/dist/types/client/useDictionaryAsync.d.ts +0 -1
- package/dist/types/client/useDictionaryAsync.d.ts.map +1 -1
- package/dist/types/client/useDictionaryDynamic.d.ts +2 -3
- package/dist/types/client/useDictionaryDynamic.d.ts.map +1 -1
- package/dist/types/client/useI18n.d.ts +0 -1
- package/dist/types/client/useI18n.d.ts.map +1 -1
- package/dist/types/client/useIntlayer.d.ts +3 -4
- package/dist/types/client/useIntlayer.d.ts.map +1 -1
- package/dist/types/client/useLoadDynamic.d.ts.map +1 -1
- package/dist/types/client/useLocale.d.ts.map +1 -1
- package/dist/types/client/useLocaleBase.d.ts +5 -5
- package/dist/types/client/useLocaleBase.d.ts.map +1 -1
- package/dist/types/client/useLocaleStorage.d.ts +5 -6
- package/dist/types/client/useLocaleStorage.d.ts.map +1 -1
- package/dist/types/client/useTraduction.d.ts +0 -1
- package/dist/types/client/useTraduction.d.ts.map +1 -1
- package/dist/types/editor/ContentSelectorWrapper.d.ts.map +1 -1
- package/dist/types/editor/IntlayerEditorProvider.d.ts.map +1 -1
- package/dist/types/editor/useEditedContentRenderer.d.ts.map +1 -1
- package/dist/types/getDictionary.d.ts.map +1 -1
- package/dist/types/getIntlayer.d.ts.map +1 -1
- package/dist/types/html/HTMLProvider.d.ts.map +1 -1
- package/dist/types/html/HTMLRenderer.d.ts +46 -0
- package/dist/types/html/HTMLRenderer.d.ts.map +1 -0
- package/dist/types/html/HTMLRendererPlugin.d.ts +2 -7
- package/dist/types/html/HTMLRendererPlugin.d.ts.map +1 -1
- package/dist/types/html/index.d.ts +4 -2
- package/dist/types/html/types.d.ts +26 -0
- package/dist/types/html/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +4 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/markdown/MarkdownProvider.d.ts +34 -36
- package/dist/types/markdown/MarkdownProvider.d.ts.map +1 -1
- package/dist/types/markdown/MarkdownRenderer.d.ts +235 -13
- package/dist/types/markdown/MarkdownRenderer.d.ts.map +1 -1
- package/dist/types/markdown/MarkdownRendererPlugin.d.ts.map +1 -1
- package/dist/types/markdown/index.d.ts +2 -2
- package/dist/types/markdown/processor.d.ts +0 -1
- package/dist/types/markdown/processor.d.ts.map +1 -1
- package/dist/types/markdown/runtime.d.ts +0 -1
- package/dist/types/markdown/runtime.d.ts.map +1 -1
- package/dist/types/plugins.d.ts +17 -4
- package/dist/types/plugins.d.ts.map +1 -1
- package/dist/types/reactElement/renderReactElement.d.ts +2 -2
- package/dist/types/reactElement/renderReactElement.d.ts.map +1 -1
- package/dist/types/server/IntlayerServerProvider.d.ts +2 -2
- package/dist/types/server/IntlayerServerProvider.d.ts.map +1 -1
- package/dist/types/server/format/useCompact.d.ts +0 -1
- package/dist/types/server/format/useCompact.d.ts.map +1 -1
- package/dist/types/server/format/useCurrency.d.ts +0 -1
- package/dist/types/server/format/useCurrency.d.ts.map +1 -1
- package/dist/types/server/format/useDate.d.ts +0 -1
- package/dist/types/server/format/useDate.d.ts.map +1 -1
- package/dist/types/server/format/useIntl.d.ts +0 -1
- package/dist/types/server/format/useIntl.d.ts.map +1 -1
- package/dist/types/server/format/useList.d.ts +0 -1
- package/dist/types/server/format/useList.d.ts.map +1 -1
- package/dist/types/server/format/useNumber.d.ts +0 -1
- package/dist/types/server/format/useNumber.d.ts.map +1 -1
- package/dist/types/server/format/usePercentage.d.ts +0 -1
- package/dist/types/server/format/usePercentage.d.ts.map +1 -1
- package/dist/types/server/format/useRelativeTime.d.ts +0 -1
- package/dist/types/server/format/useRelativeTime.d.ts.map +1 -1
- package/dist/types/server/format/useUnit.d.ts +0 -1
- package/dist/types/server/format/useUnit.d.ts.map +1 -1
- package/dist/types/server/serverContext.d.ts +0 -1
- package/dist/types/server/serverContext.d.ts.map +1 -1
- package/dist/types/server/t.d.ts +0 -1
- package/dist/types/server/t.d.ts.map +1 -1
- package/dist/types/server/useDictionary.d.ts +2 -3
- package/dist/types/server/useDictionary.d.ts.map +1 -1
- package/dist/types/server/useDictionaryAsync.d.ts +2 -3
- package/dist/types/server/useDictionaryAsync.d.ts.map +1 -1
- package/dist/types/server/useDictionaryDynamic.d.ts +2 -3
- package/dist/types/server/useDictionaryDynamic.d.ts.map +1 -1
- package/dist/types/server/useI18n.d.ts +0 -1
- package/dist/types/server/useI18n.d.ts.map +1 -1
- package/dist/types/server/useIntlayer.d.ts +3 -3
- package/dist/types/server/useIntlayer.d.ts.map +1 -1
- package/dist/types/server/useLoadDynamic.d.ts.map +1 -1
- package/dist/types/server/useLocale.d.ts.map +1 -1
- package/package.json +11 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarkdownRendererPlugin.cjs","names":["useMarkdownContext","useEditedContentRenderer"],"sources":["../../../src/markdown/MarkdownRendererPlugin.tsx"],"sourcesContent":["'use client';\n\nimport {
|
|
1
|
+
{"version":3,"file":"MarkdownRendererPlugin.cjs","names":["useMarkdownContext","useEditedContentRenderer"],"sources":["../../../src/markdown/MarkdownRendererPlugin.tsx"],"sourcesContent":["'use client';\n\nimport { getContentNodeByKeyPath, getMarkdownMetadata } from '@intlayer/core';\nimport { useEditorLocale } from '@intlayer/editor-react';\nimport type { ContentNode, KeyPath, LocalesValues } from '@intlayer/types';\nimport type { FC, ReactNode } from 'react';\nimport { useEditedContentRenderer } from '../editor/useEditedContentRenderer';\nimport { useMarkdownContext } from './MarkdownProvider';\n\ntype MarkdownRendererPluginProps = {\n dictionaryKey: string;\n keyPath: KeyPath[];\n locale?: LocalesValues;\n children: string;\n [key: string]: any;\n};\n\nexport const MarkdownRendererPlugin: FC<MarkdownRendererPluginProps> = (\n props\n): ReactNode => {\n const { dictionaryKey, keyPath, children, locale, ...components } = props;\n const context = useMarkdownContext();\n const renderMarkdown = context?.renderMarkdown ?? ((md) => md);\n const editedContentContext = useEditedContentRenderer({\n dictionaryKey,\n keyPath,\n children,\n });\n\n const contentToRender =\n typeof editedContentContext === 'string' ? editedContentContext : children;\n\n return renderMarkdown(contentToRender, components);\n};\n\ntype MarkdownMetadataRendererProps = MarkdownRendererPluginProps & {\n metadataKeyPath: KeyPath[];\n};\n\nexport const MarkdownMetadataRenderer: FC<MarkdownMetadataRendererProps> = ({\n dictionaryKey,\n keyPath,\n children,\n metadataKeyPath,\n}): ReactNode => {\n const editedContentContext = useEditedContentRenderer({\n dictionaryKey,\n keyPath,\n children,\n });\n const currentLocale = useEditorLocale();\n\n const metadata = getMarkdownMetadata(editedContentContext);\n\n const metadataEl = getContentNodeByKeyPath(\n metadata as ContentNode,\n metadataKeyPath,\n currentLocale\n );\n\n return metadataEl as ReactNode;\n};\n"],"mappings":";;;;;;;;;AAiBA,MAAa,0BACX,UACc;CACd,MAAM,EAAE,eAAe,SAAS,UAAU,QAAQ,GAAG,eAAe;CAEpE,MAAM,iBADUA,sDAAoB,EACJ,oBAAoB,OAAO;CAC3D,MAAM,uBAAuBC,iEAAyB;EACpD;EACA;EACA;EACD,CAAC;AAKF,QAAO,eAFL,OAAO,yBAAyB,WAAW,uBAAuB,UAE7B,WAAW;;AAOpD,MAAa,4BAA+D,EAC1E,eACA,SACA,UACA,sBACe;CACf,MAAM,uBAAuBA,iEAAyB;EACpD;EACA;EACA;EACD,CAAC;CACF,MAAM,6DAAiC;AAUvC,4FARqC,qBAAqB,EAIxD,iBACA,cACD"}
|
|
@@ -17,6 +17,8 @@ exports.compileMarkdown = require_markdown_processor.compileMarkdown;
|
|
|
17
17
|
exports.compiler = require_markdown_processor.compiler;
|
|
18
18
|
exports.createReactRuntime = require_markdown_runtime.createReactRuntime;
|
|
19
19
|
exports.reactRuntime = require_markdown_runtime.reactRuntime;
|
|
20
|
+
exports.renderMarkdown = require_markdown_MarkdownRenderer.renderMarkdown;
|
|
20
21
|
exports.sanitizer = _intlayer_core.sanitizer;
|
|
21
22
|
exports.slugify = _intlayer_core.slugify;
|
|
22
|
-
exports.useMarkdownContext = require_markdown_MarkdownProvider.useMarkdownContext;
|
|
23
|
+
exports.useMarkdownContext = require_markdown_MarkdownProvider.useMarkdownContext;
|
|
24
|
+
exports.useMarkdownRenderer = require_markdown_MarkdownRenderer.useMarkdownRenderer;
|
|
@@ -4,11 +4,6 @@ let _intlayer_core = require("@intlayer/core");
|
|
|
4
4
|
|
|
5
5
|
//#region src/markdown/processor.tsx
|
|
6
6
|
/**
|
|
7
|
-
* React adapter for the framework-agnostic markdown processor.
|
|
8
|
-
*
|
|
9
|
-
* Provides a backward-compatible compiler and LegacyMarkdownRenderer component.
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
7
|
* Default React runtime for markdown rendering.
|
|
13
8
|
*/
|
|
14
9
|
const DEFAULT_RUNTIME = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processor.cjs","names":["createElement"],"sources":["../../../src/markdown/processor.tsx"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"processor.cjs","names":["createElement"],"sources":["../../../src/markdown/processor.tsx"],"sourcesContent":["import {\n type Overrides as CoreOverrides,\n compile as coreCompile,\n sanitizer as defaultSanitizer,\n slugify as defaultSlugify,\n type MarkdownContext,\n type MarkdownOptions,\n type MarkdownRuntime,\n type ParserResult,\n type ParseState,\n type RenderRuleHook,\n type RuleOutput,\n RuleType,\n} from '@intlayer/core';\nimport {\n cloneElement,\n createElement,\n type FC,\n Fragment,\n type HTMLAttributes,\n type JSX,\n type ReactNode,\n} from 'react';\n\n// Re-export RuleType for compatibility\nexport { RuleType };\n\n// Re-export utilities for compatibility\nexport { defaultSlugify as slugify, defaultSanitizer as sanitizer };\n\ntype HTMLTags = keyof JSX.IntrinsicElements;\n\ntype State = ParseState;\n\n/**\n * Refined MarkdownRendererOptions type.\n */\nexport type MarkdownRendererOptions = Partial<{\n /**\n * Ultimate control over the output of all rendered JSX.\n */\n createElement: (\n tag: Parameters<typeof createElement>[0],\n props: JSX.IntrinsicAttributes,\n ...children: ReactNode[]\n ) => ReactNode;\n\n /**\n * The library automatically generates an anchor tag for bare URLs included in the markdown\n * document, but this behavior can be disabled if desired.\n */\n disableAutoLink: boolean;\n\n /**\n * Disable the compiler's best-effort transcription of provided raw HTML\n * into JSX-equivalent.\n */\n disableParsingRawHTML: boolean;\n\n /**\n * Forces the compiler to have space between hash sign and the header text.\n */\n enforceAtxHeadings: boolean;\n\n /**\n * Forces the compiler to always output content with a block-level wrapper.\n */\n forceBlock: boolean;\n\n /**\n * Forces the compiler to always output content with an inline wrapper.\n */\n forceInline: boolean;\n\n /**\n * Forces the compiler to wrap results, even if there is only a single child.\n */\n forceWrapper: boolean;\n\n /**\n * Supply additional HTML entity: unicode replacement mappings.\n */\n namedCodesToUnicode: {\n [key: string]: string;\n };\n\n /**\n * Selectively control the output of particular HTML tags.\n */\n components: CoreOverrides;\n\n /**\n * Allows for full control over rendering of particular rules.\n */\n renderRule: RenderRuleHook;\n\n /**\n * Override the built-in sanitizer function for URLs.\n */\n sanitizer: (value: string, tag: HTMLTags, attribute: string) => string | null;\n\n /**\n * Override normalization of non-URI-safe characters for anchor linking.\n */\n slugify: (input: string) => string;\n\n /**\n * Declare the type of the wrapper to be used when there are multiple children.\n */\n wrapper: any | null;\n\n /**\n * Whether to preserve frontmatter in the markdown content.\n */\n preserveFrontmatter: boolean;\n\n /**\n * Whether to use the GitHub Tag Filter.\n */\n tagfilter: boolean;\n}>;\n\n/**\n * Default React runtime for markdown rendering.\n */\nconst DEFAULT_RUNTIME: MarkdownRuntime = {\n createElement: createElement as any,\n cloneElement,\n Fragment,\n normalizeProps: (_tag, props) => props,\n};\n\n/**\n * Compile markdown to React elements.\n * This is the primary export - use this for new code.\n */\nexport const compileMarkdown = (\n markdown: string = '',\n options: MarkdownRendererOptions = {}\n): JSX.Element => {\n const {\n createElement: customCreateElement,\n disableAutoLink,\n disableParsingRawHTML,\n enforceAtxHeadings,\n forceBlock,\n forceInline,\n forceWrapper,\n namedCodesToUnicode,\n components,\n renderRule,\n sanitizer,\n slugify,\n wrapper,\n preserveFrontmatter,\n tagfilter,\n } = options;\n\n const runtime = customCreateElement\n ? { ...DEFAULT_RUNTIME, createElement: customCreateElement as any }\n : DEFAULT_RUNTIME;\n\n const ctx: MarkdownContext = {\n runtime,\n components,\n namedCodesToUnicode,\n sanitizer: sanitizer as any,\n slugify,\n };\n\n const compilerOptions: MarkdownOptions = {\n disableAutoLink,\n disableParsingRawHTML,\n enforceAtxHeadings,\n forceBlock,\n forceInline,\n forceWrapper,\n renderRule,\n wrapper,\n preserveFrontmatter,\n tagfilter,\n };\n\n return coreCompile(markdown, ctx, compilerOptions) as JSX.Element;\n};\n\n// Backward compatibility aliases\nexport const compiler = compileMarkdown;\nexport const compile = compileMarkdown;\n\n/**\n * React component that renders markdown to JSX (legacy).\n */\nexport const LegacyMarkdownRenderer: FC<\n Omit<HTMLAttributes<Element>, 'children'> & {\n children: string;\n options?: MarkdownRendererOptions;\n }\n> = ({ children = '', options, ...props }) => {\n if (process.env.NODE_ENV !== 'production' && typeof children !== 'string') {\n console.error(\n 'intlayer: <Markdown> component only accepts a single string as a child, received:',\n children\n );\n }\n\n return cloneElement(\n compiler(children, options),\n props as JSX.IntrinsicAttributes\n );\n};\n\nexport type {\n State,\n ParseState,\n ParserResult,\n RuleOutput,\n CoreOverrides as Overrides,\n};\n"],"mappings":";;;;;;;;AA6HA,MAAM,kBAAmC;CACvC,eAAeA;CACf;CACA;CACA,iBAAiB,MAAM,UAAU;CAClC;;;;;AAMD,MAAa,mBACX,WAAmB,IACnB,UAAmC,EAAE,KACrB;CAChB,MAAM,EACJ,eAAe,qBACf,iBACA,uBACA,oBACA,YACA,aACA,cACA,qBACA,YACA,YACA,WACA,SACA,SACA,qBACA,cACE;AA2BJ,oCAAmB,UArBU;EAC3B,SALc,sBACZ;GAAE,GAAG;GAAiB,eAAe;GAA4B,GACjE;EAIF;EACA;EACW;EACX;EACD,EAEwC;EACvC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAEiD;;AAIpD,MAAa,WAAW;AACxB,MAAa,UAAU;;;;AAKvB,MAAa,0BAKR,EAAE,WAAW,IAAI,SAAS,GAAG,YAAY;AAC5C,KAAI,QAAQ,IAAI,aAAa,gBAAgB,OAAO,aAAa,SAC/D,SAAQ,MACN,qFACA,SACD;AAGH,gCACE,SAAS,UAAU,QAAQ,EAC3B,MACD"}
|
package/dist/cjs/plugins.cjs
CHANGED
|
@@ -37,6 +37,49 @@ const reactNodePlugins = {
|
|
|
37
37
|
})
|
|
38
38
|
})
|
|
39
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Split insertion string and join with React nodes using shared core logic
|
|
42
|
+
*/
|
|
43
|
+
const splitAndJoinInsertion = (template, values) => {
|
|
44
|
+
const result = (0, _intlayer_core.splitInsertionTemplate)(template, values);
|
|
45
|
+
if (result.isSimple) return result.parts;
|
|
46
|
+
return (0, react.createElement)(react.Fragment, null, ...result.parts.map((part, index) => (0, react.createElement)(react.Fragment, { key: index }, part)));
|
|
47
|
+
};
|
|
48
|
+
/** Insertion plugin for React. Handles component/node insertion. */
|
|
49
|
+
const insertionPlugin = {
|
|
50
|
+
id: "insertion-plugin",
|
|
51
|
+
canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types.NodeType.Insertion,
|
|
52
|
+
transform: (node, props, deepTransformNode) => {
|
|
53
|
+
const newKeyPath = [...props.keyPath, { type: _intlayer_types.NodeType.Insertion }];
|
|
54
|
+
const children = node[_intlayer_types.NodeType.Insertion];
|
|
55
|
+
/** Insertion string plugin. Replaces string node with a component that render the insertion. */
|
|
56
|
+
const insertionStringPlugin = {
|
|
57
|
+
id: "insertion-string-plugin",
|
|
58
|
+
canHandle: (node) => typeof node === "string",
|
|
59
|
+
transform: (node, subProps, deepTransformNode) => {
|
|
60
|
+
const transformedResult = deepTransformNode(node, {
|
|
61
|
+
...subProps,
|
|
62
|
+
children: node,
|
|
63
|
+
plugins: [...(props.plugins ?? []).filter((plugin) => plugin.id !== "intlayer-node-plugin")]
|
|
64
|
+
});
|
|
65
|
+
return (values) => {
|
|
66
|
+
const result = splitAndJoinInsertion(transformedResult, values);
|
|
67
|
+
return deepTransformNode(result, {
|
|
68
|
+
...subProps,
|
|
69
|
+
plugins: props.plugins,
|
|
70
|
+
children: result
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
return deepTransformNode(children, {
|
|
76
|
+
...props,
|
|
77
|
+
children,
|
|
78
|
+
keyPath: newKeyPath,
|
|
79
|
+
plugins: [insertionStringPlugin, ...props.plugins ?? []]
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
40
83
|
/** Markdown string plugin. Replaces string node with a component that render the markdown. */
|
|
41
84
|
const markdownStringPlugin = {
|
|
42
85
|
id: "markdown-string-plugin",
|
|
@@ -47,14 +90,14 @@ const markdownStringPlugin = {
|
|
|
47
90
|
plugins: [{
|
|
48
91
|
id: "markdown-metadata-plugin",
|
|
49
92
|
canHandle: (metadataNode) => typeof metadataNode === "string" || typeof metadataNode === "number" || typeof metadataNode === "boolean" || !metadataNode,
|
|
50
|
-
transform: (metadataNode, props
|
|
51
|
-
...props
|
|
93
|
+
transform: (metadataNode, props) => require_IntlayerNode.renderIntlayerNode({
|
|
94
|
+
...props,
|
|
52
95
|
value: metadataNode,
|
|
53
96
|
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_editor_ContentSelectorWrapper.ContentSelectorRenderer, {
|
|
54
97
|
...rest,
|
|
55
98
|
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_markdown_MarkdownRendererPlugin.MarkdownMetadataRenderer, {
|
|
56
99
|
...rest,
|
|
57
|
-
metadataKeyPath: props
|
|
100
|
+
metadataKeyPath: props.keyPath,
|
|
58
101
|
children: node
|
|
59
102
|
})
|
|
60
103
|
})
|
|
@@ -87,7 +130,7 @@ const markdownStringPlugin = {
|
|
|
87
130
|
};
|
|
88
131
|
const markdownPlugin = {
|
|
89
132
|
id: "markdown-plugin",
|
|
90
|
-
canHandle: (node) => typeof node === "object" &&
|
|
133
|
+
canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types.NodeType.Markdown,
|
|
91
134
|
transform: (node, props, deepTransformNode) => {
|
|
92
135
|
const newKeyPath = [...props.keyPath, { type: _intlayer_types.NodeType.Markdown }];
|
|
93
136
|
const children = node[_intlayer_types.NodeType.Markdown];
|
|
@@ -102,9 +145,10 @@ const markdownPlugin = {
|
|
|
102
145
|
/** HTML plugin. Replaces node with a function that takes components => ReactNode. */
|
|
103
146
|
const htmlPlugin = {
|
|
104
147
|
id: "html-plugin",
|
|
105
|
-
canHandle: (node) => typeof node === "object" &&
|
|
148
|
+
canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types.NodeType.HTML,
|
|
106
149
|
transform: (node, props) => {
|
|
107
150
|
const html = node[_intlayer_types.NodeType.HTML];
|
|
151
|
+
node.tags;
|
|
108
152
|
const { plugins, ...rest } = props;
|
|
109
153
|
const render = (userComponents) => (0, react.createElement)(require_html_HTMLRendererPlugin.HTMLRendererPlugin, {
|
|
110
154
|
...rest,
|
|
@@ -122,6 +166,7 @@ const htmlPlugin = {
|
|
|
122
166
|
|
|
123
167
|
//#endregion
|
|
124
168
|
exports.htmlPlugin = htmlPlugin;
|
|
169
|
+
exports.insertionPlugin = insertionPlugin;
|
|
125
170
|
exports.intlayerNodePlugins = intlayerNodePlugins;
|
|
126
171
|
exports.markdownPlugin = markdownPlugin;
|
|
127
172
|
exports.markdownStringPlugin = markdownStringPlugin;
|
package/dist/cjs/plugins.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugins.cjs","names":["renderIntlayerNode","EditedContentRenderer","ContentSelectorRenderer","renderReactElement","props","MarkdownMetadataRenderer","MarkdownRendererPlugin","NodeType","HTMLRendererPlugin"],"sources":["../../src/plugins.tsx"],"sourcesContent":["import {\n type DeepTransformContent as DeepTransformContentCore,\n getMarkdownMetadata,\n type HTMLCond,\n type HTMLContent,\n type IInterpreterPluginState as IInterpreterPluginStateCore,\n type MarkdownContent,\n type Plugins,\n} from '@intlayer/core';\nimport {\n type DeclaredLocales,\n type KeyPath,\n type LocalesValues,\n NodeType,\n} from '@intlayer/types';\nimport { createElement, type ReactElement, type ReactNode } from 'react';\nimport { ContentSelectorRenderer } from './editor';\nimport { EditedContentRenderer } from './editor/useEditedContentRenderer';\nimport { HTMLRendererPlugin } from './html';\nimport { type IntlayerNode, renderIntlayerNode } from './IntlayerNode';\nimport { MarkdownMetadataRenderer, MarkdownRendererPlugin } 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 <EditedContentRenderer {...rest}>{rest.children}</EditedContentRenderer>\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 const render = (components?: any) =>\n renderIntlayerNode({\n ...props,\n value: node,\n children: (\n <ContentSelectorRenderer {...rest}>\n <MarkdownRendererPlugin {...rest} {...components}>\n {node}\n </MarkdownRendererPlugin>\n </ContentSelectorRenderer>\n ),\n additionalProps: {\n metadata: metadataNodes,\n },\n });\n\n const element = render() as ReactElement;\n\n return new Proxy(element, {\n get(target, prop, receiver) {\n if (prop === 'value') {\n return node;\n }\n if (prop === 'metadata') {\n return metadataNodes;\n }\n\n if (prop === 'use') {\n return (components?: any) => render(components);\n }\n\n return Reflect.get(target, prop, receiver);\n },\n }) as any;\n },\n};\n\nexport type MarkdownCond<T, S, L extends LocalesValues> = T extends {\n nodeType: NodeType | string;\n [NodeType.Markdown]: infer M;\n metadata?: infer U;\n}\n ? {\n use: (components: any) => any;\n metadata: DeepTransformContent<U, L>;\n } & any\n : never;\n\nexport const markdownPlugin: Plugins = {\n id: 'markdown-plugin',\n canHandle: (node) =>\n typeof node === 'object' &&\n (node?.nodeType === NodeType.Markdown || node?.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/** ---------------------------------------------\n * HTML PLUGIN\n * --------------------------------------------- */\n\nexport type HTMLPluginCond<T, S, L> = HTMLCond<T, S, L>;\n\n/** HTML plugin. Replaces node with a function that takes components => ReactNode. */\nexport const htmlPlugin: Plugins = {\n id: 'html-plugin',\n canHandle: (node) =>\n typeof node === 'object' &&\n (node?.nodeType === NodeType.HTML || node?.nodeType === 'html'),\n\n transform: (node: HTMLContent, props) => {\n const html = node[NodeType.HTML];\n const { plugins, ...rest } = props;\n\n const render = (userComponents?: Record<string, any>): ReactNode =>\n createElement(HTMLRendererPlugin, { ...rest, html, userComponents });\n\n const element = render() as ReactElement;\n\n return new Proxy(element, {\n get(target, prop, receiver) {\n if (prop === 'value') {\n return html;\n }\n\n if (prop === 'use') {\n return (userComponents?: Record<string, any>) =>\n render(userComponents);\n }\n\n return Reflect.get(target, prop, receiver);\n },\n }) as any;\n },\n};\n\n/** ---------------------------------------------\n * PLUGINS RESULT\n * --------------------------------------------- */\n\nexport interface IInterpreterPluginReact<T, S, L extends LocalesValues> {\n reactNode: ReactNodeCond<T>;\n reactIntlayerNode: IntlayerNodeCond<T>;\n reactMarkdown: MarkdownCond<T, S, L>;\n reactHtml: HTMLPluginCond<T, S, L>;\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 reactIntlayerNode: true;\n reactMarkdown: true;\n reactHtml: true;\n};\n\nexport type DeepTransformContent<\n T,\n L extends LocalesValues = DeclaredLocales,\n> = DeepTransformContentCore<T, IInterpreterPluginState, L>;\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"plugins.cjs","names":["renderIntlayerNode","EditedContentRenderer","ContentSelectorRenderer","renderReactElement","Fragment","NodeType","MarkdownMetadataRenderer","MarkdownRendererPlugin","HTMLRendererPlugin"],"sources":["../../src/plugins.tsx"],"sourcesContent":["import {\n type DeepTransformContent as DeepTransformContentCore,\n getMarkdownMetadata,\n type HTMLCond,\n type HTMLContent,\n type IInterpreterPluginState as IInterpreterPluginStateCore,\n type InsertionContent,\n type MarkdownContent,\n type Plugins,\n splitInsertionTemplate,\n} from '@intlayer/core';\nimport {\n type DeclaredLocales,\n type KeyPath,\n type LocalesValues,\n NodeType,\n} from '@intlayer/types';\nimport {\n type ComponentType,\n createElement,\n type ElementType,\n Fragment,\n type ReactElement,\n type ReactNode,\n} from 'react';\nimport { ContentSelectorRenderer } from './editor';\nimport { EditedContentRenderer } from './editor/useEditedContentRenderer';\nimport { HTMLRendererPlugin } from './html';\nimport type { ReactHTMLComponentMap } from './html/types';\nimport { type IntlayerNode, renderIntlayerNode } from './IntlayerNode';\nimport { MarkdownMetadataRenderer, MarkdownRendererPlugin } 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 <EditedContentRenderer {...rest}>{rest.children}</EditedContentRenderer>\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 * INSERTION PLUGIN\n * --------------------------------------------- */\n\nexport type InsertionCond<T, S, L> = T extends {\n nodeType: NodeType | string;\n [NodeType.Insertion]: string;\n fields: readonly string[];\n}\n ? (\n values: {\n [K in T['fields'][number]]: ReactNode;\n }\n ) => ReactNode\n : never;\n\n/**\n * Split insertion string and join with React nodes using shared core logic\n */\nconst splitAndJoinInsertion = (\n template: string,\n values: Record<string, string | number | ReactNode>\n): ReactNode => {\n const result = splitInsertionTemplate(template, values);\n\n if (result.isSimple) {\n // Simple string replacement\n return result.parts as string;\n }\n\n // Return as Fragment with proper keys\n return createElement(\n Fragment,\n null,\n ...(result.parts as any[]).map((part, index) =>\n createElement(Fragment, { key: index }, part)\n )\n );\n};\n\n/** Insertion plugin for React. Handles component/node insertion. */\nexport const insertionPlugin: Plugins = {\n id: 'insertion-plugin',\n canHandle: (node) =>\n typeof node === 'object' && node?.nodeType === NodeType.Insertion,\n transform: (node: InsertionContent, props, deepTransformNode) => {\n const newKeyPath: KeyPath[] = [\n ...props.keyPath,\n {\n type: NodeType.Insertion,\n },\n ];\n\n const children = node[NodeType.Insertion];\n\n /** Insertion string plugin. Replaces string node with a component that render the insertion. */\n const insertionStringPlugin: Plugins = {\n id: 'insertion-string-plugin',\n canHandle: (node) => typeof node === 'string',\n transform: (node: string, subProps, deepTransformNode) => {\n const transformedResult = deepTransformNode(node, {\n ...subProps,\n children: node,\n plugins: [\n ...(props.plugins ?? ([] as Plugins[])).filter(\n (plugin) => plugin.id !== 'intlayer-node-plugin'\n ),\n ],\n });\n\n return (\n values: {\n [K in InsertionContent['fields'][number]]:\n | string\n | number\n | ReactNode;\n }\n ) => {\n const result = splitAndJoinInsertion(transformedResult, values);\n\n return deepTransformNode(result, {\n ...subProps,\n plugins: props.plugins,\n children: result,\n });\n };\n },\n };\n\n return deepTransformNode(children, {\n ...props,\n children,\n keyPath: newKeyPath,\n plugins: [insertionStringPlugin, ...(props.plugins ?? [])],\n });\n },\n};\n\n/**\n * MARKDOWN PLUGIN\n */\n\nexport type MarkdownStringCond<T> = T extends string\n ? IntlayerNode<\n string,\n { metadata: DeepTransformContent<string>; use: (components: any) => any }\n >\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 const render = (components?: any) =>\n renderIntlayerNode({\n ...props,\n value: node,\n children: (\n <ContentSelectorRenderer {...rest}>\n <MarkdownRendererPlugin {...rest} {...components}>\n {node}\n </MarkdownRendererPlugin>\n </ContentSelectorRenderer>\n ),\n additionalProps: {\n metadata: metadataNodes,\n },\n });\n\n const element = render() as ReactElement;\n\n return new Proxy(element, {\n get(target, prop, receiver) {\n if (prop === 'value') {\n return node;\n }\n if (prop === 'metadata') {\n return metadataNodes;\n }\n\n if (prop === 'use') {\n return (components?: any) => render(components);\n }\n\n return Reflect.get(target, prop, receiver);\n },\n }) as any;\n },\n};\n\ntype MarkdownComponentMap = Record<string, ComponentType<any> | ElementType>;\n\nexport type MarkdownCond<T, S, L extends LocalesValues> = T extends {\n nodeType: NodeType | string;\n [NodeType.Markdown]: infer M;\n metadata?: infer U;\n}\n ? {\n use: (components?: MarkdownComponentMap) => ReactNode;\n metadata: DeepTransformContent<U, L>;\n } & any\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/** ---------------------------------------------\n * HTML PLUGIN\n * --------------------------------------------- */\n\nexport type HTMLPluginCond<T, S, L> = HTMLCond<T, S, L>;\n\n/** HTML plugin. Replaces node with a function that takes components => ReactNode. */\nexport const htmlPlugin: Plugins = {\n id: 'html-plugin',\n canHandle: (node) =>\n typeof node === 'object' && node?.nodeType === NodeType.HTML,\n\n transform: (node: HTMLContent<string>, props) => {\n const html = node[NodeType.HTML];\n const tags = node.tags ?? [];\n const { plugins, ...rest } = props;\n\n // Type-safe render function that accepts properly typed components\n const render = <\n T = typeof tags extends readonly (infer U extends string)[]\n ? U\n : typeof tags,\n >(\n userComponents?: ReactHTMLComponentMap<T>\n ): ReactNode =>\n createElement(HTMLRendererPlugin, { ...rest, html, userComponents });\n\n const element = render() as ReactElement;\n\n return new Proxy(element, {\n get(target, prop, receiver) {\n if (prop === 'value') {\n return html;\n }\n\n if (prop === 'use') {\n // Return a properly typed function based on custom components\n return <\n T = typeof tags extends readonly (infer U extends string)[]\n ? U\n : typeof tags,\n >(\n userComponents?: ReactHTMLComponentMap<T>\n ) => render(userComponents);\n }\n\n return Reflect.get(target, prop, receiver);\n },\n }) as any;\n },\n};\n\n/** ---------------------------------------------\n * PLUGINS RESULT\n * --------------------------------------------- */\n\nexport interface IInterpreterPluginReact<T, S, L extends LocalesValues> {\n reactNode: ReactNodeCond<T>;\n reactIntlayerNode: IntlayerNodeCond<T>;\n reactInsertion: InsertionCond<T, S, L>;\n reactMarkdown: MarkdownCond<T, S, L>;\n reactHtml: HTMLPluginCond<T, S, L>;\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 reactIntlayerNode: true;\n reactInsertion: true;\n reactMarkdown: true;\n reactHtml: true;\n};\n\nexport type DeepTransformContent<\n T,\n L extends LocalesValues = DeclaredLocales,\n> = DeepTransformContentCore<T, IInterpreterPluginState, L>;\n"],"mappings":"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"}
|
|
@@ -4,10 +4,10 @@ let react = require("react");
|
|
|
4
4
|
//#region src/reactElement/renderReactElement.ts
|
|
5
5
|
const renderReactElement = (element) => {
|
|
6
6
|
if (typeof element === "string") return element;
|
|
7
|
-
const convertChildrenAsArray = (element
|
|
8
|
-
if (element
|
|
7
|
+
const convertChildrenAsArray = (element) => {
|
|
8
|
+
if (element?.props && typeof element.props.children === "object") {
|
|
9
9
|
const childrenResult = [];
|
|
10
|
-
const { children } = element
|
|
10
|
+
const { children } = element.props;
|
|
11
11
|
Object.keys(children ?? {}).forEach((key) => {
|
|
12
12
|
const childElement = renderReactElement(children?.[key]);
|
|
13
13
|
if (typeof childElement === "object" && childElement !== null && "type" in childElement) childrenResult.push((0, react.createElement)(childElement.type, {
|
|
@@ -17,18 +17,18 @@ const renderReactElement = (element) => {
|
|
|
17
17
|
else childrenResult.push(childElement);
|
|
18
18
|
});
|
|
19
19
|
return {
|
|
20
|
-
...element
|
|
20
|
+
...element,
|
|
21
21
|
props: {
|
|
22
|
-
...element
|
|
22
|
+
...element.props,
|
|
23
23
|
children: childrenResult
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
27
|
return {
|
|
28
|
-
...element
|
|
28
|
+
...element,
|
|
29
29
|
props: {
|
|
30
|
-
...element
|
|
31
|
-
children: element
|
|
30
|
+
...element.props,
|
|
31
|
+
children: element.props?.children ?? []
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
34
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderReactElement.cjs","names":[
|
|
1
|
+
{"version":3,"file":"renderReactElement.cjs","names":[],"sources":["../../../src/reactElement/renderReactElement.ts"],"sourcesContent":["import { createElement, type ReactElement, type ReactNode } from 'react';\n\n// This function recursively creates React elements from a given JSON-like structure\nexport const renderReactElement = (element: ReactElement<any>) => {\n if (typeof element === 'string') {\n // If it's a string, simply return it (used for text content)\n return element;\n }\n\n const convertChildrenAsArray = (\n element: ReactElement<{ children?: ReactNode }>\n ): ReactElement<{ children?: ReactNode }> => {\n if (element?.props && typeof element.props.children === 'object') {\n const childrenResult: ReactNode[] = [];\n const { children } = element.props;\n\n // Create the children elements recursively, if any\n Object.keys(children ?? {}).forEach((key) => {\n const childElement = renderReactElement(\n children?.[\n key as keyof typeof children\n ] as unknown as ReactElement<any>\n );\n\n // Add key prop if the child is a React element\n if (\n typeof childElement === 'object' &&\n childElement !== null &&\n 'type' in childElement\n ) {\n childrenResult.push(\n createElement(\n childElement.type,\n { ...childElement.props, key },\n ...(Array.isArray(childElement.props?.children)\n ? childElement.props.children\n : [childElement.props?.children])\n )\n );\n } else {\n childrenResult.push(childElement);\n }\n });\n\n return {\n ...element,\n props: { ...element.props, children: childrenResult },\n };\n }\n\n return {\n ...element,\n props: { ...element.props, children: element.props?.children ?? [] },\n };\n };\n\n const fixedElement = convertChildrenAsArray(\n element as ReactElement<{ children?: ReactNode }>\n );\n\n const { type, props } = fixedElement;\n\n // Create and return the React element\n return createElement(\n type ?? 'span',\n props,\n ...(props.children as ReactNode[])\n );\n};\n"],"mappings":";;;;AAGA,MAAa,sBAAsB,YAA+B;AAChE,KAAI,OAAO,YAAY,SAErB,QAAO;CAGT,MAAM,0BACJ,YAC2C;AAC3C,MAAI,SAAS,SAAS,OAAO,QAAQ,MAAM,aAAa,UAAU;GAChE,MAAM,iBAA8B,EAAE;GACtC,MAAM,EAAE,aAAa,QAAQ;AAG7B,UAAO,KAAK,YAAY,EAAE,CAAC,CAAC,SAAS,QAAQ;IAC3C,MAAM,eAAe,mBACnB,WACE,KAEH;AAGD,QACE,OAAO,iBAAiB,YACxB,iBAAiB,QACjB,UAAU,aAEV,gBAAe,8BAEX,aAAa,MACb;KAAE,GAAG,aAAa;KAAO;KAAK,EAC9B,GAAI,MAAM,QAAQ,aAAa,OAAO,SAAS,GAC3C,aAAa,MAAM,WACnB,CAAC,aAAa,OAAO,SAAS,CACnC,CACF;QAED,gBAAe,KAAK,aAAa;KAEnC;AAEF,UAAO;IACL,GAAG;IACH,OAAO;KAAE,GAAG,QAAQ;KAAO,UAAU;KAAgB;IACtD;;AAGH,SAAO;GACL,GAAG;GACH,OAAO;IAAE,GAAG,QAAQ;IAAO,UAAU,QAAQ,OAAO,YAAY,EAAE;IAAE;GACrE;;CAOH,MAAM,EAAE,MAAM,UAJO,uBACnB,QACD;AAKD,iCACE,QAAQ,QACR,OACA,GAAI,MAAM,SACX"}
|
|
@@ -21,8 +21,8 @@ const locale = require_server_serverContext.getServerContext(IntlayerServerConte
|
|
|
21
21
|
/**
|
|
22
22
|
* Provider that store the current locale on the server side
|
|
23
23
|
*/
|
|
24
|
-
const IntlayerServerProvider = ({ children, locale
|
|
25
|
-
value: locale
|
|
24
|
+
const IntlayerServerProvider = ({ children, locale = defaultLocale }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(IntlayerServerContext.Provider, {
|
|
25
|
+
value: locale,
|
|
26
26
|
children
|
|
27
27
|
});
|
|
28
28
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntlayerServerProvider.cjs","names":["configuration","createServerContext","getServerContext"
|
|
1
|
+
{"version":3,"file":"IntlayerServerProvider.cjs","names":["configuration","createServerContext","getServerContext"],"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types';\n\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\nconst { defaultLocale } = configuration?.internationalization ?? {};\n\n/**\n * Context that store the current locale on the server side\n */\nexport const IntlayerServerContext =\n createServerContext<LocalesValues>(defaultLocale);\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayer = () => getServerContext(IntlayerServerContext);\n\n/**\n * Get the current locale\n */\nexport const locale = getServerContext(IntlayerServerContext);\n\nexport type IntlayerServerProviderProps = PropsWithChildren & {\n locale?: LocalesValues;\n};\n\n/**\n * Provider that store the current locale on the server side\n */\nexport const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({\n children,\n locale = defaultLocale,\n}) => (\n <IntlayerServerContext.Provider value={locale}>\n {children}\n </IntlayerServerContext.Provider>\n);\n"],"mappings":";;;;;;;AAMA,MAAM,EAAE,kBAAkBA,gCAAe,wBAAwB,EAAE;;;;AAKnE,MAAa,wBACXC,iDAAmC,cAAc;;;;AAKnD,MAAa,oBAAoBC,8CAAiB,sBAAsB;;;;AAKxE,MAAa,SAASA,8CAAiB,sBAAsB;;;;AAS7D,MAAa,0BAA2D,EACtE,UACA,SAAS,oBAET,2CAAC,sBAAsB;CAAS,OAAO;CACpC;EAC8B"}
|
|
@@ -36,14 +36,14 @@ const useLocale = ({ isCookieEnabled, onLocaleChange } = {}) => {
|
|
|
36
36
|
locale,
|
|
37
37
|
defaultLocale,
|
|
38
38
|
availableLocales,
|
|
39
|
-
setLocale: useCallback((locale
|
|
40
|
-
if (!availableLocales?.map(String).includes(locale
|
|
41
|
-
console.error(`Locale ${locale
|
|
39
|
+
setLocale: useCallback((locale) => {
|
|
40
|
+
if (!availableLocales?.map(String).includes(locale)) {
|
|
41
|
+
console.error(`Locale ${locale} is not available`);
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
|
-
setLocaleState(locale
|
|
45
|
-
setLocaleInStorage(locale
|
|
46
|
-
onLocaleChange?.(locale
|
|
44
|
+
setLocaleState(locale);
|
|
45
|
+
setLocaleInStorage(locale, isCookieEnabled ?? isCookieEnabledContext ?? true);
|
|
46
|
+
onLocaleChange?.(locale);
|
|
47
47
|
}, [
|
|
48
48
|
availableLocales,
|
|
49
49
|
onLocaleChange,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLocale.mjs","names":[
|
|
1
|
+
{"version":3,"file":"useLocale.mjs","names":[],"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport type { DeclaredLocales, LocalesValues } from '@intlayer/types';\nimport { useCallback, useContext } from 'react';\nimport { IntlayerClientContext } from './IntlayerProvider';\nimport { setLocaleInStorage } from './useLocaleStorage';\n\ntype UseLocaleProps = {\n isCookieEnabled?: boolean;\n onLocaleChange?: (locale: LocalesValues) => void;\n};\n\ntype UseLocaleResult = {\n locale: DeclaredLocales;\n defaultLocale: DeclaredLocales;\n availableLocales: DeclaredLocales[];\n setLocale: (locale: LocalesValues) => void;\n};\n\n/**\n * Client-side hook to get the current locale and related locale management functions.\n *\n * @param props - Optional properties for the hook.\n * @returns An object containing the current locale, default locale, available locales, and a function to update the locale.\n *\n * @example\n * ```tsx\n * import { useLocale } from 'react-intlayer';\n *\n * const LocaleSwitcher = () => {\n * const { locale, setLocale, availableLocales } = useLocale();\n *\n * return (\n * <select value={locale} onChange={(e) => setLocale(e.target.value)}>\n * {availableLocales.map((loc) => (\n * <option key={loc} value={loc}>{loc}</option>\n * ))}\n * </select>\n * );\n * };\n * ```\n */\nexport const useLocale = ({\n isCookieEnabled,\n onLocaleChange,\n}: UseLocaleProps = {}): UseLocaleResult => {\n const { defaultLocale, locales: availableLocales } =\n configuration?.internationalization ?? {};\n\n const {\n locale,\n setLocale: setLocaleState,\n isCookieEnabled: isCookieEnabledContext,\n } = useContext(IntlayerClientContext);\n\n const setLocale = useCallback(\n (locale: LocalesValues) => {\n if (!availableLocales?.map(String).includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleState(locale);\n setLocaleInStorage(\n locale,\n isCookieEnabled ?? isCookieEnabledContext ?? true\n );\n onLocaleChange?.(locale);\n },\n [availableLocales, onLocaleChange, setLocaleState, isCookieEnabled]\n );\n\n return {\n locale, // Current locale\n defaultLocale, // Principal locale defined in config\n availableLocales, // List of the available locales defined in config\n setLocale, // Function to set the locale\n } as UseLocaleResult;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAa,aAAa,EACxB,iBACA,mBACkB,EAAE,KAAsB;CAC1C,MAAM,EAAE,eAAe,SAAS,qBAC9B,eAAe,wBAAwB,EAAE;CAE3C,MAAM,EACJ,QACA,WAAW,gBACX,iBAAiB,2BACf,WAAW,sBAAsB;AAmBrC,QAAO;EACL;EACA;EACA;EACA,WArBgB,aACf,WAA0B;AACzB,OAAI,CAAC,kBAAkB,IAAI,OAAO,CAAC,SAAS,OAAO,EAAE;AACnD,YAAQ,MAAM,UAAU,OAAO,mBAAmB;AAClD;;AAGF,kBAAe,OAAO;AACtB,sBACE,QACA,mBAAmB,0BAA0B,KAC9C;AACD,oBAAiB,OAAO;KAE1B;GAAC;GAAkB;GAAgB;GAAgB;GAAgB,CACpE;EAOA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { htmlPlugin, intlayerNodePlugins, markdownPlugin, reactNodePlugins } from "./plugins.mjs";
|
|
1
|
+
import { htmlPlugin, insertionPlugin, intlayerNodePlugins, markdownPlugin, reactNodePlugins } from "./plugins.mjs";
|
|
2
2
|
import { getDictionary as getDictionary$1 } from "@intlayer/core";
|
|
3
3
|
|
|
4
4
|
//#region src/getDictionary.ts
|
|
@@ -6,6 +6,7 @@ const getDictionary = (dictionary, locale, additionalPlugins) => {
|
|
|
6
6
|
return getDictionary$1(dictionary, locale, [
|
|
7
7
|
intlayerNodePlugins,
|
|
8
8
|
reactNodePlugins,
|
|
9
|
+
insertionPlugin,
|
|
9
10
|
markdownPlugin,
|
|
10
11
|
htmlPlugin,
|
|
11
12
|
...additionalPlugins ?? []
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDictionary.mjs","names":["getDictionaryCore"],"sources":["../../src/getDictionary.ts"],"sourcesContent":["import {\n getDictionary as getDictionaryCore,\n type Plugins,\n} from '@intlayer/core';\nimport type {\n DeclaredLocales,\n Dictionary,\n LocalesValues,\n} from '@intlayer/types';\nimport {\n type DeepTransformContent,\n htmlPlugin,\n intlayerNodePlugins,\n markdownPlugin,\n reactNodePlugins,\n} from './plugins';\n\nexport const getDictionary = <\n T extends Dictionary,\n L extends LocalesValues = DeclaredLocales,\n>(\n dictionary: T,\n locale?: L,\n additionalPlugins?: Plugins[]\n): DeepTransformContent<T['content'], L> => {\n const plugins: Plugins[] = [\n intlayerNodePlugins,\n reactNodePlugins,\n markdownPlugin,\n htmlPlugin,\n ...(additionalPlugins ?? []),\n ];\n\n return getDictionaryCore(dictionary, locale, plugins) as any;\n};\n"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"getDictionary.mjs","names":["getDictionaryCore"],"sources":["../../src/getDictionary.ts"],"sourcesContent":["import {\n getDictionary as getDictionaryCore,\n type Plugins,\n} from '@intlayer/core';\nimport type {\n DeclaredLocales,\n Dictionary,\n LocalesValues,\n} from '@intlayer/types';\nimport {\n type DeepTransformContent,\n htmlPlugin,\n insertionPlugin,\n intlayerNodePlugins,\n markdownPlugin,\n reactNodePlugins,\n} from './plugins';\n\nexport const getDictionary = <\n T extends Dictionary,\n L extends LocalesValues = DeclaredLocales,\n>(\n dictionary: T,\n locale?: L,\n additionalPlugins?: Plugins[]\n): DeepTransformContent<T['content'], L> => {\n const plugins: Plugins[] = [\n intlayerNodePlugins,\n reactNodePlugins,\n insertionPlugin,\n markdownPlugin,\n htmlPlugin,\n ...(additionalPlugins ?? []),\n ];\n\n return getDictionaryCore(dictionary, locale, plugins) as any;\n};\n"],"mappings":";;;;AAkBA,MAAa,iBAIX,YACA,QACA,sBAC0C;AAU1C,QAAOA,gBAAkB,YAAY,QATV;EACzB;EACA;EACA;EACA;EACA;EACA,GAAI,qBAAqB,EAAE;EAC5B,CAEoD"}
|
package/dist/esm/getIntlayer.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { htmlPlugin, intlayerNodePlugins, markdownPlugin, reactNodePlugins } from "./plugins.mjs";
|
|
1
|
+
import { htmlPlugin, insertionPlugin, intlayerNodePlugins, markdownPlugin, reactNodePlugins } from "./plugins.mjs";
|
|
2
2
|
import { getIntlayer as getIntlayer$1 } from "@intlayer/core";
|
|
3
3
|
|
|
4
4
|
//#region src/getIntlayer.ts
|
|
@@ -6,6 +6,7 @@ const getIntlayer = (key, locale, additionalPlugins) => {
|
|
|
6
6
|
return getIntlayer$1(key, locale, [
|
|
7
7
|
intlayerNodePlugins,
|
|
8
8
|
reactNodePlugins,
|
|
9
|
+
insertionPlugin,
|
|
9
10
|
markdownPlugin,
|
|
10
11
|
htmlPlugin,
|
|
11
12
|
...additionalPlugins ?? []
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getIntlayer.mjs","names":["getIntlayerCore"],"sources":["../../src/getIntlayer.ts"],"sourcesContent":["import { getIntlayer as getIntlayerCore, type Plugins } from '@intlayer/core';\nimport type {\n DeclaredLocales,\n DictionaryKeys,\n DictionaryRegistryContent,\n LocalesValues,\n} from '@intlayer/types';\nimport {\n type DeepTransformContent,\n htmlPlugin,\n intlayerNodePlugins,\n markdownPlugin,\n reactNodePlugins,\n} from './plugins';\n\nexport const getIntlayer = <\n T extends DictionaryKeys,\n L extends LocalesValues = DeclaredLocales,\n>(\n key: T,\n locale?: L,\n additionalPlugins?: Plugins[]\n): DeepTransformContent<DictionaryRegistryContent<T>, L> => {\n const plugins: Plugins[] = [\n intlayerNodePlugins,\n reactNodePlugins,\n markdownPlugin,\n htmlPlugin,\n ...(additionalPlugins ?? []),\n ];\n\n return getIntlayerCore(key, locale, plugins) as any;\n};\n"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"getIntlayer.mjs","names":["getIntlayerCore"],"sources":["../../src/getIntlayer.ts"],"sourcesContent":["import { getIntlayer as getIntlayerCore, type Plugins } from '@intlayer/core';\nimport type {\n DeclaredLocales,\n DictionaryKeys,\n DictionaryRegistryContent,\n LocalesValues,\n} from '@intlayer/types';\nimport {\n type DeepTransformContent,\n htmlPlugin,\n insertionPlugin,\n intlayerNodePlugins,\n markdownPlugin,\n reactNodePlugins,\n} from './plugins';\n\nexport const getIntlayer = <\n T extends DictionaryKeys,\n L extends LocalesValues = DeclaredLocales,\n>(\n key: T,\n locale?: L,\n additionalPlugins?: Plugins[]\n): DeepTransformContent<DictionaryRegistryContent<T>, L> => {\n const plugins: Plugins[] = [\n intlayerNodePlugins,\n reactNodePlugins,\n insertionPlugin,\n markdownPlugin,\n htmlPlugin,\n ...(additionalPlugins ?? []),\n ];\n\n return getIntlayerCore(key, locale, plugins) as any;\n};\n"],"mappings":";;;;AAgBA,MAAa,eAIX,KACA,QACA,sBAC0D;AAU1D,QAAOA,cAAgB,KAAK,QATD;EACzB;EACA;EACA;EACA;EACA;EACA,GAAI,qBAAqB,EAAE;EAC5B,CAE2C"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useHTMLContext } from "./HTMLProvider.mjs";
|
|
4
|
+
import { Fragment, createElement } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
import { HTML_TAGS, getHTML } from "@intlayer/core";
|
|
7
|
+
|
|
8
|
+
//#region src/html/HTMLRenderer.tsx
|
|
9
|
+
const createDefaultHTMLComponents = () => {
|
|
10
|
+
const components = {};
|
|
11
|
+
for (const tag of HTML_TAGS) components[tag] = ({ children, ...props }) => createElement(tag, props, children);
|
|
12
|
+
return components;
|
|
13
|
+
};
|
|
14
|
+
const defaultHTMLComponents = createDefaultHTMLComponents();
|
|
15
|
+
/**
|
|
16
|
+
* Renders HTML-like content to JSX with the provided components.
|
|
17
|
+
*
|
|
18
|
+
* This function does not use context from HTMLProvider. Use `useHTMLRenderer`
|
|
19
|
+
* hook if you want to leverage provider context.
|
|
20
|
+
*/
|
|
21
|
+
const renderHTML = (content, { components } = {}) => {
|
|
22
|
+
const mergedComponents = {
|
|
23
|
+
...defaultHTMLComponents,
|
|
24
|
+
...components
|
|
25
|
+
};
|
|
26
|
+
return /* @__PURE__ */ jsx(Fragment, { children: getHTML(content, Object.fromEntries(Object.entries(mergedComponents).filter(([, Component]) => Component).map(([key, Component]) => [key, (props) => createElement(Component, props)]))) });
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Hook that returns a function to render HTML content.
|
|
30
|
+
*
|
|
31
|
+
* This hook considers the configuration from the `HTMLProvider` context if available,
|
|
32
|
+
* falling back to the provided components.
|
|
33
|
+
*/
|
|
34
|
+
const useHTMLRenderer = ({ components } = {}) => {
|
|
35
|
+
const context = useHTMLContext();
|
|
36
|
+
return (content) => {
|
|
37
|
+
return renderHTML(content, { components: {
|
|
38
|
+
...context?.components,
|
|
39
|
+
...components
|
|
40
|
+
} });
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* React component that renders HTML-like content to JSX.
|
|
45
|
+
*
|
|
46
|
+
* This component uses the components from the `HTMLProvider` context
|
|
47
|
+
* if available.
|
|
48
|
+
*/
|
|
49
|
+
const HTMLRenderer = ({ children = "", components }) => {
|
|
50
|
+
return useHTMLRenderer({ components })(children);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { HTMLRenderer, defaultHTMLComponents, renderHTML, useHTMLRenderer };
|
|
55
|
+
//# sourceMappingURL=HTMLRenderer.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HTMLRenderer.mjs","names":[],"sources":["../../../src/html/HTMLRenderer.tsx"],"sourcesContent":["'use client';\n\nimport { getHTML, HTML_TAGS } from '@intlayer/core';\nimport { createElement, type FC, Fragment, type JSX } from 'react';\nimport { useHTMLContext } from './HTMLProvider';\nimport type { ReactHTMLComponent } from './types';\n\n/**\n * Type for React HTML tag components.\n */\ntype HTMLTagComponent = ReactHTMLComponent;\n\nconst createDefaultHTMLComponents = (): Record<string, HTMLTagComponent> => {\n const components: Record<string, HTMLTagComponent> = {};\n\n for (const tag of HTML_TAGS) {\n components[tag] = ({ children, ...props }) =>\n createElement(tag, props, children);\n }\n\n return components;\n};\n\nexport const defaultHTMLComponents = createDefaultHTMLComponents();\n\nexport type RenderHTMLProps = {\n /**\n * Component overrides for HTML tags.\n * Allows you to customize how specific HTML elements are rendered.\n */\n components?: Record<string, any>;\n};\n\n/**\n * Renders HTML-like content to JSX with the provided components.\n *\n * This function does not use context from HTMLProvider. Use `useHTMLRenderer`\n * hook if you want to leverage provider context.\n */\nexport const renderHTML = (\n content: string,\n { components }: RenderHTMLProps = {}\n): JSX.Element => {\n const mergedComponents = {\n ...defaultHTMLComponents,\n ...components,\n };\n\n // Wrap all components to ensure they are rendered via React.createElement\n const wrappedComponents = Object.fromEntries(\n Object.entries(mergedComponents)\n .filter(([, Component]) => Component)\n .map(([key, Component]) => [\n key,\n (props: any) => createElement(Component as any, props),\n ])\n );\n\n return <Fragment>{getHTML(content, wrappedComponents)}</Fragment>;\n};\n\n/**\n * Hook that returns a function to render HTML content.\n *\n * This hook considers the configuration from the `HTMLProvider` context if available,\n * falling back to the provided components.\n */\nexport const useHTMLRenderer = ({ components }: RenderHTMLProps = {}) => {\n const context = useHTMLContext();\n\n return (content: string) => {\n return renderHTML(content, {\n components: {\n ...context?.components,\n ...components,\n },\n });\n };\n};\n\nexport type HTMLRendererProps = RenderHTMLProps & {\n /**\n * The HTML content to render as a string.\n */\n children: string;\n};\n\n/**\n * React component that renders HTML-like content to JSX.\n *\n * This component uses the components from the `HTMLProvider` context\n * if available.\n */\nexport const HTMLRenderer: FC<HTMLRendererProps> = ({\n children = '',\n components,\n}) => {\n const render = useHTMLRenderer({ components });\n\n return render(children);\n};\n"],"mappings":";;;;;;;;AAYA,MAAM,oCAAsE;CAC1E,MAAM,aAA+C,EAAE;AAEvD,MAAK,MAAM,OAAO,UAChB,YAAW,QAAQ,EAAE,UAAU,GAAG,YAChC,cAAc,KAAK,OAAO,SAAS;AAGvC,QAAO;;AAGT,MAAa,wBAAwB,6BAA6B;;;;;;;AAgBlE,MAAa,cACX,SACA,EAAE,eAAgC,EAAE,KACpB;CAChB,MAAM,mBAAmB;EACvB,GAAG;EACH,GAAG;EACJ;AAYD,QAAO,oBAAC,sBAAU,QAAQ,SATA,OAAO,YAC/B,OAAO,QAAQ,iBAAiB,CAC7B,QAAQ,GAAG,eAAe,UAAU,CACpC,KAAK,CAAC,KAAK,eAAe,CACzB,MACC,UAAe,cAAc,WAAkB,MAAM,CACvD,CAAC,CACL,CAEoD,GAAY;;;;;;;;AASnE,MAAa,mBAAmB,EAAE,eAAgC,EAAE,KAAK;CACvE,MAAM,UAAU,gBAAgB;AAEhC,SAAQ,YAAoB;AAC1B,SAAO,WAAW,SAAS,EACzB,YAAY;GACV,GAAG,SAAS;GACZ,GAAG;GACJ,EACF,CAAC;;;;;;;;;AAiBN,MAAa,gBAAuC,EAClD,WAAW,IACX,iBACI;AAGJ,QAFe,gBAAgB,EAAE,YAAY,CAAC,CAEhC,SAAS"}
|
|
@@ -3,17 +3,12 @@
|
|
|
3
3
|
import { ContentSelectorRenderer } from "../editor/ContentSelectorWrapper.mjs";
|
|
4
4
|
import { useEditedContentRenderer } from "../editor/useEditedContentRenderer.mjs";
|
|
5
5
|
import { useHTMLContext } from "./HTMLProvider.mjs";
|
|
6
|
+
import { defaultHTMLComponents } from "./HTMLRenderer.mjs";
|
|
6
7
|
import { createElement } from "react";
|
|
7
8
|
import { jsx } from "react/jsx-runtime";
|
|
8
|
-
import {
|
|
9
|
+
import { getHTML } from "@intlayer/core";
|
|
9
10
|
|
|
10
11
|
//#region src/html/HTMLRendererPlugin.tsx
|
|
11
|
-
const createDefaultHTMLComponents = () => {
|
|
12
|
-
const components = {};
|
|
13
|
-
for (const tag of HTML_TAGS) components[tag] = ({ children, ...props }) => createElement(tag, props, children);
|
|
14
|
-
return components;
|
|
15
|
-
};
|
|
16
|
-
const defaultHTMLComponents = createDefaultHTMLComponents();
|
|
17
12
|
const HTMLRendererPlugin = (props) => {
|
|
18
13
|
const { dictionaryKey, keyPath, html, userComponents } = props;
|
|
19
14
|
const globalComponents = useHTMLContext()?.components || {};
|
|
@@ -28,7 +23,7 @@ const HTMLRendererPlugin = (props) => {
|
|
|
28
23
|
...globalComponents,
|
|
29
24
|
...userComponents
|
|
30
25
|
};
|
|
31
|
-
const wrappedComponents = Object.fromEntries(Object.entries(mergedComponents).map(([key, Component]) => [key, (props
|
|
26
|
+
const wrappedComponents = Object.fromEntries(Object.entries(mergedComponents).filter(([, Component]) => Component).map(([key, Component]) => [key, (props) => createElement(Component, props)]));
|
|
32
27
|
return /* @__PURE__ */ jsx(ContentSelectorRenderer, {
|
|
33
28
|
...props,
|
|
34
29
|
children: getHTML(contentToRender, wrappedComponents)
|
|
@@ -36,5 +31,5 @@ const HTMLRendererPlugin = (props) => {
|
|
|
36
31
|
};
|
|
37
32
|
|
|
38
33
|
//#endregion
|
|
39
|
-
export { HTMLRendererPlugin
|
|
34
|
+
export { HTMLRendererPlugin };
|
|
40
35
|
//# sourceMappingURL=HTMLRendererPlugin.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HTMLRendererPlugin.mjs","names":[
|
|
1
|
+
{"version":3,"file":"HTMLRendererPlugin.mjs","names":[],"sources":["../../../src/html/HTMLRendererPlugin.tsx"],"sourcesContent":["'use client';\n\nimport { getHTML } from '@intlayer/core';\nimport type { KeyPath, Locale } from '@intlayer/types';\nimport { createElement, type FC, type ReactNode } from 'react';\nimport { ContentSelectorRenderer } from '../editor';\nimport { useEditedContentRenderer } from '../editor/useEditedContentRenderer';\nimport { useHTMLContext } from './HTMLProvider';\nimport { defaultHTMLComponents } from './HTMLRenderer';\nimport type { ReactComponentProps } from './types';\n\ntype HTMLRendererPluginProps = {\n dictionaryKey: string;\n keyPath: KeyPath[];\n html: string;\n locale?: Locale;\n userComponents?: Record<string, any>;\n};\n\nexport const HTMLRendererPlugin: FC<HTMLRendererPluginProps> = (\n props\n): ReactNode => {\n const { dictionaryKey, keyPath, html, userComponents } = props;\n const context = useHTMLContext();\n const globalComponents = context?.components || {};\n\n const editedContentContext = useEditedContentRenderer({\n dictionaryKey: dictionaryKey ?? '',\n keyPath: keyPath ?? [],\n children: html,\n });\n\n const contentToRender =\n typeof editedContentContext === 'string' ? editedContentContext : html;\n\n const mergedComponents = {\n ...defaultHTMLComponents,\n ...globalComponents,\n ...userComponents,\n };\n\n // Wrap all components to ensure they are rendered via React.createElement\n // This is important because it allows React to handle the component's lifecycle,\n // hooks, and Babel-injected variables correctly.\n const wrappedComponents = Object.fromEntries(\n Object.entries(mergedComponents)\n .filter(([, Component]) => Component)\n .map(([key, Component]) => [\n key,\n (props: ReactComponentProps) => createElement(Component, props),\n ])\n );\n\n return (\n <ContentSelectorRenderer {...props}>\n {getHTML(contentToRender, wrappedComponents)}\n </ContentSelectorRenderer>\n );\n};\n"],"mappings":";;;;;;;;;;;AAmBA,MAAa,sBACX,UACc;CACd,MAAM,EAAE,eAAe,SAAS,MAAM,mBAAmB;CAEzD,MAAM,mBADU,gBAAgB,EACE,cAAc,EAAE;CAElD,MAAM,uBAAuB,yBAAyB;EACpD,eAAe,iBAAiB;EAChC,SAAS,WAAW,EAAE;EACtB,UAAU;EACX,CAAC;CAEF,MAAM,kBACJ,OAAO,yBAAyB,WAAW,uBAAuB;CAEpE,MAAM,mBAAmB;EACvB,GAAG;EACH,GAAG;EACH,GAAG;EACJ;CAKD,MAAM,oBAAoB,OAAO,YAC/B,OAAO,QAAQ,iBAAiB,CAC7B,QAAQ,GAAG,eAAe,UAAU,CACpC,KAAK,CAAC,KAAK,eAAe,CACzB,MACC,UAA+B,cAAc,WAAW,MAAM,CAChE,CAAC,CACL;AAED,QACE,oBAAC;EAAwB,GAAI;YAC1B,QAAQ,iBAAiB,kBAAkB;GACpB"}
|
package/dist/esm/html/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HTMLProvider, useHTMLContext } from "./HTMLProvider.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { HTMLRenderer, defaultHTMLComponents, renderHTML, useHTMLRenderer } from "./HTMLRenderer.mjs";
|
|
3
|
+
import { HTMLRendererPlugin } from "./HTMLRendererPlugin.mjs";
|
|
3
4
|
|
|
4
|
-
export { HTMLProvider, HTMLRendererPlugin, defaultHTMLComponents, useHTMLContext };
|
|
5
|
+
export { HTMLProvider, HTMLRenderer, HTMLRendererPlugin, defaultHTMLComponents, renderHTML, useHTMLContext, useHTMLRenderer };
|
|
File without changes
|
package/dist/esm/index.mjs
CHANGED
|
@@ -11,9 +11,10 @@ import { useIntlayer } from "./client/useIntlayer.mjs";
|
|
|
11
11
|
import { useLocale } from "./client/useLocale.mjs";
|
|
12
12
|
import { useLocaleBase } from "./client/useLocaleBase.mjs";
|
|
13
13
|
import { HTMLProvider } from "./html/HTMLProvider.mjs";
|
|
14
|
+
import { HTMLRenderer, renderHTML, useHTMLRenderer } from "./html/HTMLRenderer.mjs";
|
|
14
15
|
import { MarkdownProvider } from "./markdown/MarkdownProvider.mjs";
|
|
15
|
-
import { MarkdownRenderer } from "./markdown/MarkdownRenderer.mjs";
|
|
16
|
+
import { MarkdownRenderer, renderMarkdown, useMarkdownRenderer } from "./markdown/MarkdownRenderer.mjs";
|
|
16
17
|
import { getDictionary } from "./getDictionary.mjs";
|
|
17
18
|
import { useIntl } from "./client/format/useIntl.mjs";
|
|
18
19
|
|
|
19
|
-
export { HTMLProvider, IntlayerClientContext, IntlayerProvider, IntlayerProviderContent, MarkdownProvider, MarkdownRenderer, getDictionary, getIntlayer, localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, t, useDictionary, useDictionaryAsync, useDictionaryDynamic, useI18n, useIntl, useIntlayer, useIntlayerContext, useLoadDynamic, useLocale, useLocaleBase, useLocaleCookie, useLocaleStorage };
|
|
20
|
+
export { HTMLProvider, HTMLRenderer, IntlayerClientContext, IntlayerProvider, IntlayerProviderContent, MarkdownProvider, MarkdownRenderer, getDictionary, getIntlayer, localeCookie, localeInStorage, renderHTML, renderMarkdown, setLocaleCookie, setLocaleInStorage, t, useDictionary, useDictionaryAsync, useDictionaryDynamic, useHTMLRenderer, useI18n, useIntl, useIntlayer, useIntlayerContext, useLoadDynamic, useLocale, useLocaleBase, useLocaleCookie, useLocaleStorage, useMarkdownRenderer };
|