How do I store/display paragraphs with mongodb?

后端 未结 2 1338
青春惊慌失措
青春惊慌失措 2021-01-01 02:04

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

相关标签:
2条回答
  • 2021-01-01 02:39

    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/

    0 讨论(0)
  • 2021-01-01 02:46

    This has nothing to do with MongoDB. You get text from HTML textarea element and it has "new line" (\n, \r or \r\n) characters as paragraph seperator. When you put it in HTML p elements, those new lines are interpreted as basic whitespaces. You should convert those "new line" characters into HTML br elements or put every text block into seperate p elements.

    Check this npm package: https://www.npmjs.com/package/nl2br

    You can easily code a better one.

    0 讨论(0)
提交回复
热议问题