docs/rich-text/converting-plaintext.mdx
Here's how you can convert richtext data to plaintext using @payloadcms/richtext-lexical/plaintext.
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
import { convertLexicalToPlaintext } from '@payloadcms/richtext-lexical/plaintext'
// Your richtext data here
const data: SerializedEditorState = {}
const plaintext = convertLexicalToPlaintext({ data })
The convertLexicalToPlaintext functions accepts a converters object that allows you to customize how specific nodes are converted to plaintext.
import type {
DefaultNodeTypes,
SerializedBlockNode,
} from '@payloadcms/richtext-lexical'
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
import type { MyTextBlock } from '@/payload-types'
import {
convertLexicalToPlaintext,
type PlaintextConverters,
} from '@payloadcms/richtext-lexical/plaintext'
// Your richtext data here
const data: SerializedEditorState = {}
const converters: PlaintextConverters<
DefaultNodeTypes | SerializedBlockNode<MyTextBlock>
> = {
blocks: {
textBlock: ({ node }) => {
return node.fields.text ?? ''
},
},
link: ({ node }) => {
return node.fields.url ?? ''
},
}
const plaintext = convertLexicalToPlaintext({
converters,
data,
})
Unlike other converters, there are no default converters for plaintext.
If a node does not have a converter defined, the following heuristics are used to convert it to plaintext:
text field, it will be used as the plaintext.children field, the children will be recursively converted to plaintext.