49 lines
2.4 KiB
TypeScript
49 lines
2.4 KiB
TypeScript
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical';
|
|
import type { JSXConvertersFunction } from '@payloadcms/richtext-lexical/react';
|
|
export type RenderRichTextProps = {
|
|
/** Wrapper class (e.g. Tailwind prose classes). */
|
|
className?: string;
|
|
/**
|
|
* Override the JSX for specific node types to apply the project's own
|
|
* styling. Receives Payload's defaultConverters; return the merged map.
|
|
* This is where a project puts its Tailwind classes per node (headings,
|
|
* paragraphs, links, …) — the design stays in the project, the parsing in
|
|
* the plugin's wrapper over Payload's official converter.
|
|
*/
|
|
converters?: JSXConvertersFunction;
|
|
/**
|
|
* Lexical editor state from a Payload richText field. Already localized —
|
|
* Payload returns the current locale's value, so NO per-locale key handling
|
|
* is needed here (unlike hand-rolled renderers that hardcode `.pl`).
|
|
*/
|
|
data: null | SerializedEditorState | undefined;
|
|
};
|
|
/**
|
|
* Renders a Payload Lexical richText field to React — a thin wrapper over
|
|
* Payload's OFFICIAL converter (@payloadcms/richtext-lexical/react).
|
|
*
|
|
* Why not hand-roll it: parsing Lexical by hand (node.type switch, node.format
|
|
* bit flags) duplicates what Payload already does, misses node types, drifts
|
|
* on Lexical updates, and tempts shortcuts like dangerouslySetInnerHTML (XSS)
|
|
* or a hardcoded `.pl` locale. The official converter handles every node,
|
|
* stays in sync, and is safe. This is the RenderBlocks of rich text: the
|
|
* plugin owns the engine, the project owns the look via `converters`.
|
|
*
|
|
* // project — a Server Component
|
|
* import { RenderRichText } from '@intecion/ipal-kit/rsc'
|
|
* <RenderRichText data={page.content} className="prose" />
|
|
*
|
|
* // with project styling per node:
|
|
* const converters: JSXConvertersFunction = ({ defaultConverters }) => ({
|
|
* ...defaultConverters,
|
|
* heading: ({ node, nodesToJSX }) => {
|
|
* const children = nodesToJSX({ nodes: node.children })
|
|
* const cls = { h2: 'text-2xl font-semibold mt-10', h3: 'text-xl mt-7' }[node.tag] ?? ''
|
|
* const Tag = node.tag as 'h1'|'h2'|'h3'|'h4'
|
|
* return <Tag className={cls}>{children}</Tag>
|
|
* },
|
|
* })
|
|
* <RenderRichText data={page.content} converters={converters} />
|
|
*/
|
|
export declare function RenderRichText({ className, converters, data }: RenderRichTextProps): import("react/jsx-runtime").JSX.Element | null;
|