Skip to content

Commit

Permalink
add inspect helper since that was removed from handlebars-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
doowb committed Mar 15, 2017
1 parent 483cdce commit bd118d8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -56,6 +56,7 @@
"grunt-verb": "^0.2.4",
"handlebars-helper-eachitems": "^0.1.2",
"relative": "^0.1.4",
"sort-object": "^3.0.2",
"time-grunt": "^0.3.2"
},
"keywords": [
Expand Down
47 changes: 47 additions & 0 deletions test/helpers/logging.js
@@ -0,0 +1,47 @@
/**
* Assemble <http://assemble.io>
*
* Copyright (c) 2014-2017, Jon Schlinkert, Brian Woodward, contributors.
* Licensed under the MIT License (MIT).
*/

var sort = require('sort-object');

module.exports.register = function (Handlebars) {
'use strict';

Handlebars.registerHelper('inspect', function(context, options) {
var hash = options.hash || {};
var ext = hash.ext || '.html';
context = JSON.stringify(sort(context), null, 2);

// Wrap the returned JSON in either markdown code fences
// or HTML, depending on the extension.
var md = '\n```json\n' + context + '\n```';
var html = '<pre><code class="json">\n' + context + '\n</code></pre>';
var result = switchOutput(ext, md, html);
return new Handlebars.SafeString(result);
});
};

function switchOutput(ext, markdown, html) {
var output;
switch (ext) {

// return markdown
case '.markdown':
case '.md':
output = markdown;
break;

// return HTML
case '.html':
case '.htm':
output = html;
break;

default:
output = html;
}
return output;
}

0 comments on commit bd118d8

Please sign in to comment.