Last active
September 22, 2017 15:48
-
-
Save chrissy-dev/e30d3540c46bfa52854f6bdd5fa3486b to your computer and use it in GitHub Desktop.
Sample Gulpfile to compile liquid (Jekyll) templates with Front Matter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var engine = new Liquid.Engine, | |
es = require('event-stream'), | |
frontmatter = require('gulp-front-matter'), | |
fs = require('fs'), | |
Liquid = require('liquid-node'), | |
engine.registerFileSystem(new Liquid.LocalFileSystem('./_includes')) | |
gulp.task('compile', function() { | |
return gulp.src(['./page.html']) | |
// only compile pages that have changed | |
.pipe(changed('./_site/', { | |
extension: '.html' | |
})) | |
// get the frontmatter, accessible via file.meta | |
.pipe(frontmatter({ | |
property: 'meta' | |
})).pipe(es.map(function(file, cb) { | |
// if layout is defined in the frontmatter, if not use default.html | |
if (file.meta.layout) { | |
var template = String(fs.readFileSync('./_layouts/' + file.meta.layout + '.html')) | |
} else { | |
var template = String(fs.readFileSync('./_layouts/default.html')) | |
} | |
// run the main layout through node-liquid putting frontmatter in 'page' namespace. | |
var mainLayout = engine.parseAndRender(template, { | |
page: file.meta, | |
content: String(file.contents) | |
}).then(function(result) { | |
// compile page content with no namespace on the frontmatter | |
var mainLayout = engine.parseAndRender(result, file.meta).then(function(final) { | |
file.contents = new Buffer(final) | |
cb(null, file) | |
}) | |
}) | |
})) | |
.pipe(gulp.dest('./_site/')) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment