Merge pull request #473 from kiliansinger/3.6.1_issue_469

used DOMParser to convert HTML to plain text
This commit is contained in:
Mic 2025-08-20 21:12:44 +02:00 committed by GitHub
commit 1fd08225f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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