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

note-view-ctrl.js 7.5KB

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