Skip to content

Commit 073c453

Browse files
committed
✨ feat(core): enhance htmlToPlainText function to strip unwanted tags
- remove <head>, <style>, and <script> blocks from HTML - eliminate zero-width and invisible Unicode characters - collapse multiple spaces and blank lines for cleaner output
1 parent e85e692 commit 073c453

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

scripts/react-email-renderer.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ function postProcessBlade(html: string): string {
174174
*/
175175
function htmlToPlainText(html: string): string {
176176
return html
177+
// Remove entire <head>…</head> block (contains <style> and <title>)
178+
.replace(/<head[\s\S]*?<\/head>/gi, '')
179+
// Remove entire <style>…</style> blocks (CSS content, @font-face, etc.)
180+
.replace(/<style[\s\S]*?<\/style>/gi, '')
181+
// Remove entire <script>…</script> blocks
182+
.replace(/<script[\s\S]*?<\/script>/gi, '')
177183
// Block elements → newlines before stripping
178184
.replace(/<br\s*\/?>/gi, '\n')
179185
.replace(/<\/p>/gi, '\n\n')
@@ -191,6 +197,12 @@ function htmlToPlainText(html: string): string {
191197
.replace(/&quot;/g, '"')
192198
.replace(/&#39;/g, "'")
193199
.replace(/&nbsp;/g, ' ')
200+
// Remove zero-width and invisible Unicode characters used as email preview spacers
201+
.replace(/[\u200B\u200C\u200D\u200E\u200F\u00AD\uFEFF\u2060\u180E]/g, '')
202+
// Collapse runs of spaces/tabs on a single line to one space
203+
.replace(/[^\S\n]+/g, ' ')
204+
// Remove lines that contain only whitespace
205+
.replace(/^ +$/gm, '')
194206
// Collapse runs of blank lines to at most two
195207
.replace(/\n{3,}/g, '\n\n')
196208
.trim();

0 commit comments

Comments
 (0)