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

note-view-ctrl.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. var parts = $scope.getUrlParts(a[i].toString())
  43. if(parts.protocol == "file:"){
  44. a[i].className += "internal-link";
  45. }
  46. }
  47. // Syntax Highlight
  48. code = document.getElementsByTagName("code");
  49. for (var i = 0; i < code.length; i++) {
  50. hljs.highlightBlock(code[i]);
  51. }
  52. function handleAnchor(e){
  53. e.preventDefault();
  54. var r = new RegExp('^(?:[a-z]+:)?//', 'i');
  55. if(e.srcElement.protocol == "http:" || e.srcElement.protocol == "https:"){
  56. console.log("-> Prevented link from opening: " + e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
  57. var open = require("open");
  58. open(e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
  59. }
  60. if(e.srcElement.protocol == "file:"){
  61. var url = e.srcElement.outerHTML.match(/href="([^"]*)/)[1];
  62. //url = FileService.getNotesDir() + "/" + url;
  63. url = $scope.fixRelativeURL($scope.note.path, url)
  64. var note = FileService.getNote(url);
  65. FileService.setCurrentNote(note);
  66. $scope.note = note;
  67. console.log("-> Opening Link: " + note.path)
  68. $scope.loadNoteView();
  69. }
  70. }
  71. function updateContent() {
  72. // Do something with `this.responseText`
  73. }
  74. });
  75. }
  76. $scope.loadNoteView();
  77. $rootScope.$on('note-view:reload', function() {
  78. $scope.note = FileService.getCurrentNote();
  79. $scope.html_data = "";
  80. $scope.loadNoteView();
  81. });
  82. $scope.fixImgURLs = function(current_note_path, html){
  83. // var images = html.getElementsByTagName('img');
  84. // var srcList = [];
  85. // for(var i = 0; i < images.length; i++) {
  86. // console.log(images[i]);
  87. // images[i].src = $scope.fixRelativeURL(current_note_path, images[i].src);
  88. // }
  89. // page = angular.element(html);
  90. // console.log(page.find("img"))
  91. // page.find("img").each(function() {
  92. // var obj = angular.element(this);
  93. // console.log(page)
  94. // console.log(obj)
  95. // obj.attr('src') = $scope.fixRelativeURL(current_note_path, obj.attr('src'));
  96. // console.log(obj.attr('src'));
  97. // });
  98. var imgs = angular.element(html).find("img");
  99. var img_urls = []
  100. for (var i = 0; i < imgs.length; i++) {
  101. img_urls.push($scope.fixRelativeURL(current_note_path, $scope.absoluteToRelativeURL(current_note_path, imgs[i].src)));
  102. }
  103. var page_images = document.getElementsByTagName('img');
  104. console.log("-> Changing "+ img_urls.length + " images")
  105. for(var i = 0; i < img_urls.length; i++) {
  106. console.log(page_images[i]);
  107. page_images[i].src = img_urls[i];
  108. }
  109. }
  110. $scope.fixRelativeURL = function(current_url, relative_url) {
  111. console.log("-> Fixing URL")
  112. console.log(" * Relative URL: " + relative_url)
  113. console.log(" * Note URL: " + current_url)
  114. // split urls and create arrays
  115. var current_path = current_url.split('/');
  116. var relative_path = relative_url.split('/');
  117. // remove the current note's filename from the url
  118. current_path.pop();
  119. // count how many folders the relative path goes back and erase '..'
  120. var count = 0;
  121. for (var i = 0; i < relative_path.length; i++) {
  122. if(relative_path[i] == ".."){
  123. count = count + 1;
  124. relative_path[i] = "";
  125. }
  126. }
  127. // make the relative path a string again
  128. relative_path = relative_path.join('/');
  129. // remove the same count of folders from the end of the current notes url
  130. for (var i = 0; i < count; i++) {
  131. current_path.pop();
  132. }
  133. // make the current note's url a string again
  134. current_path = current_path.join('/');
  135. // add a '/' if the relative url pointed to a file or folder above the current notes root
  136. if(count == 0){
  137. var fixed_url = current_path + "/" + relative_path;
  138. } else {
  139. var fixed_url = current_path + relative_path;
  140. }
  141. // return the fixed relative url
  142. console.log(" * Fixed URL: " + fixed_url)
  143. return fixed_url;
  144. }
  145. $scope.absoluteToRelativeURL = function(current_url, absolute_url) {
  146. console.log("-> Converting absolute URL to relative")
  147. console.log(" * Absolute URL: " + absolute_url)
  148. console.log(" * Note URL: " + current_url)
  149. // split urls and create arrays
  150. var current_path = current_url.split('/');
  151. var absolute_path = $scope.getUrlParts(absolute_url).pathname.split('/');
  152. // remove the current note's filename from the url and the image filename from the url
  153. current_path.pop();
  154. current_path.shift();
  155. absolute_path.shift();
  156. // count how many folders the current path has
  157. var current_path_count = 0;
  158. for (var i = 0; i < current_path.length; i++) {
  159. current_path_count = current_path_count + 1;
  160. }
  161. // count how many folders the absolute path has
  162. var absolute_path_count = 0;
  163. for (var i = 0; i < absolute_path.length; i++) {
  164. absolute_path_count = absolute_path_count + 1;
  165. }
  166. absolute_path_count = absolute_path_count - 1;
  167. console.log(" * Cleaned current URL (" + current_path_count + " parts): " + current_path.join('/'))
  168. console.log(" * Cleaned absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  169. dif = current_path_count - (absolute_path_count -1);
  170. for (var i = 0; i < absolute_path_count; i++) {
  171. absolute_path.shift();
  172. }
  173. console.log(" * Modified current URL (" + current_path_count + " parts): " + current_path.join('/'))
  174. console.log(" * Modified absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  175. // make the relative path a string again
  176. var relative_path = absolute_path.join('/');
  177. console.log(" * Converted relative URL: " + relative_path)
  178. return relative_path;
  179. }
  180. $scope.getUrlParts = function(url) {
  181. var a = document.createElement('a');
  182. a.href = url;
  183. return {
  184. href: a.href,
  185. host: a.host,
  186. hostname: a.hostname,
  187. port: a.port,
  188. pathname: a.pathname,
  189. protocol: a.protocol,
  190. hash: a.hash,
  191. search: a.search
  192. };
  193. }
  194. }]);