Skip to content

Instantly share code, notes, and snippets.

@tonysherbondy
Created November 9, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonysherbondy/d05a0794dc4398e8ae6285dbd9f011d2 to your computer and use it in GitHub Desktop.
Save tonysherbondy/d05a0794dc4398e8ae6285dbd9f011d2 to your computer and use it in GitHub Desktop.
const convertHtmlToPlainText = html => {
const dict = {
gt: '>',
hellip: '...',
ldquo: '"',
rdquo: '"',
lsquo: "'",
rsquo: "'",
ndash: '-',
}
const text = html
.replace(/<[^>]*>/g, '')
.replace(/&frac12;/g, '1/2')
.replace(/&frac14;/g, '1/4')
.replace(/&frac34;/g, '3/4')
.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);?/gi, (first, sec) => {
return dict.hasOwnProperty(sec) ? dict[sec] : first
})
.trim()
return text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment