To convert text to HTML in JavaScript, you can use the createRange()
and createContextualFragment()
functions. Here's an example:
index.tsx238 chars10 lines
This will output a DocumentFragment
object representing the converted HTML. You can then append this fragment to an element on the page using standard DOM manipulation techniques.
Note that this method does not escape any HTML entities, so you should only use it if you trust the source of the input text. If you need to convert text with entities (e.g. "<"), you can use the innerHTML
property of an element instead:
index.tsx236 chars10 lines
This will output the HTML string:
42 chars2 lines
gistlibby LogSnag