Created
August 6, 2019 13:23
-
-
Save zaydek-old/0c142c5ba8c20ef82abd3022cfe05445 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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