import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical' import type { JSXConverters, JSXConvertersFunction } from '@payloadcms/richtext-lexical/react' import { RichText as PayloadRichText } from '@payloadcms/richtext-lexical/react' import { cloneElement, isValidElement, type ReactElement, type ReactNode } from 'react' import { headingIds as computeHeadingIds, type HeadingLevel } from './headings.js' 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 /** * Anchor ids on headings — the same ids as extractHeadings, so a * links to them. `true` = h2; or `{ levels, className }` * (className e.g. `scroll-mt-24` under a sticky header). Your own `heading` * converter still renders the heading; the id is added to what it returns. */ headingIds?: { className?: string; levels?: readonly HeadingLevel[] } | boolean } type HeadingNode = { children?: unknown[]; tag: HeadingLevel } /** Adds ids (and an optional class) to headings, on top of the project's converters. */ function withHeadingIds( converters: JSXConvertersFunction | undefined, ids: Map, className: string | undefined, ): JSXConvertersFunction { return (args) => { const base: JSXConverters = converters ? converters(args) : args.defaultConverters const inner = base.heading return { ...base, heading: (props: { node: HeadingNode; nodesToJSX: (a: { nodes: unknown[] }) => ReactNode[] }) => { const anchor = ids.get(props.node) const rendered: ReactNode = typeof inner === 'function' ? (inner as (p: typeof props) => ReactNode)(props) : (() => { const Tag = props.node.tag return {props.nodesToJSX({ nodes: props.node.children ?? [] })} })() if (!anchor || !isValidElement(rendered)) {return rendered} const el = rendered as ReactElement<{ className?: string; id?: string }> const cls = [el.props.className, className].filter(Boolean).join(' ') || undefined // The project's converter owns the markup; only the id is added to what it returned. // eslint-disable-next-line @eslint-react/no-clone-element return cloneElement(el, { id: anchor.id, ...(cls ? { className: cls } : {}) }) }, } as JSXConverters } } /** * 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' * * * // 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 {children} * }, * }) * * * // long document with a table of contents (same ids on both sides): * // /client * */ export function RenderRichText({ className, converters, data, headingIds }: RenderRichTextProps) { if (!data) {return null} if (headingIds) { const opts = headingIds === true ? {} : headingIds const ids = computeHeadingIds(data, opts.levels ? { levels: opts.levels } : {}) if (ids.size) { return } } return }