I know how to retrieve body_markdown with the Stack Exchange API. But if I copy the retrieved body into a Markdown editor then I don't get back the "real" body of the answer. I've tried using stackedit.io, Github's editor and I have even tried to copy the body into the edit page of the answer itself. But I don't get back the "real" answer.

For example, I receive 2 newlines in body_markdown with the API as:

\r\n\r\n

But no Markdown editor will transform this into 2 real newlines.

Ok, so answering to the comments, I made an example showing that the markdown I receive from the API is not complete(or not correct):

$.getJSON (
    "https://api.stackexchange.com/2.2/questions/2353416?site=stackoverflow&filter=!-W2dp-oiHRff49soT1df",
    data => {console.log (data.items[0].body_markdown);}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Now if I copy the markdown body back into StackEdit or I paste it into the "edit" page of the question itself, I receive some garbage, it is not converted back to the original question.

How can I get back the "real", correct body(no markdown, no HTML)?

  • How is it transformed then? – rene Oct 10 at 19:45
  • that's what I'm asking – Csaba Oct 10 at 19:46
  • 3
    I meant: what is wrong and/or what do you expect. It depends on where those newlines are if they are rendered or not. Maybe include the example post you used so we can re-create. – rene Oct 10 at 20:15
  • @rene this is the post itself: stackoverflow.com/questions/2353416/no-module-named-urls and the API response: api.stackexchange.com/docs/… – Csaba Oct 11 at 11:35
  • @rene I updated my question with an example – Csaba Oct 11 at 11:51
up vote 5 down vote accepted

From the API docs, under General:

All API responses are JSON,...

So this means that your program must JSON.parse the results somehow -- which will actualize all of that \n, \r, etc. text into the appropriate char codes.

For example:

$.getJSON (
    "https://api.stackexchange.com/2.2/questions/316603?site=meta&filter=!-W2dp-oiHRff49soT1df",
    data => {console.log (data.items[0].body_markdown);}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Run that, and you will see copy and pastable markdown text in the console.


Text with HTML entities:

In many cases, editor inputs handle HTML entities smoothly. EG you enter &clubs; and "♣" is rendered and stored properly.

But for some editors, or for preformatted code, the entities need to be decoded/unescaped.

For example, in javascript:

$.getJSON (
    "https://api.stackexchange.com/2.2/questions/2353416?site=stackoverflow&filter=!-W2dp-oiHRff49soT1df",
    data => {
        var rawMarkdown = htmlDecode (data.items[0].body_markdown);
        console.log (rawMarkdown);}
);

function htmlDecode (textWithHtmlEntities) {
    var tmpDoc = new DOMParser().parseFromString (textWithHtmlEntities, "text/html");
    return tmpDoc.documentElement.textContent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

-- which yields text with the code blocks as expected.

  • your answer seems to work with my question but does not work with other ones(please check my updated question - I made an example with your code but another post id) – Csaba Oct 11 at 11:54
  • @Csaba, for code blocks, you also need to decode any HTML entities. See the updated answer. – Awesome Poodles Oct 11 at 17:54
  • 1
    wow, thank you very much, this really works, I am so happy now, you made my day happier :) – Csaba Oct 11 at 18:06
  • @Csaba, You're welcome; glad to help. – Awesome Poodles Oct 11 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .