Skip to content

Instantly share code, notes, and snippets.

@towfiqpiash
Last active October 12, 2022 10:54
Show Gist options
  • Save towfiqpiash/d6b51e97120adbea5a4581edc6094219 to your computer and use it in GitHub Desktop.
Save towfiqpiash/d6b51e97120adbea5a4581edc6094219 to your computer and use it in GitHub Desktop.
Javascript function to convert HTML to plain text
// converts HTML to text using Javascript
function html2text(html) {
var tag = document.createElement('div');
tag.innerHTML = html;
return tag.innerText;
}
// Convert Any copied text to plain text in TinyMCE (strip all tags)
paste_preprocess: function(plugin, args) {
var tag = document.createElement('div');
tag.innerHTML = args.content;
args.content = tag.innerText;
}
// Convert Any copied text to plain text in TinyMCE (Keep <P> tag only)
paste_preprocess: function(plugin, args) {
var pWithStyle = args.content.replace(/<[^p](?:.|\n)*?>/gm, '');
args.content = pWithStyle.replace(/\sstyle=\"(.*?)\"/gm, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment