Skip to content

Instantly share code, notes, and snippets.

@williamlsh
Created February 19, 2019 09:56
Show Gist options
  • Save williamlsh/7f8d52413e9a7a0e0b863a2855153d0c to your computer and use it in GitHub Desktop.
Save williamlsh/7f8d52413e9a7a0e0b863a2855153d0c to your computer and use it in GitHub Desktop.
function htmlDecode(str) {
const doc = new DOMParser().parseFromString(str, 'text/html');
return doc.documentElement.innerHTML;
}
// htmlDecode example
// const str =
// '\u003ch1\u003e\n\u003ca id="user-content-sonnet-1" class="anchor" href="#sonnet-1" aria-hidden="true"\u003e\u003cspan aria-hidden="true" class="octicon octicon-link"\u003e\u003c/span\u003e\u003c/a\u003eSonnet 1\u003c/h1\u003e\n\u003cp\u003eFrom fairest creatures we desire increase,\nThat thereby beauty’s rose might never die,\nBut as the riper should by time decease,\nHis tender heir might bear his memory:\nBut thou, contracted to thine own bright eyes,\nFeed’st thy light’st flame with self-substantial fuel,\nMaking a famine where abundance lies,\nThyself thy foe, to thy sweet self too cruel.\nThou that art now the world’s fresh ornament\nAnd only herald to the gaudy spring,\nWithin thine own bud buriest thy content\nAnd, tender churl, makest waste in niggarding.\nPity the world, or else this glutton be,\nTo eat the world’s due, by the grave and thee.\u003c/p\u003e\n';
// htmlDecode(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment