- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
110 lines
5.1 KiB
TypeScript
110 lines
5.1 KiB
TypeScript
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
|
|
* <TableOfContents> 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<object, { id: string }>,
|
|
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 <Tag>{props.nodesToJSX({ nodes: props.node.children ?? [] })}</Tag>
|
|
})()
|
|
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'
|
|
* <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} />
|
|
*
|
|
* // long document with a table of contents (same ids on both sides):
|
|
* <TableOfContents items={extractHeadings(page.content)} label={toc.label} /> // /client
|
|
* <RenderRichText data={page.content} headingIds={{ className: 'scroll-mt-24' }} />
|
|
*/
|
|
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 <PayloadRichText className={className} converters={withHeadingIds(converters, ids, opts.className)} data={data} />
|
|
}
|
|
}
|
|
return <PayloadRichText className={className} converters={converters} data={data} />
|
|
}
|