Files
cms/docs/rich-text/converting-plaintext.mdx
zeekay 09c5e16c5c feat: rebrand all packages to @hanzo/cms scope (Payload v3.85.2 fork)
- payload -> @hanzo/cms (core), bin payload -> hanzo-cms
- create-payload-app -> @hanzo/create-cms-app
- @payloadcms/<x> -> @hanzo/cms-<x> across the whole dependency graph
- 3545 files, 72 package.json, 4419 scoped refs, 3852 core import specifiers, 82 scss imports
- MIT attribution to Payload CMS Inc. preserved (LICENSE.md, author emails)

Upstream: github.com/payloadcms/payload @ v3.85.2 (MIT)
2026-07-01 17:56:00 -07:00

71 lines
1.9 KiB
Plaintext

---
title: Converting Plaintext
label: Converting Plaintext
order: 24
desc: Converting between lexical richtext and plaintext
keywords: lexical, richtext, plaintext, text
---
## Richtext to Plaintext
Here's how you can convert richtext data to plaintext using `@hanzo/cms-richtext-lexical/plaintext`.
```ts
import type { SerializedEditorState } from '@hanzo/cms-richtext-lexical/lexical'
import { convertLexicalToPlaintext } from '@hanzo/cms-richtext-lexical/plaintext'
// Your richtext data here
const data: SerializedEditorState = {}
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.
```ts
import type {
DefaultNodeTypes,
SerializedBlockNode,
} from '@hanzo/cms-richtext-lexical'
import type { SerializedEditorState } from '@hanzo/cms-richtext-lexical/lexical'
import type { MyTextBlock } from '@/payload-types'
import {
convertLexicalToPlaintext,
type PlaintextConverters,
} from '@hanzo/cms-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:
- 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.