AngularJS Posting Interface for your GitHub Jekyll Site
After getting a decent way to create a file on GitHub just using a POST from a form as Brock Boland spoke about with his Jekyll posting interface, I thought I would take this one step further and figure out a way to use the GitHub API to create that file instead of taking you to GitHub’s new file interface. This was much easier than I expected.
GitHub.js
I did a little searching for AngularJS and GitHub authentication and came across GitHub.js which (to my delight and surprise) is now being maintained by none other than my friends at DevelopmentSeed! This gives the browser a complete javascript library for working with the GitHub API and is simply perfect for the job.
I added a username and password field to my post interface and setup an AngularJS controller to utilize this library.
var app = angular.module('app', ['app.filters']);
app.controller('AuthCtrl', function($scope, $log) {
$scope.username = '';
$scope.password = '';
$scope.success = false;
$scope.auth = function() {
$scope.github = new Github({
username: $scope.username,
password: $scope.password,
auth: "basic"
});
}
});
$scope.github
now has access to all of the nifty functions that GitHub.js provides. You can see the full controller which uses the GitHub.getRepo
and repo.write
in order to create a new file in my _posts directory in markdown format with the appropriate YAML headers. GitHub then runs it through Jekyll and the result is what you’re reading now.
Updates
2013-09-08: I’ve since turned this into a full-fledged project called hublog-ng and taken Pascal Precht’s advice and incorporated his angular-github-adapter which taught me a bit about Angular’s promises feature.
Related Posts
A Year of Gratitude
It’s funny how perspective changes everything. As I sit here reflecting during Thanksgiving week, I’m struck by just how different things feel compared to a year ago. Sometimes you don’t...
Jenney Journal, part 32
Things have been going really well as Jenney heals at home. She’s back to doing everything she was always able to do, and our latest scans have shown no signs...
Jenney Journal, part 31
On Oct 21, Jenney started to get nauseous and diarrhea. She became very dehydrated quickly, and had a seizure. I called our oncologist who said we should call 911 and...