Skip to content

Instantly share code, notes, and snippets.

@pfulton
Last active August 29, 2015 14:05
Show Gist options
  • Save pfulton/7ea6aab3131d509bf49f to your computer and use it in GitHub Desktop.
Save pfulton/7ea6aab3131d509bf49f to your computer and use it in GitHub Desktop.
gray-matter + Assemble to output front matter in pattern library partials
/**
* Handlebars Helpers for Pattern Lab
* Copyright (c) 2014 Jon Schlinkert
* Licensed under the MIT License (MIT).
*/
'use strict';
var path = require('path');
var file = require('fs-utils');
var _ = require('lodash');
var matter = require('gray-matter');
var inspectProperties = function(obj){
var str = '';
for(var k in obj){
if(obj.hasOwnProperty(k)){
str += k+ ": " + obj[k] + "\n";
}
}
return str;
}
module.exports.register = function (Handlebars, options, params) {
options = options || {};
var config = _.extend(options, options.data || {});
var patterns = ['atom', 'molecule', 'organism'];
patterns.forEach(function(pattern) {
var inflection = pattern + 's';
file.expand(options.patterns[inflection]).map(function(filepath) {
var name = pattern + '-' + file.base(filepath);
var template = file.readFileSync(filepath);
Handlebars.registerPartial(name, template);
});
Handlebars.registerHelper(pattern, function(name, context) {
context = _.extend(config, context || {});
var template = Handlebars.partials[pattern + '-' + name];
var frontMatter = matter(template);
// the source of the partial without front-matter
var body = frontMatter.content;
// the data from lowest-to-highest priority being overridden in front-matter
var data = _.extend(context, frontMatter.data, this, options.hash);
var fn = Handlebars.compile(body);
return new Handlebars.SafeString(fn(data));
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment