This was helpful to me, thanks! Based on this article I adapted to:
export function text2HTML(text) {
// 1: Escape special characters
const div = document.createElement('div');
div.appendChild(document.createTextNode(text));
text = div.innerHTML;
// 2: Line Breaks
text = text.replace(/\r\n?|\n/g, "<br>");
// 3: Paragraphs
text = text.replace(/<br>\s*<br>/g, "</p><p>");
// 4: Wrap in Paragraph Tags
text = "<p>" + text + "</p>";
return text;
}
This was helpful to me, thanks! Based on this article I adapted to: