Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Converting Plaintext

Converting Richtext to Plaintext

Here's how you can convert richtext data to plaintext using @payloadcms/richtext-lexical/plaintext.

1
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
2
3
import { convertLexicalToPlaintext } from '@payloadcms/richtext-lexical/plaintext'
4
5
// Your richtext data here
6
const data: SerializedEditorState = {}
7
8
const plaintext = convertLexicalToPlaintext({ data })

Custom Converters

The convertLexicalToPlaintext functions accepts a converters object that allows you to customize how specific nodes are converted to plaintext.

1
import type {
2
DefaultNodeTypes,
3
SerializedBlockNode,
4
} from '@payloadcms/richtext-lexical'
5
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
6
import type { MyTextBlock } from '@/payload-types'
7
8
import {
9
convertLexicalToPlaintext,
10
type PlaintextConverters,
11
} from '@payloadcms/richtext-lexical/plaintext'
12
13
// Your richtext data here
14
const data: SerializedEditorState = {}
15
16
const converters: PlaintextConverters<
17
DefaultNodeTypes | SerializedBlockNode<MyTextBlock>
18
> = {
19
blocks: {
20
textBlock: ({ node }) => {
21
return node.fields.text ?? ''
22
},
23
},
24
link: ({ node }) => {
25
return node.fields.url ?? ''
26
},
27
}
28
29
const plaintext = convertLexicalToPlaintext({
30
converters,
31
data,
32
})

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.
Next

Custom Features