Desktop markdown wiki app. Built with node, Electron Framework and AngularJS.

note-view-ctrl.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @ngdoc function
  3. * @name domainManagerApp.controller:AboutCtrl
  4. * @description
  5. * # AboutCtrl
  6. * Controller of the domainManagerApp
  7. */
  8. angular.module('codexApp.noteView', [])
  9. .controller('NoteViewCtrl',['$scope', '$rootScope', '$state', 'FileService', function ($scope, $rootScope, $state, FileService) {
  10. var marked = require('marked');
  11. marked.setOptions({
  12. renderer: new marked.Renderer(),
  13. gfm: true,
  14. tables: true,
  15. breaks: true,
  16. pedantic: true,
  17. sanitize: false,
  18. smartLists: true,
  19. smartypants: true
  20. });
  21. var filesystem = require("fs");
  22. console.log('-> Note View opened!')
  23. $scope.note = FileService.getCurrentNote();
  24. $scope.container = "note-container";
  25. $scope.html_data = "";
  26. $scope.loadNoteView = function() {
  27. filesystem.readFile($scope.note.path, function(err, data) {
  28. //$scope.note.data = String.fromCharCode.apply(null, data);
  29. $scope.note.data = new Buffer(data).toString('utf8')
  30. if(!$scope.$$phase) {
  31. $scope.$apply(function(){
  32. $scope.html_data = marked($scope.note.data);
  33. });
  34. } else {
  35. $scope.html_data = marked($scope.note.data);
  36. }
  37. //console.log($scope.raw_data);
  38. $scope.fixImgURLs($scope.note.path, $scope.html_data);
  39. var a = document.getElementsByTagName('a'), ajax;
  40. for (var i=0; i<a.length; ++i) {
  41. a[i].addEventListener('click', handleAnchor, false);
  42. }
  43. // Syntax Highlight
  44. code = document.getElementsByTagName("code");
  45. for (var i = 0; i < code.length; i++) {
  46. hljs.highlightBlock(code[i]);
  47. }
  48. function handleAnchor(e){
  49. e.preventDefault();
  50. var r = new RegExp('^(?:[a-z]+:)?//', 'i');
  51. if(e.srcElement.protocol == "http:"){
  52. console.log("-> Prevented link from opening: " + e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
  53. var open = require("open");
  54. open(e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
  55. }
  56. if(e.srcElement.protocol == "file:"){
  57. var url = e.srcElement.outerHTML.match(/href="([^"]*)/)[1];
  58. var note = FileService.getNote(url);
  59. FileService.setCurrentNote(note);
  60. $scope.note = note;
  61. console.log("-> Opening Link: " + note.path)
  62. $scope.loadNoteView();
  63. }
  64. }
  65. function updateContent() {
  66. // Do something with `this.responseText`
  67. }
  68. });
  69. }
  70. $scope.loadNoteView();
  71. $rootScope.$on('note-view:reload', function() {
  72. $scope.note = FileService.getCurrentNote();
  73. $scope.html_data = "";
  74. $scope.loadNoteView();
  75. });
  76. $scope.fixImgURLs = function(current_note_path, html){
  77. // var images = html.getElementsByTagName('img');
  78. // var srcList = [];
  79. // for(var i = 0; i < images.length; i++) {
  80. // console.log(images[i]);
  81. // images[i].src = $scope.fixRelativeURL(current_note_path, images[i].src);
  82. // }
  83. // page = angular.element(html);
  84. // console.log(page.find("img"))
  85. // page.find("img").each(function() {
  86. // var obj = angular.element(this);
  87. // console.log(page)
  88. // console.log(obj)
  89. // obj.attr('src') = $scope.fixRelativeURL(current_note_path, obj.attr('src'));
  90. // console.log(obj.attr('src'));
  91. // });
  92. var imgs = angular.element(html).find("img");
  93. var img_urls = []
  94. for (var i = 0; i < imgs.length; i++) {
  95. img_urls.push($scope.fixRelativeURL(current_note_path, $scope.absoluteToRelativeURL(current_note_path, imgs[i].src)));
  96. }
  97. var page_images = document.getElementsByTagName('img');
  98. console.log("-> Changing "+ img_urls.length + " images")
  99. console.log(page_images)
  100. for(var i = 0; i < img_urls.length; i++) {
  101. console.log(page_images[i]);
  102. page_images[i].src = img_urls[i];
  103. }
  104. }
  105. $scope.fixRelativeURL = function(current_url, relative_url) {
  106. console.log("-> Fixing URL")
  107. console.log(" * Relative URL: " + relative_url)
  108. console.log(" * Note URL: " + current_url)
  109. // split urls and create arrays
  110. var current_path = current_url.split('/');
  111. var relative_path = relative_url.split('/');
  112. // remove the current note's filename from the url
  113. current_path.pop();
  114. // count how many folders the relative path goes back and erase '..'
  115. var count = 0;
  116. for (var i = 0; i < relative_path.length; i++) {
  117. if(relative_path[i] == ".."){
  118. count = count + 1;
  119. relative_path[i] = "";
  120. }
  121. }
  122. // make the relative path a string again
  123. relative_path = relative_path.join('/');
  124. // remove the same count of folders from the end of the current notes url
  125. for (var i = 0; i < count; i++) {
  126. current_path.pop();
  127. }
  128. // make the current note's url a string again
  129. current_path = current_path.join('/');
  130. // add a '/' if the relative url pointed to a file or folder above the current notes root
  131. if(count == 0){
  132. var fixed_url = current_path + "/" + relative_path;
  133. } else {
  134. var fixed_url = current_path + relative_path;
  135. }
  136. // return the fixed relative url
  137. console.log(" * Fixed URL: " + fixed_url)
  138. return fixed_url;
  139. }
  140. $scope.absoluteToRelativeURL = function(current_url, absolute_url) {
  141. console.log("-> Converting absolute URL to relative")
  142. console.log(" * Absolute URL: " + absolute_url)
  143. console.log(" * Note URL: " + current_url)
  144. // split urls and create arrays
  145. var current_path = current_url.split('/');
  146. var absolute_path = $scope.getUrlParts(absolute_url).pathname.split('/');
  147. // remove the current note's filename from the url and the image filename from the url
  148. current_path.pop();
  149. current_path.shift();
  150. absolute_path.shift();
  151. // count how many folders the current path has
  152. var current_path_count = 0;
  153. for (var i = 0; i < current_path.length; i++) {
  154. current_path_count = current_path_count + 1;
  155. }
  156. // count how many folders the absolute path has
  157. var absolute_path_count = 0;
  158. for (var i = 0; i < absolute_path.length; i++) {
  159. absolute_path_count = absolute_path_count + 1;
  160. }
  161. absolute_path_count = absolute_path_count - 1;
  162. console.log(" * Cleaned current URL (" + current_path_count + " parts): " + current_path.join('/'))
  163. console.log(" * Cleaned absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  164. dif = current_path_count - (absolute_path_count -1);
  165. for (var i = 0; i < absolute_path_count; i++) {
  166. absolute_path.shift();
  167. }
  168. console.log(" * Modified current URL (" + current_path_count + " parts): " + current_path.join('/'))
  169. console.log(" * Modified absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  170. // make the relative path a string again
  171. var relative_path = absolute_path.join('/');
  172. console.log(" * Converted relative URL: " + relative_path)
  173. return relative_path;
  174. }
  175. $scope.getUrlParts = function(url) {
  176. var a = document.createElement('a');
  177. a.href = url;
  178. return {
  179. href: a.href,
  180. host: a.host,
  181. hostname: a.hostname,
  182. port: a.port,
  183. pathname: a.pathname,
  184. protocol: a.protocol,
  185. hash: a.hash,
  186. search: a.search
  187. };
  188. }
  189. }]);