typescript
Converting Lexical rich text to plaintext for PayloadCMS search
PayloadCMS stores rich text in Lexical format, but the search plugin needs plaintext for indexing. The @payloadcms/richtext-lexical package exports a convertLexicalToPlaintext utility that handles this conversion.
Use it in the beforeSync hook of the search plugin to transform rich text fields before they're indexed.
import { searchPlugin } from "@payloadcms/plugin-search"
import { convertLexicalToPlaintext } from "@payloadcms/richtext-lexical/plaintext"
export const search = searchPlugin({
collections: ["posts"],
beforeSync: async ({ originalDoc, searchDoc }) => {
return {
...searchDoc,
// Convert Lexical JSON to searchable plaintext
description: convertLexicalToPlaintext({
data: originalDoc.description, // Lexical rich text field
}),
}
},
})payloadcmstypescript
lubosmato