Last active
October 12, 2022 10:54
-
-
Save towfiqpiash/d6b51e97120adbea5a4581edc6094219 to your computer and use it in GitHub Desktop.
Javascript function to convert HTML to plain text
This file contains 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
// 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