Skip to content

Commit

Permalink
Merge pull request #403 from maabiddevra/master
Browse files Browse the repository at this point in the history
Added option to pass juiceResources with render and renderAll
  • Loading branch information
niftylettuce committed Sep 8, 2020
2 parents e28bf78 + e2f05e2 commit 7f2ca0f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ If this asset is in another folder, then you will need to modify the default opt
const email = new Email({
// <https://github.com/Automattic/juice>
juice: true,
// Override juice global settings <https://github.com/Automattic/juice#juicecodeblockss>
juiceSettings: {
tableElements: ['TABLE']
},
juiceResources: {
preserveImportant: true,
webResources: {
Expand Down Expand Up @@ -313,6 +317,45 @@ The example above assumes you have the following directory structure (note that

The Promise for `email.render` resolves with a String (the HTML or text rendered).

> If you need pass juiceResources in render function, with this option you don't need create Email instance every time
```js
const Email = require('email-templates');

const email = new Email();

email
.render({
path: 'mars/html',
juiceResources: {
preserveImportant: true,
webResources: {
// view folder path, it will get css from `mars/style.css`
relativeTo: path.resolve('mars')
}
}
}, {
name: 'Elon'
})
.then(console.log)
.catch(console.error);
```

The example above will be useful when you have a structure like this, this will be useful when you have a separate CSS file for every template

```sh
.
├── app.js
└── emails
└── mars
├── html.pug
├── text.pug
├── subject.pug
└── style.css
```

The Promise for `email.render` resolves with a String (the HTML or text rendered).

> If you need to render all available template files for a given email template (e.g. `html.pug`, `text.pug`, and `subject.pug` – you can use `email.renderAll` (this is the method that `email.send` uses).
```js
Expand Down
52 changes: 43 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class Email {
subjectPrefix: false,
// <https://github.com/Automattic/juice>
juice: true,
// Override juice global settings <https://github.com/Automattic/juice#juicecodeblockss>
juiceSettings: {
tableElements: ['TABLE']
},
juiceResources: {
preserveImportant: true,
webResources: {
Expand All @@ -113,6 +117,13 @@ class Email {
if (!_.isFunction(this.config.transport.sendMail))
this.config.transport = nodemailer.createTransport(this.config.transport);

// Override juice global settings https://github.com/Automattic/juice#juicecodeblocks
if (_.isObject(this.config.juiceSettings)) {
_.forEach(this.config.juiceSettings, (key, value) => {
juice[key] = value;
});
}

debug('transformed config %O', this.config);

this.juiceResources = this.juiceResources.bind(this);
Expand All @@ -126,12 +137,20 @@ class Email {

// shorthand use of `juiceResources` with the config
// (mainly for custom renders like from a database)
juiceResources(html) {
return juiceResources(html, this.config.juiceResources);
juiceResources(html, juiceRenderResources = {}) {
const juiceR = _.merge(this.config.juiceResources, juiceRenderResources);
return juiceResources(html, juiceR);
}

// a simple helper function that gets the actual file path for the template
async getTemplatePath(template) {
let juiceRenderResources = {};

if (_.isObject(template)) {
juiceRenderResources = template.juiceResources;
template = template.path;
}

const [root, view] = path.isAbsolute(template)
? [path.dirname(template), path.basename(template)]
: [this.config.views.root, template];
Expand All @@ -141,7 +160,7 @@ class Email {
this.config.views.options.extension
);
const filePath = path.resolve(root, paths.rel);
return { filePath, paths };
return { filePath, paths, juiceRenderResources };
}

// returns true or false if a template exists
Expand All @@ -159,24 +178,39 @@ class Email {
}

async checkAndRender(type, template, locals) {
let juiceRenderResources = {};

if (_.isObject(template)) {
juiceRenderResources = template.juiceResources;
template = template.path;
}

const str = this.config.getPath(type, template, locals);
if (!this.config.customRender) {
const exists = await this.templateExists(str);
if (!exists) return;
}

return this.render(str, {
...locals,
...(type === 'html' ? {} : { pretty: false })
});
return this.render(
str,
{
...locals,
...(type === 'html' ? {} : { pretty: false })
},
juiceRenderResources
);
}

// promise version of consolidate's render
// inspired by koa-views and re-uses the same config
// <https://github.com/queckezz/koa-views>
async render(view, locals = {}) {
const { map, engineSource } = this.config.views.options;
const { filePath, paths } = await this.getTemplatePath(view);
const {
filePath,
paths,
juiceRenderResources
} = await this.getTemplatePath(view);
if (paths.ext === 'html' && !map) {
const res = await readFile(filePath, 'utf8');
return res;
Expand Down Expand Up @@ -217,7 +251,7 @@ class Email {
// google now supports media queries
// https://developers.google.com/gmail/design/reference/supported_css
if (!this.config.juice) return res;
const html = await this.juiceResources(res);
const html = await this.juiceResources(res, juiceRenderResources);
return html;
}

Expand Down

0 comments on commit 7f2ca0f

Please sign in to comment.