Text to HTML conversion in Node Js - html

I am using nodemailer to send mail from node server. I am getting the content for this mail from MSSQL SQL server which is formatted in plain text format, which meansa there are new line characters in it, however when I send it using nodemailer the newline characters are missing and the whole text looks messed up. The other way is to insert html tags for line break in the plain text and send this works fine. But there is too much mannual work involved what I am looking is for a library or utility which can convert the plain text into the html which I can send using mail.
Is there any liberary for this requirement or a way to do this automatically?

The following will wrap all parts that are separated by more than one newline in paragraphs (<p>...</p>) and insert breaks (<br>) where there is just one newline. A text block without any newlines will simply be wrapped in a paragraph.
template = '<p>' + template.replace(/\n{2,}/g, '</p><p>').replace(/\n/g, '<br>') + '</p>';
So for example, it will take this:
Title
First line.
Second line.
Footer
And convert it to this:
<p>Title</p><p>First line.<br>Second line.</p><p>Footer</p>

The simplest solution is you can replace the new line characters with <br>.
Try
text.split('\n').join('\n<br>\n')
then you are done.

Ok finally this code snippet worked for me -
template = template.replace(/\n/gi, "</p></br/>")
template = template.replace(/<\/p>/gi, "</p><p></br/>")
This was a lot of hit and trial but eventually it worked.

Related

Can I embedded HTML tags in an HREF's TITLE? (I need some sort of line break)

It seems not,as they are showing up as cleartext.
I am trying to format a rather large tootlip by inserting <p> and <br>, but, as I say, Chrome treats them as text.
What am I allowed to put into such a tooltip? Anything other than a single string?
Since \n seems to be ignored, is there any way to get a line break into such a string?
You can add symbol for a new line: 
 (\r) or
(\n) to your title.
test
Another option is to find some JavaScript tooltip library.
If you feed them actual line breaks, they will work.
<span title="Two
Liner">Hover here</span>
However, if you need more complex HTML inside, I'd suggest qTip or Bootstrap's tooltips

Using ruby variables as html code

I would expect that the following:
<div style="padding-top:90px;"><%= u.one_line %></div>
simply pulls whatever is in u.one_line (which in my case is text from database), and puts it in the html file. The problem I'm having is that sometimes, u.one_line has text with formatted html in it (just line breaks). For example sometimes:
u.one_line is "This is < / b r > awesome"
and I would like the page to process the fact that there's a line break in there... I had to put it with spaces up ^^^ here because the browser would not display it otherwise on stackoverflow. But on my server it's typed correctly, unfortunately instead of the browser processing the line break, it prints out the "< / b r>" part...
I hope you guys understand what I mean :(?
always remember to use raw or html_safe for html output in rails because rails by default auto-escapes html content for protecting against XSS attacks.
for more see
When to use raw() and when to use .html_safe

php/html: Force line breaks to appear inside textarea

This should not be that hard but I can't seem to find information on it. How do you get line breaks to appear inside a text area when you are echoing it from the server? In other words what is <some code> in line below?
<text area>first line <some code> second line <some code> third line</text area>
I know how to write out <some code>. I just need to know what it should be. I have tried \n, \r\n, '\n', "\n", \N and variations but cannot get the line breaks to display.
Note: This is not about displaying in HTML so <br> is not what I want. This is not about getting it to display if you are typing it yourself where you can type a carriage return, i.e. :
<textarea>first line
second line </textarea>
When you are outputting from server you cannot use keyboard. This is what code to accomplish above.
Thanks for any suggestions
This is a super old question, but I ran into the same issue ajaxing content into a text area.
Thanks to this answer I used the html code for a new line,
and that worked for me. So basically:
<textarea> Here's my returned text
And it gets two lines </textarea>
which (at least for me) comes out as
Here's my returned text
And it gets two lines
try this
<'p'>firstline<'/p'>
<'p'>secondline<'/p'>
remove comma from p and /p
for display \n in html you should use nl2br() php function .
otherwise you should using "\n" (not "/n") for break the line inside your text ;
You should try \r\n instead of /r/n.
You need to use "\r\n" (with double quotes) when echoing it out from PHP.

Multiline Edit Box value into HTML to be sent in email in xpages

I found some great javascript code(xpHTMLMail file) to be able to create an HTML e-mail that the users create on the fly from an xpage document that they write a review on a salesperson. However, there are some Multiline edit boxes on there and they have carriage returns, spaces, etc in them. These do not come over when they are added to the HTML. Anything I can do to keep the formatting for the e-mail that is created? Thanks in advance.
Here's the code that deals with this part of my question(inputClosing is a Multiline Edit Box):
mail.addHTML("<br /><br /><b>Closing</b><br />"+getComponent('inputClosing').getValue())
If inputClosing has...
"Dear Joe,
Great work. Keep it up!
Thanks,
Bill"
It comes into the email as...
Dear Joe, Great Work. Keep it up! Thanks, Bill
I wrote that library so thanks!
Since you're creating an HTML mail, you need to replace the line breaks in the value of the Multiline Edit Box by <br /> tags. Since you're dealing with Java in XPages, the line breaks are stored in the value using the \r\n sequence.
You can replace them using the the replaceAll() or (SSJS) #ReplaceSubstring() function.
Your code might then look like this:
var content:string = getComponent("inputClosing").getValue();
mail.addHTML("<b>Closing</b><br />" + content.replaceAll("\r\n", "<br />") );
Mark's suggestion definitely works, but it might be easier just to wrap the text fields with <pre></pre> then it will treat them as text instead of html, no matter what kind of formatting character is in it.

Add a linebreak in an HTML text area

How can i add a line break to the text area in a html page?
i use VB.net for server side coding.
If it's not vb you can use
(ascii codes for cr,lf)
Add a linefeed ("\n") to the output:
<textarea>Hello
Bybye</textarea>
Will have a newline in it.
You could use \r\n, or System.Environment.NewLine.
If you're inserting text from a database or such (which one usually do), convert all "<br />"'s to &vbCrLf. Works great for me :)
In a text area, as in the form input, then just a normal line break will work:
<textarea>
This is a text area
line breaks are automatic
</textarea>
If you're talking about normal text on the page, the <br /> (or just <br> if using plain 'ole HTML4) is a line break.
However, I'd say that you often don't actually want a line break. Usually, your text is seperated into paragraphs:
<p>
This is some text
</p>
<p>
This is some more
</p>
Which is much better because it gives a clue as to how your text is structured to machines that read it. Machines that read it include screen readers for the partially sighted or blind, seperating text into paragraphs gives it a chance of being presented correctly to these users.
I believe this will work:
TextArea.Text = "Line 1" & vbCrLf & "Line 2"
System.Environment.NewLine could be used in place of vbCrLf if you wanted to be a little less VB6 about it.
Escape sequences like "\n" work fine ! even with text area! I passed a java string with the "\n" to a html textarea and it worked fine as it works on consoles for java!
Here is my method made with pure PHP and CSS :
/** PHP code */
<?php
$string = "the string with linebreaks";
$string = strtr($string,array("."=>".\r\r",":"=>" : \r","-"=>"\r - "));
?>
And the CSS :
.your_textarea_class {
style='white-space:pre-wrap';
}
You can do the same with regex (I'm learning how to build regex with pregreplace using an associative array, seems to be better for adding the \n\r which makes the breaks display).