From c434666ba213cb3eb19f5b6cdd427e5ea5a53c8d Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 12 Aug 2020 20:14:06 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Removed=20[http://url/]=20output?= =?UTF-8?q?=20in=20member=20email=20preview=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue - we output the post excerpt in a hidden div in the email template so that email clients pick it up as the "preview" text when listing emails - when no custom excerpt is provided the preview text is grabbed from post.excerpt which is the first 500 chars of the post.plaintext value - post.plaintext formats links as "Link [http://url/]" which is unwanted in html email previews - add a basic replacement to the post email serializer to remove any `[http://url/]` occurrences from the post excerpt before rendering the email content --- core/server/services/mega/post-email-serializer.js | 8 ++++++++ test/api-acceptance/admin/email_preview_spec.js | 10 +++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/server/services/mega/post-email-serializer.js b/core/server/services/mega/post-email-serializer.js index 25513b4de0..cf250e7705 100644 --- a/core/server/services/mega/post-email-serializer.js +++ b/core/server/services/mega/post-email-serializer.js @@ -105,6 +105,14 @@ const serialize = async (postModel, options = {isBrowserPreview: false}) => { if (post.posts_meta) { post.email_subject = post.posts_meta.email_subject; } + + // we use post.excerpt as a hidden piece of text that is picked up by some email + // clients as a "preview" when listing emails. Our current plaintext/excerpt + // generation outputs links as "Link [https://url/]" which isn't desired in the preview + if (!post.custom_excerpt) { + post.excerpt = post.excerpt.replace(/\s\[http(.*?)\]/g, ''); + } + post.html = mobiledocLib.mobiledocHtmlRenderer.render(JSON.parse(post.mobiledoc), {target: 'email'}); // same options as used in Post model for generating plaintext but without `wordwrap: 80` // to avoid replacement strings being split across lines and for mail clients to handle diff --git a/test/api-acceptance/admin/email_preview_spec.js b/test/api-acceptance/admin/email_preview_spec.js index 510007c4fc..25cff30e0a 100644 --- a/test/api-acceptance/admin/email_preview_spec.js +++ b/test/api-acceptance/admin/email_preview_spec.js @@ -114,7 +114,7 @@ describe('Email Preview API', function () { id: ObjectId.generate(), title: 'Post with email-only card', slug: 'email-only-card', - mobiledoc: '{"version":"0.3.1","atoms":[],"cards":[],"markups":[],"sections":[[1,"p",[[0,[],0,"This is the actual post content... With an apostrophy: \'"]]],[1,"p",[]]]}', + mobiledoc: '{"version":"0.3.1","atoms":[],"cards":[],"markups":[["a",["href","https://ghost.org"]]],"sections":[[1,"p",[[0,[],0,"Testing "],[0,[0],1,"links"],[0,[],0," in email excerpt and apostrophes \'"]]]]}', html: '

This is the actual post content...

', plaintext: 'This is the actual post content...', status: 'draft', @@ -135,8 +135,12 @@ describe('Email Preview API', function () { should.exist(jsonResponse); should.exist(jsonResponse.email_previews); - jsonResponse.email_previews[0].html.should.match(/'/); - jsonResponse.email_previews[0].html.should.not.match(/'/); + const [preview] = jsonResponse.email_previews; + + preview.html.should.containEql('Testing links in email excerpt'); + + preview.html.should.match(/'/); + preview.html.should.not.match(/'/); }); }); });