All files / src Email.js

100% Statements 26/26
100% Branches 2/2
100% Functions 2/2
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 821x 1x 1x 1x 1x 1x 1x 1x 1x   1x                         4x 4x                   1x     1x     1x     1x 1x 1x 1x         1x     1x   1x               3x   3x               1x           1x  
const head = require('./templates/head');
const header = require('./templates/header');
const intro = require('./templates/intro');
const entry = require('./templates/entry');
const entryDivider = require('./templates/entry-divider');
const outro = require('./templates/outro');
const foot = require('./templates/foot');
const mjmlLib = require('mjml');
const stampit = require('stampit');
 
const Email = stampit({
  props: {
    config: undefined,
    feeds: undefined,
    mjmlContent: undefined,
  },
 
  /**
   * Instantiate a new email object
   * @param {{object, array}} config, feeds
   * @return {void}
   */
  init({ config, feeds }) {
    this.config = config;
    this.feeds = feeds;
  },
 
  methods: {
    /**
     * Generate mjmlContent from the config and feeds set in the constructor
     * @returns {Email}
     */
    generate() {
      // Initialize the template
      this.mjmlContent = head(this.config);
 
      // Add header
      this.mjmlContent += header(this.config);
 
      // Add intro
      this.mjmlContent += intro(this.config);
 
      // Add each feed's item to the email
      this.feeds.forEach((feed) => {
        this.mjmlContent += entryDivider(feed);
        feed.items.forEach((item) => {
          this.mjmlContent += entry(item);
        });
      });
 
      // Add outro
      this.mjmlContent += outro(this.config);
 
      // Close template
      this.mjmlContent += foot(this.config);
 
      return this;
    },
 
    /**
     * Get the MJML email as a string.
     * @return {string}
     */
    getMjml() {
      this.mjmlContent || this.generate();
 
      return this.mjmlContent;
    },
 
    /**
     * Get the HTML email as a string.
     * @return {string}
     */
    getHtml() {
      return mjmlLib(this.getMjml()).html;
    },
  },
});
 
 
module.exports = Email;