Skip to content

Instantly share code, notes, and snippets.

@ricealexander
Created October 16, 2020 02:07
Show Gist options
  • Save ricealexander/a9fa12aec17f0c09beaaf0313b2897bd to your computer and use it in GitHub Desktop.
Save ricealexander/a9fa12aec17f0c09beaaf0313b2897bd to your computer and use it in GitHub Desktop.
Strips all formatting from an element
function makePlainText (element = document.body) {
element.innerHTML = element.innerText
.replace(/\r\n?/g, '\n') // normalize line-endings to \n
.replace(/\n/g, '<br>') // replace linebreaks with <br>
.replace(/<br>\s+<br>/g, '<br><br>') // remove gaps between between <br>
.replace(/(<br>){3,}/g, '<br><br>') // cap <br> at just 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment