Skip to content

Commit

Permalink
fix: serialize lexical to plain text didn't work with nested nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
linobino1 committed Nov 28, 2024
1 parent e7e154b commit c8c3419
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
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(' ')
}
2 changes: 1 addition & 1 deletion frontend/app/components/RichText/lexicalToPlainText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function serializeToPlainText(nodes: NodeTypes[]): string {
return ''
}

return ''
return lexicalToPlainText(n.children)
})
.join('\n')
}

0 comments on commit c8c3419

Please sign in to comment.