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

note-view-ctrl.js 7.6KB

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