diff --git a/lib/message-handler.js b/lib/message-handler.js index 08c3ca91..ee911da1 100644 --- a/lib/message-handler.js +++ b/lib/message-handler.js @@ -16,6 +16,7 @@ const parseDate = require('../imap-core/lib/parse-date'); const log = require('npmlog'); const packageData = require('../package.json'); const { SettingsHandler } = require('./settings-handler'); +const { htmlToText } = require('html-to-text'); // index only the following headers for SEARCH const INDEXED_HEADERS = ['to', 'cc', 'subject', 'from', 'sender', 'reply-to', 'message-id', 'thread-index', 'list-id', 'delivered-to']; @@ -360,6 +361,30 @@ class MessageHandler { return html; }) .filter(html => html); + + messageData.intro = htmlToText(messageData.html.join('')) + // assume we get the intro text from first 2 kB + .substr(0, 2 * 1024) + // remove markdown urls + .replace(/\[[^\]]*\]/g, ' ') + // remove quoted parts + // "> quote from previous message" + .replace(/^>.*$/gm, '') + // remove lines with repetetive chars + // "---------------------" + .replace(/^\s*(.)\1+\s*$/gm, '') + // join lines + .replace(/\s+/g, ' ') + .trim(); + + if (messageData.intro.length > 128) { + let intro = messageData.intro.substr(0, 128); + let lastSp = intro.lastIndexOf(' '); + if (lastSp > 0) { + intro = intro.substr(0, lastSp); + } + messageData.intro = intro + '…'; + } } this.users.collection('users').findOneAndUpdate(