springload / draftjs_exporter

Convert Draft.js ContentState to HTML
https://www.draftail.org/blog/2018/03/13/rethinking-rich-text-pipelines-with-draft-js
MIT License
83 stars 21 forks source link

Is it possible to export draftjs state as plain text (i.e., without any html tags) #137

Closed drxcheng closed 3 years ago

drxcheng commented 3 years ago

is there a built-in function to do it?

if not, is it possible to set configuration to do it?

thanks!

thibaudcolas commented 3 years ago

Hey @drxcheng, there is no built-in function to do this, but you could configure the exporter for this yes.

This is very experimental but I made a Markdown exporter, which is based on draftjs_exporter, just redefining how elements are rendered to use Markdown syntax: https://github.com/thibaudcolas/draftjs_exporter_markdown. I wouldn’t suggest you use it as-is for anything serious, but it shows how you’d customize the exporter to output something that has less formatting.

But if you really want plain text without any markup – you don’t even need the exporter, just iterate through the blocks manually:

plain_text = ""

for block in content_state["blocks"]:
    plain_text += f"{block["text"]}\n"

I hope that helps.