Home > Blockchain >  How to keep format in html text?
How to keep format in html text?

Time:08-02

I'm have an application like a webchat, and i'm trying to format a text to a html text. For example:

Text:

"<b>Lorem ipsum dolor sit amet elit.\n\n Quisque maximus venenatis.</b>"

So to convert it to an html text, I'm using the ruby simple_format from ActionView::Helpers::TextHelper, but it's not keeping the bold formatation like following:

"<p><b>Lorem ipsum dolor sit amet elit.</p>\n\n <p>Quisque maximus venenatis.</b></p>"

when actually it should be shown as follows

"<p><b>Lorem ipsum dolor sit amet elit.</b></p>\n\n <p><b>Quisque maximus venenatis.</b></p>"

Any suggestions?

CodePudding user response:

If you apply this code:

- html = "<b>Lorem ipsum dolor sit amet elit.\n\n Quisque maximus venenatis.</b>"

= simple_format(simple)

it will produce such HTML:

<p><b>Lorem ipsum dolor sit amet elit.</p>

<p> Quisque maximus venenatis.</b></p>

And it keeps bold format for both lines

Another option -- use CSS

.wrap-lines {
  white-space: pre-line;
}

And then apply this rule to some container

- html = "<b>Lorem ipsum dolor sit amet elit.\n\n Quisque maximus venenatis.</b>"

.wrap-lines
  == html

CodePudding user response:

Use this: html_safe

<%= "<p><b>Lorem ipsum dolor sit amet elit.</p>\n\n <p>Quisque maximus venenatis.</b></p>".html_safe %>
  • Related