diff --git a/ghost/email-service/lib/email-renderer.js b/ghost/email-service/lib/email-renderer.js index d2e04f400b..6fa2e22f25 100644 --- a/ghost/email-service/lib/email-renderer.js +++ b/ghost/email-service/lib/email-renderer.js @@ -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) { diff --git a/ghost/email-service/lib/email-templates/partials/latest-posts.hbs b/ghost/email-service/lib/email-templates/partials/latest-posts.hbs index 404c05103c..9f806bd823 100644 --- a/ghost/email-service/lib/email-templates/partials/latest-posts.hbs +++ b/ghost/email-service/lib/email-templates/partials/latest-posts.hbs @@ -33,7 +33,9 @@ {{/if}}

{{title}}

-

{{publishedAt}}

+ {{#if excerpt}} +

{{excerpt}}

+ {{/if}} diff --git a/ghost/email-service/test/email-renderer.test.js b/ghost/email-service/test/email-renderer.test.js index b77a15e584..1ee4c34a41 100644 --- a/ghost/email-service/test/email-renderer.test.js +++ b/ghost/email-service/test/email-renderer.test.js @@ -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({