Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Consolidate rather than jade #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

var jade = require('jade');
var consolidate = require('consolidate');
var shortcodes = require('shortcode-parser');
var debug = require('debug')('metalsmith-shortcodes');
var each = require('async').each;
var extend = require('extend');
var omit = require('lodash.omit');
var join = require('path').join;
var match = require('multimatch');
var fs = require('fs');
Expand Down Expand Up @@ -40,8 +41,19 @@ var SHORTCODE_RE = /\[\s*([^\/][A-Za-z0-9\-_]*)/g
function plugin(opts){
opts = opts || {};

var settings = [
'directory',
'pattern',
'engine',
'ext'
];

var dir = opts.directory || 'templates';
var pattern = opts.pattern;
var engine = opts.engine || 'jade';
var ext = opts.extension || '.jade';

var consolidateParams = omit(opts, settings);

return function(files, metalsmith, done){

Expand Down Expand Up @@ -71,7 +83,7 @@ function plugin(opts){
var codes = [], c
while (c = SHORTCODE_RE.exec(str)) codes.push(c[1]);
codes.forEach( function(code){
var tmpl = metalsmith.join(dir, code + ".jade")
var tmpl = join(dir, code + ext)
// if (shortcodes._shortcodes[code]) return; // need shortcodes.has()
debug('found shortcode: %s', code);
try {
Expand All @@ -81,8 +93,20 @@ function plugin(opts){
return;
}
shortcodes.add(code, function(buf,attrs,extra){
var obj = extend({}, extra, attrs, {innerText: buf.toString()});
return jade.renderFile(tmpl, obj);
var obj = extend({}, extra, attrs, consolidateParams, metalsmith.metadata(), {innerText: buf.toString()});
// very dodgy code to force synchronous execution for consolidate
var ret;
consolidate[engine](tmpl, obj, function(err, html){
if (err) {
ret = false;
throw err;
}
ret = html;
});
while(ret === undefined) {
// do nothing
}
return ret;
})
});
return codes;
Expand Down
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
"name": "metalsmith-shortcodes",
"description": "A metalsmith plugin to render wordpress-esque shortcodes via templates.",
"repository": "git://github.com/ericgj/metalsmith-shortcodes.git",
"version": "0.0.2",
"version": "0.1.0",
"author": "Eric Gjertsen",
"license": "MIT",
"main": "lib/index.js",
"dependencies": {
"jade": "~1.3.0",
"shortcode-parser": "0.0.1",
"async": "~0.2.10",
"debug": "~0.7.4",
"extend": "~1.2.1",
"multimatch": "^0.1.0"
"async": "~2.0.0",
"consolidate": "^0.14.1",
"debug": "~2.2.0",
"extend": "~3.0.0",
"lodash.omit": "^4.3.0",
"multimatch": "^2.1.0",
"shortcode-parser": "0.0.1"
},
"devDependencies": {
"mocha": "1.x",
"metalsmith": "0.x",
"assert-dir-equal": "0.0.1"
"assert-dir-equal": "1.0.1",
"jade": "^1.11.0",
"metalsmith": "2.x",
"mocha": "2.x"
}
}