0

Let's say I have a (graphQL) API call that returns the following:

node {
   "body": "* Hello\r\n\r\n_World_",
}

Which is

* Hello

_World_

in the original Markdown.

(To be more specific, I'm fetching github issues comments. I know there's an option for HTML in the github API, but I'm just curious.)

Is there any easy way to turn that back to Markdown in javascript? Without regex.

itsagift
  • 11
  • 4
  • Do you mean that the body string contains literal backslashes and `n`s - and that those aren't what your terminal uses to display linefeeds and newlines? (It sounds quite strange, though not impossible, that the content is composed of literal `r`s and `n`s - double-check first.) – CertainPerformance Jun 30 '22 at 00:54
  • If the body string is composed of linefeeds and newline characters, you already have what you want. If the body string is composed of literal `r`s and `n`s, it looks like you just need to replace those with newlines. `.replaceAll('\\r\\n', '\n')` – CertainPerformance Jun 30 '22 at 00:59
  • @CertainPerformance Yeah that was just what the GraphiQL was returning. When I r ender the actual value, it's just `* Hello _World_` without the line breaks. – itsagift Jun 30 '22 at 00:59
  • @CertainPerformance I don't think I stated my question clearly enough, my bad. I want to parse the markdown in the string "* Hello _World_" (and render it as HTML). It seems plugins like remark would require me to create a new .md file with that API response. I just want to enter the string. – itsagift Jun 30 '22 at 01:03
  • If it renders without literal `r`s and `n`s, then you do have linefeed/newline characters in the payload - which is the markdown you want. If the newlines don't display when rendering, that's an issue with however you're rendering it, not with the body content. HTML doesn't use `\n` to put a newline inside a text node, you need an entity (or `
    `, or put everything into a `
    – CertainPerformance Jun 30 '22 at 01:03
  • @CertainPerformance Thanks. I think I figured out what I was looking for. – itsagift Jun 30 '22 at 01:06

0 Answers0