Convert a saved editorState to plain text without editor? #3806
Unanswered
francistogram
asked this question in
Q&A
Replies: 3 comments
-
|
If you know the schema, you can write your own logic to convert the editor state to plain text. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
editor.getEditorState().read(() => $getRoot().getTextContent()) will also work |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I found a solution to convert state to plain text without editor. My use case is to performing on-page indexing using lexical editor state saved from Payload CMS. The plaintext works much better than other options. Two steps:
const html = DOMPurify.sanitize(
convertLexicalToHTML({ data: x.description }),
)
const div = document.createElement("div")
div.innerHTML = html
const text = div.textContent || ""Notes:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I found this discussion which suggests to get the text content from the editor is using the rootNode's
getTextContent()I'm wondering if it's possible to do this without the editor instance. The use case I have is scheduling tweets and you'd draft it from the editor and then saved to the backend as JSON but when it's time to post I'd need the text content
While it'd work to save both the editor state and the text content I'm hesitant to take that path since they should always be in sync and was hoping some utility existed to do something like
convertEditorStateToTextContent(editorState: EditorState): stringbut I could be convinced to save the string too I supposeBeta Was this translation helpful? Give feedback.
All reactions