2019-11-05 12:14:54 +07:00
|
|
|
const juice = require('juice');
|
|
|
|
const template = require('./template');
|
|
|
|
const settingsCache = require('../../services/settings/cache');
|
|
|
|
const urlUtils = require('../../lib/url-utils');
|
2019-11-06 12:06:24 +07:00
|
|
|
const moment = require('moment');
|
2019-11-15 11:25:46 +05:30
|
|
|
const cheerio = require('cheerio');
|
2019-11-05 12:14:54 +07:00
|
|
|
|
|
|
|
const getSite = () => {
|
|
|
|
return Object.assign({}, settingsCache.getPublic(), {
|
|
|
|
url: urlUtils.urlFor('home', true)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const serialize = (post) => {
|
2019-11-06 12:06:24 +07:00
|
|
|
post.published_at = post.published_at ? moment(post.published_at).format('DD MMM YYYY') : moment().format('DD MMM YYYY');
|
2019-11-06 18:03:28 +07:00
|
|
|
post.authors = post.authors && post.authors.map(author => author.name).join(',');
|
2019-11-07 11:15:16 +07:00
|
|
|
post.html = post.html || '';
|
2019-11-06 18:32:11 +07:00
|
|
|
if (post.posts_meta) {
|
|
|
|
post.email_subject = post.posts_meta.email_subject;
|
|
|
|
}
|
2019-11-15 11:25:46 +05:30
|
|
|
let juicedHtml = juice(template({post, site: getSite()}));
|
|
|
|
// Force all links to open in new tab
|
|
|
|
let _cheerio = cheerio.load(juicedHtml);
|
|
|
|
_cheerio('a').attr('target','_blank');
|
|
|
|
juicedHtml = _cheerio.html();
|
2019-11-05 12:14:54 +07:00
|
|
|
return {
|
|
|
|
subject: post.email_subject || post.title,
|
2019-11-15 11:25:46 +05:30
|
|
|
html: juicedHtml,
|
2019-11-05 15:04:48 +07:00
|
|
|
plaintext: post.plaintext
|
2019-11-05 12:14:54 +07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
serialize: serialize
|
|
|
|
};
|