Enhance htmlBodyToPlainText function to handle additional block tags and improve HTML tag removal

This commit is contained in:
mic 2025-08-20 09:46:49 +02:00
parent 14da34c44e
commit 9483a57936

View file

@ -201,15 +201,16 @@ export function stripHtmlKeepLines(htmlString) {
export function htmlBodyToPlainText(html) { export function htmlBodyToPlainText(html) {
return html return html
.replace(/<head[\s\S]*?<\/head>/gi, '') // remove <head> and its content .replace(/<head[\s\S]*?<\/head>/gi, '')
.replace(/<br\s*\/?>/gi, '\n') // replace <br> with newline .replace(/<br\s*\/?>/gi, '\n')
.replace(/<\/p\s*>/gi, '\n') // replace </p> with newline .replace(/<\/p\s*>/gi, '\n')
.replace(/<p\s*>/gi, '') // remove <p> tag .replace(/<p\s*>/gi, '')
.replace(/<[^>]*>/g, ' ') // remove any other HTML tags .replace(/<\/(div|section|article|li|ul|ol|table|tr|td|th)>/gi, '\n') // newline for block tags
.replace(/[ \t]+\n/g, '\n') // remove spaces before newline .replace(/<[^>]*>/g, '') // remove all other tags with no extra spaces
.replace(/\n{2,}/g, '\n') // compress multiple newlines into one .replace(/[ \t]+\n/g, '\n')
.replace(/\s+/g, ' ') // normalize spaces inside lines .replace(/\n{2,}/g, '\n')
.trim(); // trim spaces at start and end .replace(/[ \t]+/g, ' ')
.trim();
} }
export function convertNewlinesToBr(text) { export function convertNewlinesToBr(text) {