Created
January 23, 2015 20:29
-
-
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.
This file contains 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
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