Skip to content

docs: adds documentation to converters #10907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/rich-text/converters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,44 @@ export const MyComponent = ({ data }: { data: SerializedEditorState }) => {

The `RichText` component includes built-in serializers for common Lexical nodes but allows customization through the `converters` prop.

In our website template [you have an example](https://github.com/payloadcms/payload/blob/main/templates/website/src/components/RichText/index.tsx) of how to use `converters` to render custom blocks.
In our website template [you have an example](https://github.com/payloadcms/payload/blob/main/templates/website/src/components/RichText/index.tsx) of how to use `converters` to render custom blocks. For instance, if you want to modify how headings are rendered, you can create a new component like this:

```tsx
import type { SerializedHeadingNode } from '@payloadcms/richtext-lexical'
import type { JSXConverters } from '@payloadcms/richtext-lexical/react'

export const HeadingJSXConverter: JSXConverters<SerializedHeadingNode> = {
heading: ({ node, nodesToJSX }) => {
const children = nodesToJSX({
nodes: node.children,
})

const NodeTag = node.tag

const classes = {
h1: 'h1-aNewClassName',
h2: 'h2-aNewClassName',
h3: 'h3-aNewClassName',
h4: 'h4-aNewClassName',
h5: 'h5-aNewClassName',
h6: 'h6-aNewClassName'
}

return <NodeTag className={classes[NodeTag] || 'default-class'}>{children}</NodeTag>
},
}
```

Then insert it into `jsxConverters` using the spread operator:

```tsx
const jsxConverters: JSXConvertersFunction<NodeTypes> = ({ defaultConverters }) => ({
...defaultConverters,
...HeadingJSXConverter,
...
})```

Additional examples can be found [in our repo](https://github.com/payloadcms/payload/tree/main/packages/richtext-lexical/src/exports/react/components/RichText/converter/converters).

<Banner type="default">
The JSX converter expects the input data to be fully populated. When fetching data, ensure the `depth` setting is high enough, to ensure that lexical nodes such as uploads are populated.
Expand Down