html-to-text Convert HTML to plain text preserving line breaks
Examples
Convert an HTML email to readable plain text
Usage
"<div style="font-family:Arial">Hi team,</div><div><br></div>..." | html-to-text Extract text from an HTML newsletter preserving structure
Usage
"<p>Welcome to our newsletter!</p><p>Here’s what's..." | html-to-text Get plain text from an HTML calendar invite
Usage
"<table><tr><td>Meeting:</td><td>Monday 9am</td></tr></table>..." | html-to-text View source
(input)=>{
let out = input;
out = out.replace(/<\/(p|div|tr|table|blockquote|h[1-6])>/gi, "\n\n");
out = out.replace(/<br\s*\/?>/gi, "\n");
out = out.replace(/<li[^>]*>/gi, "\n- ");
out = out.replace(/<[^>]+>/g, "");
out = out.replace(/ /gi, " ");
out = out.replace(/&/g, "&");
out = out.replace(/</g, "<");
out = out.replace(/>/g, ">");
out = out.replace(/"/g, '"');
out = out.replace(/'/g, "'");
out = out.replace(/&#(\d+);/g, (_, n)=>String.fromCharCode(n));
out = out.replace(/&#x([0-9a-f]+);/gi, (_, h)=>String.fromCharCode(parseInt(h, 16)));
return out;
}