From 413b06d1b58b2195265e938d3b090b000a395e4f Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 25 Aug 2021 09:25:55 +0100 Subject: [PATCH] Fixed leading/trailing HR removal when rendering email content refs https://github.com/TryGhost/Team/issues/1007 - `:root` selector wasn't working as expected and ended up matching HRs within content - switched to wrapping the post html inside a `` element before parsing so that we have a proper top-level element for direct child selectors to match against --- core/server/services/mega/post-email-serializer.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/core/server/services/mega/post-email-serializer.js b/core/server/services/mega/post-email-serializer.js index aca097f70d..5d527946f5 100644 --- a/core/server/services/mega/post-email-serializer.js +++ b/core/server/services/mega/post-email-serializer.js @@ -220,11 +220,16 @@ const serialize = async (postModel, options = {isBrowserPreview: false, apiVersi post.html = mobiledocLib.mobiledocHtmlRenderer.render(JSON.parse(post.mobiledoc), {target: 'email'}); // perform any email specific adjustments to the mobiledoc->HTML render output - let _cheerio = cheerio.load(post.html); + // body wrapper is required so we can get proper top-level selections + let _cheerio = cheerio.load(`${post.html}`); // remove leading/trailing HRs - _cheerio(':root > hr:first-child, :root > div:first-child > hr:first-child').remove(); - _cheerio(':root > hr:last-child, :root > div:last-child > hr:last-child').remove(); - post.html = _cheerio.html(); + _cheerio(` + body > hr:first-child, + body > hr:last-child, + body > div:first-child > hr:first-child, + body > div:last-child > hr:last-child + `).remove(); + post.html = _cheerio('body').html(); post.plaintext = htmlToPlaintext(post.html);