import { defaultJSXConverters, RichText } from '@payloadcms/richtext-lexical/react'; import type { JSXConverters } from '@payloadcms/richtext-lexical/react'; import Image from 'next/image'; import { Fragment } from 'react'; import { allBlocks } from '@/src/payload/blocks/allBlocks'; import ObfuscatedEmail from '@/components/ObfuscatedEmail'; import ObfuscatedPhone from '@/components/ObfuscatedPhone'; /** * Splits a text string on \n and intersperses
elements. */ function textWithLineBreaks(text: string, key: string) { const parts = text.split('\n'); if (parts.length === 1) return text; return parts.map((part, i) => ( {part} {i < parts.length - 1 &&
}
)); } const jsxConverters: JSXConverters = { ...defaultJSXConverters, linebreak: () =>
, text: ({ node }: any) => { let content: React.ReactNode = node.text || ''; if (typeof content === 'string' && content.includes('\n')) { content = textWithLineBreaks(content, `t-${(node.text || '').slice(0, 8)}`); } if (typeof content === 'string' && content.includes('@')) { const emailRegex = /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g; const parts = content.split(emailRegex); content = parts.map((part, i) => { if (part.match(emailRegex)) { return ; } return part; }); } if (typeof content === 'string' && content.match(/\+\d+/)) { const phoneRegex = /(\+\d{1,4}[\d\s-]{5,15})/g; const parts = content.split(phoneRegex); content = parts.map((part, i) => { if (part.match(phoneRegex)) { return ; } return part; }); } return content; }, upload: ({ node }: any) => { return (
{node.value.alt
); }, blocks: allBlocks.reduce((acc, block) => { const Component = block.render; if (!Component) return acc; const renderFn = ({ node }: any) => { // Pass all fields as props to the component, and also as 'data' prop return ; }; // Map both the direct slug and the block- prefixed slug return { ...acc, [block.slug]: renderFn, [`block-${block.slug}`]: renderFn, }; }, {}), }; export default function PayloadRichText({ data, className, references }: { data: any; className?: string; references?: any[] }) { if (!data) return null; return (
); }