used DOMParser to convert HTML to plain text

This commit is contained in:
Kilian Singer 2025-08-20 18:10:18 +02:00
parent 2fa356ee6d
commit a96702b0a8

View file

@ -198,19 +198,24 @@ export function stripHtmlKeepLines(htmlString) {
.replace(/<[^>]*>/g, '') // removes any other HTML tags .replace(/<[^>]*>/g, '') // removes any other HTML tags
.trim(); // removes leading/trailing whitespace .trim(); // removes leading/trailing whitespace
} }
function htmlBodyToPlainText(htmlString) {
// Create a new DOMParser instance
const parser = new DOMParser();
// Parse the HTML string
const doc = parser.parseFromString(htmlString, 'text/html');
export function htmlBodyToPlainText(html) { // remove invisible elements https://stackoverflow.com/questions/39813081/queryselector-where-display-is-not-none
return html // return doc;
.replace(/<head[\s\S]*?<\/head>/gi, '') const docsan=doc.querySelectorAll('[style*="visibility:hidden"]').forEach(e => e.remove());//.querySelector('html').children.not(':visible').remove()
.replace(/<br\s*\/?>/gi, '\n') // Extract text content
.replace(/<\/p\s*>/gi, '\n') const textContent = doc.body.textContent || "";
.replace(/<p\s*>/gi, '') // Trim whitespace
.replace(/<\/(div|section|article|li|ul|ol|table|tr|td|th)>/gi, '\n') // newline for block tags return textContent
.replace(/<[^>]*>/g, '') // remove all other tags with no extra spaces .replace(/[ \t]+\n/g, '\n')
.replace(/[ \t]+\n/g, '\n') .replace(/\n{2,}/g, '\n')
.replace(/\n{2,}/g, '\n') .replace(/[ \t]+/g, ' ')
.replace(/[ \t]+/g, ' ') .replace(/&nbsp;/gi,"")
.trim(); .trim();
} }
export function convertNewlinesToBr(text) { export function convertNewlinesToBr(text) {