-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: serialize lexical to plain text didn't work with nested nodes
- Loading branch information
Showing
2 changed files
with
10 additions
and
18 deletions.
There are no files selected for viewing
26 changes: 9 additions & 17 deletions
26
cms/src/collections/Mailings/lexical/serializeLexicalToPlainText.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,17 @@ | ||
import { SerializedLexicalNode } from './types' | ||
|
||
function serializeToPlainText(nodes: SerializedLexicalNode[]): string { | ||
export function serializeLexicalToPlainText(json: any): string { | ||
return serialize(json?.root?.children ?? []) | ||
} | ||
|
||
function serialize(nodes: SerializedLexicalNode[]): string { | ||
return nodes | ||
.map((node): string => { | ||
.map((node) => { | ||
if (node.type === 'text') { | ||
return node.text ?? '' | ||
} | ||
|
||
if (node.type === 'block') { | ||
return '' | ||
return node.text | ||
} | ||
|
||
return '' | ||
return serialize(node.children ?? []) | ||
}) | ||
.join('\n') | ||
} | ||
|
||
export function serializeLexicalToPlainText(json: any): string { | ||
if (!Array.isArray(json?.root?.children)) { | ||
console.error('Invalid JSON structure', json) | ||
throw new Error('Invalid JSON structure') | ||
} | ||
return serializeToPlainText(json?.root?.children) | ||
.join(' ') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters