AngularJS Posting Interface for your GitHub Jekyll Site

blog

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.