mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Lazyloaded cheerio dependency
- this moves cheerio to be lazyloaded - given there are many uses of cheerio in this file, I've just made a helper function to DRY up the code
This commit is contained in:
parent
b092929bba
commit
ab4c67f2d2
1 changed files with 11 additions and 6 deletions
|
@ -8,7 +8,6 @@ const {textColorForBackgroundColor, darkenToContrastThreshold} = require('@trygh
|
||||||
const {DateTime} = require('luxon');
|
const {DateTime} = require('luxon');
|
||||||
const htmlToPlaintext = require('@tryghost/html-to-plaintext');
|
const htmlToPlaintext = require('@tryghost/html-to-plaintext');
|
||||||
const tpl = require('@tryghost/tpl');
|
const tpl = require('@tryghost/tpl');
|
||||||
const cheerio = require('cheerio');
|
|
||||||
const {EmailAddressParser} = require('@tryghost/email-addresses');
|
const {EmailAddressParser} = require('@tryghost/email-addresses');
|
||||||
const {registerHelpers} = require('./helpers/register-helpers');
|
const {registerHelpers} = require('./helpers/register-helpers');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
@ -46,6 +45,12 @@ function escapeRegExp(string) {
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This aids with lazyloading the cheerio dependency
|
||||||
|
function cheerioLoad(html) {
|
||||||
|
const cheerio = require('cheerio');
|
||||||
|
return cheerio.load(html);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {string|null} Segment
|
* @typedef {string|null} Segment
|
||||||
* @typedef {object} Post
|
* @typedef {object} Post
|
||||||
|
@ -253,7 +258,7 @@ class EmailRenderer {
|
||||||
return allowedSegments;
|
return allowedSegments;
|
||||||
}
|
}
|
||||||
|
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerioLoad(html);
|
||||||
|
|
||||||
let allSegments = $('[data-gh-segment]')
|
let allSegments = $('[data-gh-segment]')
|
||||||
.get()
|
.get()
|
||||||
|
@ -316,7 +321,7 @@ class EmailRenderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let $ = cheerio.load(html);
|
let $ = cheerioLoad(html);
|
||||||
|
|
||||||
// Remove parts of the HTML not applicable to the current segment - We do this
|
// Remove parts of the HTML not applicable to the current segment - We do this
|
||||||
// before rendering the template as the preheader for the email may be generated
|
// before rendering the template as the preheader for the email may be generated
|
||||||
|
@ -412,7 +417,7 @@ class EmailRenderer {
|
||||||
// juice will explicitly set the width/height attributes to `auto` on the <img /> tag
|
// juice will explicitly set the width/height attributes to `auto` on the <img /> tag
|
||||||
// This is not supported by Outlook, so we need to reset the width/height attributes to the original values
|
// This is not supported by Outlook, so we need to reset the width/height attributes to the original values
|
||||||
// Other clients will ignore the width/height attributes and use the inlined CSS instead
|
// Other clients will ignore the width/height attributes and use the inlined CSS instead
|
||||||
$ = cheerio.load(html);
|
$ = cheerioLoad(html);
|
||||||
const originalImageSizes = $('img').get().map((image) => {
|
const originalImageSizes = $('img').get().map((image) => {
|
||||||
const src = image.attribs.src;
|
const src = image.attribs.src;
|
||||||
const width = image.attribs.width;
|
const width = image.attribs.width;
|
||||||
|
@ -429,7 +434,7 @@ class EmailRenderer {
|
||||||
html = juice(html, {inlinePseudoElements: true, removeStyleTags: true});
|
html = juice(html, {inlinePseudoElements: true, removeStyleTags: true});
|
||||||
|
|
||||||
// happens after inlining of CSS so we can change element types without worrying about styling
|
// happens after inlining of CSS so we can change element types without worrying about styling
|
||||||
$ = cheerio.load(html);
|
$ = cheerioLoad(html);
|
||||||
|
|
||||||
// Reset any `height="auto"` or `width="auto"` attributes to their original values before inlining CSS
|
// Reset any `height="auto"` or `width="auto"` attributes to their original values before inlining CSS
|
||||||
const imageTags = $('img').get();
|
const imageTags = $('img').get();
|
||||||
|
@ -797,7 +802,7 @@ class EmailRenderer {
|
||||||
// Actual template
|
// Actual template
|
||||||
const htmlTemplateSource = await fs.readFile(path.join(__dirname, './email-templates/', `template.hbs`), 'utf8');
|
const htmlTemplateSource = await fs.readFile(path.join(__dirname, './email-templates/', `template.hbs`), 'utf8');
|
||||||
this.#renderTemplate = this.#handlebars.compile(Buffer.from(htmlTemplateSource).toString());
|
this.#renderTemplate = this.#handlebars.compile(Buffer.from(htmlTemplateSource).toString());
|
||||||
|
|
||||||
return this.#renderTemplate(data);
|
return this.#renderTemplate(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue