Added post excerpts to the latest posts email

refs https://github.com/TryGhost/Team/issues/2769

Truncates if it is too long (also for the title).
This commit is contained in:
Simon Backx 2023-03-22 16:09:59 +01:00
parent 661de46ae0
commit 24c5a45057
3 changed files with 29 additions and 18 deletions

View File

@ -676,6 +676,14 @@ class EmailRenderer {
}
}
truncateText(text, maxLength) {
if (text && text.length > maxLength) {
return text.substring(0, maxLength - 1).trim() + '…';
} else {
return text ?? '';
}
}
/**
* @private
*/
@ -750,12 +758,7 @@ class EmailRenderer {
const {href: featureImageMobile, width: featureImageMobileWidth, height: featureImageMobileHeight} = await this.limitImageWidth(latestPost.get('feature_image'), 600, 480);
latestPosts.push({
title: latestPost.get('title'),
publishedAt: (latestPost.get('published_at') ? DateTime.fromJSDate(latestPost.get('published_at')) : DateTime.local()).setZone(timezone).setLocale('en-gb').toLocaleString({
year: 'numeric',
month: 'short',
day: 'numeric'
}),
title: this.truncateText(latestPost.get('title'), 85),
url: this.#getPostUrl(latestPost),
featureImage: featureImage ? {
src: featureImage,
@ -766,7 +769,8 @@ class EmailRenderer {
src: featureImageMobile,
width: featureImageMobileWidth,
height: featureImageMobileHeight
} : null
} : null,
excerpt: this.truncateText(latestPost.get('custom_excerpt') || latestPost.get('plaintext'), 60)
});
if (featureImage) {

View File

@ -33,7 +33,9 @@
{{/if}}
<td valign="top" align="left" class="latest-post-title">
<h4>{{title}}</h4>
<p>{{publishedAt}}</p>
{{#if excerpt}}
<p>{{excerpt}}</p>
{{/if}}
</td>
</tr>
</table>

View File

@ -6,7 +6,6 @@ const linkReplacer = require('@tryghost/link-replacer');
const sinon = require('sinon');
const logging = require('@tryghost/logging');
const {HtmlValidate} = require('html-validate');
const {DateTime} = require('luxon');
function validateHtml(html) {
const htmlvalidate = new HtmlValidate({
@ -1289,17 +1288,20 @@ describe('Email renderer', function () {
{
title: 'Test Post 1',
published_at: new Date('2018-01-01T00:00:00.000Z'),
custom_excerpt: 'Super long custom excerpt. Super long custom excerpt. Super long custom excerpt. Super long custom excerpt. Super long custom excerpt.',
feature_image: 'http://example.com/image.jpg'
},
{
title: 'Test Post 2',
published_at: new Date('2018-01-01T00:00:00.000Z'),
feature_image: null
feature_image: null,
plaintext: ''
},
{
title: 'Test Post 3',
published_at: null, // required for full test coverage
feature_image: null
feature_image: null,
plaintext: 'Nothing special.'
}
]
})
@ -1562,7 +1564,7 @@ describe('Email renderer', function () {
assert.deepEqual(data.latestPosts,
[
{
publishedAt: '1 Jan 2018',
excerpt: 'Super long custom excerpt. Super long custom excerpt. Super…',
title: 'Test Post 1',
url: 'http://example.com',
featureImage: {
@ -1579,18 +1581,14 @@ describe('Email renderer', function () {
{
featureImage: null,
featureImageMobile: null,
publishedAt: '1 Jan 2018',
excerpt: '',
title: 'Test Post 2',
url: 'http://example.com'
},
{
featureImage: null,
featureImageMobile: null,
publishedAt: DateTime.local().setZone('UTC').setLocale('en-gb').toLocaleString({
year: 'numeric',
month: 'short',
day: 'numeric'
}),
excerpt: 'Nothing special.',
title: 'Test Post 3',
url: 'http://example.com'
}
@ -1640,6 +1638,13 @@ describe('Email renderer', function () {
});
});
describe('truncateText', function () {
it('works for null', async function () {
const emailRenderer = new EmailRenderer({});
assert.equal(emailRenderer.truncateText(null, 100), '');
});
});
describe('limitImageWidth', function () {
it('Limits width of local images', async function () {
const emailRenderer = new EmailRenderer({