Get text from CM6 editor without losing indentations and line breaks

I am implementing a code editor in python using codemirror 6 as shown in the image below.

To run the code, I want to read it from the editor and store it in a backend file for execution. However, when I am trying to get the contents of the editor I am losing indentations which is an important part of python syntax.

I want file contents to be like this -


for i in range(10):

But I am currently getting this -

print("hello") for i in range(10): print(i+1)

Is there a way to get the code from the codemirror editor without losing the line breaks and indentations?

Yes, view.state.doc.toString() is probably what you’re looking for. I’m kind of curious what you’re doing that does strip line breaks. view.dom.textContent?

I am using view.state.doc.toString() only to get the text. I think I am making some mistakes in processing that text due to which line breaks are getting stripped.

Thank you for your help.