all files / swig-email-templates/ index.js

96.08% Statements 49/51
92.86% Branches 26/28
100% Functions 9/9
100% Lines 49/49
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                            11× 10×                                   12× 12× 11×                                            
'use strict';
 
var path = require('path');
var fs = require('fs');
var swig = require('swig');
var juice = require('juice');
var cheerio = require('cheerio');
var htmlToText = require('html-to-text');
 
var EmailTemplates = function (options) {
 
  var self = this;
 
  options = options || {}
  options.root = options.root || path.join(__dirname, 'templates');
  options.juice = options.juice || {};
  options.juice.webResources = options.juice.webResources || {};
  options.juice.webResources.relativeTo = options.juice.webResources.relativeTo || options.root;
 
  swig.setDefaults(options.swig);
 
  if (options.filters) {
    for (var filter in options.filters) {
      swig.setFilter(filter, options.filters[filter]);
    }
  }
 
 
  /*
   * (Internal) Compile and render a swig template
   */
  this.useTemplate = function (templatePath, context) {
    var template = swig.compileFile(templatePath);
    return template(context);
  }
 
 
  /*
   * (Internal) Generate text counterpart to HTML template
   */
  this.generateText = function (templatePath, context, html, cb) {
    if (options.hasOwnProperty('text') && !options.text)
      return cb(null, null);
 
    var textFilename = path.basename(templatePath, path.extname(templatePath)) + '.txt';
    var textPath = path.resolve(path.dirname(templatePath), textFilename);
 
    fs.exists(textPath, function(exists) {
      if (exists) {
        cb(null, self.useTemplate(textPath, context));
      } else {
        cb(null, htmlToText.fromString(html));
      }
    });
  }
 
 
  /*
   * (Internal) Rewrite URLs in a Cheerio doc using a given function
   */
  this.rewriteUrls = function ($, rewrite) {
    $("a").each(function(idx, anchor) {
      var href = $(anchor).attr('href');
      if (href !== undefined) {
        $(anchor).attr('href', rewrite(href));
      }
    });
  }
 
 
  /*
   * Render a template given 'templateName' and context 'context'.
   */
  this.render = function (templateName, context, cb) {
    var templatePath = path.resolve(options.root, templateName);
 
    context = context || {};
 
    try {
      var html = self.useTemplate(templatePath, context);
      var $ = cheerio.load(html);
      if (options.rewriteUrl)
        self.rewriteUrls($, options.rewriteUrl);
    } catch (err) {
      return cb(err);
    }
 
    // Inline resources
    juice.juiceResources($.html(), options.juice, function(err, inlinedHTML) {
      Iif (err) return cb(err);
 
      self.generateText(templatePath, context, html, function(err, text) {
        Iif (err) return cb(err);
 
        cb(null, inlinedHTML, text);
      });
    });
  }
}
 
module.exports = EmailTemplates;