Skip to content

Instantly share code, notes, and snippets.

@zaydek-old
Created August 6, 2019 13:23
Show Gist options
  • Save zaydek-old/0c142c5ba8c20ef82abd3022cfe05445 to your computer and use it in GitHub Desktop.
Save zaydek-old/0c142c5ba8c20ef82abd3022cfe05445 to your computer and use it in GitHub Desktop.
// parse("hello,\nworld!") ->
//
// <div>
// <p>
// hello,
// </p>
// <p>
// world!
// </p>
// </div>
function parse(value) {
const fragment = document.createDocumentFragment()
const items = value.split("\n")
for (const value of items) {
const el = document.createElement("p")
const node = !value ? document.createElement("br") : document.createTextNode(value)
el.appendChild(node)
fragment.appendChild(el)
}
const root = document.createElement("div")
root.appendChild(fragment)
return root
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment