Richtext to Plaintext
Here's how you can convert richtext data to plaintext using @payloadcms/richtext-lexical/plaintext.
1import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
3import { convertLexicalToPlaintext } from '@payloadcms/richtext-lexical/plaintext'
6const data: SerializedEditorState = {}
8const plaintext = convertLexicalToPlaintext({ data })
Custom Converters
The convertLexicalToPlaintext functions accepts a converters object that allows you to customize how specific nodes are converted to plaintext.
4} from '@payloadcms/richtext-lexical'
5import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
6import type { MyTextBlock } from '@/payload-types'
9 convertLexicalToPlaintext,
10 type PlaintextConverters,
11} from '@payloadcms/richtext-lexical/plaintext'
14const data: SerializedEditorState = {}
16const converters: PlaintextConverters<
17 DefaultNodeTypes | SerializedBlockNode<MyTextBlock>
20 textBlock: ({ node }) => {
21 return node.fields.text ?? ''
25 return node.fields.url ?? ''
29const plaintext = convertLexicalToPlaintext({
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:
- If the node has a
text field, it will be used as the plaintext. - If the node has a
children field, the children will be recursively converted to plaintext. - If the node has neither, it will be ignored.
- Paragraph, text and tab nodes insert newline / tab characters.