Skip to content

Instantly share code, notes, and snippets.

@mlconnor
Created January 23, 2015 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mlconnor/7e2653f81d396f10c2dc to your computer and use it in GitHub Desktop.
Save mlconnor/7e2653f81d396f10c2dc to your computer and use it in GitHub Desktop.
A simple front matter (YAML) with Handlebars template. Pass the name of the file into the program.
yaml = require('js-yaml');
fs = require('fs');
Handlebars = require('hbs');
Handlebars.registerHelper("sep", function(options){
if(options.data.last) {
return options.inverse();
} else {
return options.fn();
}
});
try {
var fileStr = fs.readFileSync(process.argv[2], {encoding:'utf8'});
var docStruct = fileStr.match(/^---\n([\s\S]+)---([\s\S]+)$/);
var frontMatterStr = docStruct[1];
var frontMatter = yaml.safeLoad(frontMatterStr);
var template = docStruct[2];
var cTemplate = Handlebars.compile(template);
var result = cTemplate(frontMatter);
console.log(result);
} catch (e) {
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment