# [Resolved] MongoDB Atlas line-break issue

# The context

While creating [this](https://mynoteit.herokuapp.com) PWA, I wanted to store the *markdown* data into the MongoDB's [Atlas](https://www.mongodb.com/cloud/atlas).

**Example Markdown**

```markdown
# Some interesting title
Description about the topic...

- list #1
- list #2

> Maybe a quote ?
```

The above markdown would be stored as a **single string** in the database.

```js
{
  // other fields
  markdown: "# Some interesting title\nDescription about the topic...\n- list #1\n- list #2\n> Maybe a quote ?"
  // further fields
}
```

# The problem

While reading the data from the [Atlas](https://www.mongodb.com/cloud/atlas), the line-break escape character, i.e. `\n` would come as already escaped, i.e. `\\n` **notice the double '\\'**.

Therefore, while parsing it as *HTML*, the **line-break** wouldn't be read as a line-break character but a literal *\\n* character.

**Rendered HTML**

![Unexpected Render of Markdown](https://cdn.hashnode.com/res/hashnode/image/upload/v1623161725006/o3zBlcLG1.png align="left")

*The Markdown parser(*[*marked.js*](https://marked.js.org/)*) expects a line break between each block(headings, lists, paragraphs, quotes, etc.) to render them as expected. Otherwise, it will render them as a single line of string.*

*In the example above, it renders everything as a heading level 1.*

## Why?

When the Markdown parser sees `#` token, it assumes that the text after it(until a line break) is to be rendered as a H₁. Thus, everything including lists, paragraphs, and quotes is rendered as `<h1>` because of no line-break.

# The Solution

I made a mistake by thinking that the problem was with the *Markdown parser,* while instead, the problem was with the **data** coming from MongoDB.

The doubly escaped character `\\n` was the culprit.

As a workaround, I tried to *replace* all `\\n`s with `\n`s from the markdown string before passing it to the [Markdown Parser](https://marked.js.org/).

```js
import parser from "marked";

// 'markdownString' would be the markdown field read from mongodb
const replacedWithSingleEscape = markdownString.replace(/\\n/g, "\n");

parser(replacedWithSingleEscape);
```

**Solved! This is how the rendered output would look after the fix**

![Expected Render of Markdown](https://cdn.hashnode.com/res/hashnode/image/upload/v1623161782608/ZqQtQ83jw.png align="left")
