Skip to content

Improved escaping #1

@jonom

Description

@jonom

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions