Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store/display paragraphs with mongodb?

I understand that that mongo is not for formatting, but how can I create a blog post and display it in paragraph form without it being one block of text?

For example, if I have an admin section where I can create blog posts and I enter this into the textarea portion...

Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

It displays as this...

Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

like image 890
tim Avatar asked Apr 13 '15 15:04

tim


1 Answers

That isn't a problem with MongoDB, but you found out how HTML works :)

When you submit a textarea, all newlines are simply newline characters sent to the server (\n or \r\n). They are stored in the database as is.
However, in HTML newlines are ignored and considered like spaces, when representing text (unless you wrap that in a <pre></pre> block).

The solution is to replace all \n with <br /> tags. If you're familiar with PHP, it would be using the nl2br function, which has been ported to JavaScript here: http://phpjs.org/functions/nl2br/

like image 164
ItalyPaleAle Avatar answered Oct 12 '22 00:10

ItalyPaleAle