@bernierllc/email-template-engine
Version:
Email template processing engine with compilation, rendering, and text generation
40 lines (38 loc) • 1.36 kB
JavaScript
;
/*
Copyright (c) 2025 Bernier LLC
This file is licensed to the client under a limited-use license.
The client may use and modify this code *only within the scope of the project it was delivered for*.
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.htmlToText = htmlToText;
/**
* Simple HTML to plain text converter
* Removes HTML tags and normalizes whitespace
*/
function htmlToText(html) {
if (!html) {
return '';
}
return html
// Remove script and style tags with their content
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
// Convert common block elements to newlines
.replace(/<\/?(p|div|br|hr|h[1-6])[^>]*>/gi, '\n')
.replace(/<\/li>/gi, '\n')
// Remove remaining HTML tags
.replace(/<[^>]+>/g, '')
// Decode common HTML entities
.replace(/ /g, ' ')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
// Normalize whitespace
.replace(/\n\s*\n/g, '\n\n')
.replace(/[ \t]+/g, ' ')
.trim();
}