Skip to content

Commit

Permalink
Merge pull request #12 from mjmlio/mjml3
Browse files Browse the repository at this point in the history
MJML 3 : fix  #11
  • Loading branch information
Lohek authored Oct 21, 2016
2 parents 7d5a835 + d9e2783 commit 81ba1cb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 30 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ Add Gulp to your MJML workflow!
``` javascript

var gulp = require('gulp');
var gulp = require('gulp')
var mjml = require('gulp-mjml')

gulp.task('default', function () {
return gulp.src('./test.mjml')
.pipe(mjml())
.pipe(gulp.dest('./html'))
});
})

```

> If you have custom components linked to your own mjmlEngine, you can pass it to the gulp task so it uses your engine to render the html:
``` javascript

var gulp = require('gulp');
var gulp = require('gulp')
var mjml = require('gulp-mjml')

// Require your own components if needed, and your mjmlEngine
Expand All @@ -35,6 +35,6 @@ gulp.task('default', function () {
return gulp.src('./test.mjml')
.pipe(mjml(mjmlEngine))
.pipe(gulp.dest('./html'))
});
})

```
7 changes: 3 additions & 4 deletions examples/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

var gulp = require('gulp');
var mjml = require('../index')
var gulp = require('gulp')
var mjml = require('../src/index')

gulp.task('default', function () {
gulp.src('./test.mjml')
.pipe(mjml())
.pipe(gulp.dest('./html'))
});
})
17 changes: 17 additions & 0 deletions examples/test.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<mjml>
<mj-body>
<mj-container>
<mj-section>
<mj-column>

<mj-image width="100" src="/assets/img/logo-small.png"></mj-image>

<mj-divider border-color="#F45E43"></mj-divider>

<mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello World</mj-text>

</mj-column>
</mj-section>
</mj-container>
</mj-body>
</mjml>
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-mjml",
"version": "1.0.1",
"version": "2.0.0",
"description": "Add Gulp to your MJML workflow",
"main": "src/index",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"dependencies": {
"gulp": "^3.9.0",
"gulp-util": "^3.0.7",
"mjml": "latest",
"mjml": "^3.0.0",
"through2": "^2.0.0"
}
}
48 changes: 28 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@

var through = require ('through2')
, mjmlDefaultEngine = require ('mjml')
, gutil = require ('gulp-util');
var mjmlDefaultEngine = require ('mjml')
var gutil = require ('gulp-util')

var GulpError = gutil.PluginError,
NAME = 'MJML';
var GulpError = gutil.PluginError
var NAME = 'MJML'

module.exports = function mjml (mjmlEngine) {
if(mjmlEngine === undefined) {
mjmlEngine = mjmlDefaultEngine;
mjmlEngine = mjmlDefaultEngine
}

return through.obj(function (file, enc, callback) {
return through.obj(function (file, enc, callback) {

if (file.isStream()) {
this.emit('error', new GulpError(NAME, 'Streams are not supported!'))
return callback()
}

if (file.isBuffer()) {
var output = file.clone()
var render

if (file.isStream()) {
this.emit('error', new PluginError(NAME, 'Streams are not supported!'));
return callback()
}
try {
render = mjmlEngine.mjml2html(file.contents.toString())
} catch (e) {
this.emit('error', new GulpError(NAME, e))
return callback()
}

if (file.isBuffer()) {
var output = file.clone();
output.contents = new Buffer(mjmlEngine.mjml2html(file.contents.toString()));
output.path = gutil.replaceExtension(file.path.toString(), '.html');
this.push(output);
}
return callback();
})
};
output.contents = new Buffer(render.html)
output.path = gutil.replaceExtension(file.path.toString(), '.html')
this.push(output)
}
return callback()
})
}

0 comments on commit 81ba1cb

Please sign in to comment.