/* global $ */

'use strict';

angular.module('memoApp')
  .controller('MemoCtrl', function ($scope, $rootScope, $http, $routeParams, memoService) {
    $('.nav li').removeClass('active');
    $('#nav-memos').addClass('active');

    $scope.file = $routeParams.file + '.md';
    // console.log($scope.file);

    var file = 'memos/' + $scope.file;
    var index = file.lastIndexOf('/');
    if (index !== -1) {
      $scope.dir = file.substr(0, index);
    }
    $scope.dirSplit = memoService.getDirSplit(file, true);

    $rootScope.title = $scope.dirSplit[$scope.dirSplit.length - 1].name;

    $scope.memo = null;

    memoService.watch($scope.file);

    memoService.load($scope.file, function (memo) {
      // console.log(memo);
      $scope.memo = memo;
      $scope.$apply();
    });

    $('.modal').on('show.bs.modal', function (e) {
      // Get the first line in the content as a candidate of content's title
      var content = $scope.memo.content;
      var contentLines = content.split('\n');
      var title = '';
      for (var i = 0; i < contentLines.length; i++) {
        if (contentLines[i]) {
          title = contentLines[i];
          break;
        }
      }

      // Remove '#' and spaces
      title = title.replace(/#+\s*/, '');
      // console.log(title);

      $scope.memoTitle = title;
      $scope.$apply();
    });

    $('.modal').on('shown.bs.modal', function (e) {
      $(this).find('input').focus().select();
    });

    $scope.publish = function () {
      // console.log('publish');

      $http.post('/rss/' + $scope.file + '?t=' + $scope.memoTitle);

      $('.modal').modal('hide');
    };
  });


@@ -1278,24 +1278,136 @@
 og(title);%0A%0A
+      // Get title if it is link%0A      if (title.match(/%5C%5B(.+)%5C%5D%5C(.+%5C)/)) %7B%0A        title = RegExp.$1;%0A      %7D%0A%0A
       $scope

Set proper title if it is link in markdown style