Skip to content

Instantly share code, notes, and snippets.

@chrissy-dev
Last active September 22, 2017 15:48
Show Gist options
  • Save chrissy-dev/e30d3540c46bfa52854f6bdd5fa3486b to your computer and use it in GitHub Desktop.
Save chrissy-dev/e30d3540c46bfa52854f6bdd5fa3486b to your computer and use it in GitHub Desktop.
Sample Gulpfile to compile liquid (Jekyll) templates with Front Matter.
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