修复TG一些邮件解析为空和格式优化

This commit is contained in:
eoao
2026-02-08 08:02:01 +08:00
parent 216f7272d8
commit dafb8dda01
4 changed files with 27 additions and 10 deletions

View File

@@ -14,13 +14,30 @@ const emailUtils = {
return parts.length === 2 ? parts[0] : '';
},
formatText(text) {
if (!text) return ''
return text
.split('\n')
.map(line => {
return line.replace(/[\u200B-\u200F\uFEFF\u034F\u200B-\u200F\u00A0\u3000\u00AD]/g, '')
.replace(/\s+/g, ' ')
.trim();
})
.join('\n')
.replace(/\n{3,}/g, '\n')
.trim();
},
htmlToText(content) {
if (!content) return ''
try {
const { document } = parseHTML(content);
const wrappedContent = content.includes('<body')
? content
: `<!DOCTYPE html><html><body>${content}</body></html>`;
const { document } = parseHTML(wrappedContent);
document.querySelectorAll('style, script, title').forEach(el => el.remove());
let text = document.body.innerText;
return text.trim();
return this.formatText(text);
} catch (e) {
console.error(e)
return ''