So I’m trying to convert a markdown file and send it over a rest API, and render it in the frontend. The problem is that the newlines, and other things get lost while trying to convert it back.
How do I render it back in a way that the original markdown is preserved?
Is there a standard way of doing this?
Tried to convert markdown by json.stringify, sent it over the api, tried to convert it back to markdown but doesn’t work like original anymore.
3
Answers
You can add all the content in a
<pre>
tag as followingIn a
<pre>
tag, the text preserves both spaces and line breaks thus it will be displayed exactly as written in the HTML source code.You can try using react-markdown
You can either send the original file as a string, or if that is not an option, you can format the object with this function:
Just use
format(objectGoesHere)
and it will return a string that is formatted for HTML.