Skip to content

Instantly share code, notes, and snippets.

@expalmer
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expalmer/beca5945f19792228900 to your computer and use it in GitHub Desktop.
Save expalmer/beca5945f19792228900 to your computer and use it in GitHub Desktop.
Metalsmith Gist Embed Plugin
/**
* Module dependencies.
*/
var request = require('request');
var Q = require('q');
/**
* GistEmbed prototype.
*/
var app = GistEmbed.prototype;
/**
* Expose `plugin`.
*/
exports = module.exports = function() {
return function ( files, metalsmith, done ) {
new GistEmbed( files )
.init(function() {
done();
});
};
};
/**
* Initialize a new `GistEmbed`.
*
* @api public
*/
function GistEmbed( files ) {
this.files = files;
this.posts = [];
Object.keys( files ).forEach( function( file ) {
if ( files[file].gist ) {
this.posts.push( file ) ;
}
}.bind(this));
}
app.init = function( callback ) {
if ( !this.posts.length ) {
return callback();
}
var that = this;
var total = this.posts.length;
var isDone = function() {
total -= 1;
if ( total === 0 ) {
callback();
}
}
this.posts.forEach( function( file ) {
that.postWithGist( file, function() {
isDone();
});
});
};
app.postWithGist = function( file, callback ) {
if ( typeof this.files[file].gist === 'string' ) {
this.files[file].gist = this.helpers.toArray( this.files[file].gist );
}
var that = this;
var total = this.files[file].gist.length;
var isDone = function() {
total -= 1;
if ( total === 0 ) {
callback();
}
}
this.files[file].gist.forEach( function( gist ) {
that.requestGithubGists( gist )
.then( function( ghGist ) {
that.replacePost( file, gist, ghGist, function( res ) {
isDone();
});
}, function( err ) {
console.log('err', err);
isDone();
});
});
};
app.requestGithubGists = function( gist ) {
var deferred = Q.defer();
var url = 'https://gist.github.com/'+gist+'.json';
request( url , function ( error, response, body ) {
if ( !error && response.statusCode === 200 ) {
var res = JSON.parse(body);
deferred.resolve( res );
} else {
deferred.reject( true );
}
});
return deferred.promise;
};
app.replacePost = function ( file, gist, ghGist, callback ) {
var style = '<link rel="stylesheet" href="' + ghGist.stylesheet + '">',
value = style + ' ' + ghGist.div,
contents = this.helpers.replaceGist( this.files[file].contents, gist, value );
this.files[file].contents = new Buffer( contents );
callback('replaced ' + gist );
};
/**
* Helpers
*/
app.helpers = {
replaceGist: function( contents, gist, value ) {
return contents.toString().replace("gist:" + gist, value);
},
toArray: function( gist ) {
return gist.trim()
.replace(/([\s-,])/g,'|')
.split('|')
.filter(function( a ){ return a; } );
}
};
// page.md
---
title: "Another Foo Bar"
date: 2014-03-01
template: article.hbt
tags: ruby on rails
gist: expalmer/cfbaf085256d5902ce2d, expalmer/5f1693761a94c215cb23
---
# here I put the same gist:id that I put above in array
gist:expalmer/cfbaf085256d5902ce2d
# here too
gist:expalmer/5f1693761a94c215cb23
// index.js
var gistEmbed = require('./gist-embed');
Metalsmith(__dirname)
.use(collections({
articles: {
pattern: 'content/articles/*.md',
sortBy: 'date',
reverse: true
}
}))
.use(markdown())
.use(gistEmbed())
.. continue ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment