crowi.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* jshint browser: true, jquery: true */
  2. /* global FB, marked */
  3. /* Author: Sotaro KARASAWA <sotarok@crocos.co.jp>
  4. */
  5. var hljs = require('highlight.js');
  6. var marked = require('marked');
  7. var Crowi = {};
  8. if (!window) {
  9. window = {};
  10. }
  11. window.Crowi = Crowi;
  12. Crowi.createErrorView = function(msg) {
  13. $('#main').prepend($('<p class="alert-message error">' + msg + '</p>'));
  14. };
  15. Crowi.linkPath = function(revisionPath) {
  16. var $revisionPath = revisionPath || '#revision-path';
  17. var $title = $($revisionPath);
  18. if (!$title.get(0)) {
  19. return;
  20. }
  21. var path = '';
  22. var pathHtml = '';
  23. var splittedPath = $title.html().split(/\//);
  24. splittedPath.shift();
  25. splittedPath.forEach(function(sub) {
  26. path += '/';
  27. pathHtml += ' <a href="' + path + '">/</a> ';
  28. if (sub) {
  29. path += sub;
  30. pathHtml += '<a href="' + path + '">' + sub + '</a>';
  31. }
  32. });
  33. if (path.substr(-1, 1) != '/') {
  34. path += '/';
  35. pathHtml += ' <a href="' + path + '" class="last-path">/</a>';
  36. }
  37. $title.html(pathHtml);
  38. };
  39. Crowi.correctHeaders = function(contentId) {
  40. // h1 ~ h6 の id 名を補正する
  41. var $content = $(contentId || '#revision-body-content');
  42. var i = 0;
  43. $('h1,h2,h3,h4,h5,h6', $content).each(function(idx, elm) {
  44. var id = 'head' + i++;
  45. $(this).attr('id', id);
  46. $(this).addClass('revision-head');
  47. $(this).append('<span class="revision-head-link"><a href="#' + id +'"><i class="fa fa-link"></i></a></span>');
  48. });
  49. };
  50. Crowi.revisionToc = function(contentId, tocId) {
  51. var $content = $(contentId || '#revision-body-content');
  52. var $tocId = $(tocId || '#revision-toc');
  53. var $tocContent = $('<div id="revision-toc-content" class="revision-toc-content collapse"></div>');
  54. $tocId.append($tocContent);
  55. $('h1', $content).each(function(idx, elm) {
  56. var id = $(this).attr('id');
  57. var title = $(this).text();
  58. var selector = '#' + id + ' ~ h2:not(#' + id + ' ~ h1 ~ h2)';
  59. var $toc = $('<ul></ul>');
  60. var $tocLi = $('<li><a href="#' + id +'">' + title + '</a></li>');
  61. $tocContent.append($toc);
  62. $toc.append($tocLi);
  63. $(selector).each(function()
  64. {
  65. var id2 = $(this).attr('id');
  66. var title2 = $(this).text();
  67. var selector2 = '#' + id2 + ' ~ h3:not(#' + id2 + ' ~ h2 ~ h3)';
  68. var $toc2 = $('<ul></ul>');
  69. var $tocLi2 = $('<li><a href="#' + id2 +'">' + title2 + '</a></li>');
  70. $tocLi.append($toc2);
  71. $toc2.append($tocLi2);
  72. $(selector2).each(function()
  73. {
  74. var id3 = $(this).attr('id');
  75. var title3 = $(this).text();
  76. var $toc3 = $('<ul></ul>');
  77. var $tocLi3 = $('<li><a href="#' + id3 +'">' + title3 + '</a></li>');
  78. $tocLi2.append($toc3);
  79. $toc3.append($tocLi3);
  80. });
  81. });
  82. });
  83. };
  84. Crowi.escape = function(s) {
  85. s = s.replace(/&/g, '&amp;')
  86. .replace(/</g, '&lt;')
  87. .replace(/>/g, '&gt;')
  88. .replace(/'/g, '&#39;')
  89. .replace(/"/g, '&quot;')
  90. ;
  91. return s;
  92. };
  93. Crowi.unescape = function(s) {
  94. s = s.replace(/&nbsp;/g, ' ')
  95. .replace(/&amp;/g, '&')
  96. .replace(/&lt;/g, '<')
  97. .replace(/&gt;/g, '>')
  98. .replace(/&#39;/g, '\'')
  99. .replace(/&quot;/g, '"')
  100. ;
  101. return s;
  102. };
  103. Crowi.getRendererType = function() {
  104. return new Crowi.rendererType.markdown();
  105. };
  106. Crowi.rendererType = {};
  107. Crowi.rendererType.markdown = function(){};
  108. Crowi.rendererType.markdown.prototype = {
  109. render: function(contentText) {
  110. marked.setOptions({
  111. gfm: true,
  112. highlight: function (code, lang, callback) {
  113. var result, hl;
  114. if (lang) {
  115. try {
  116. hl = hljs.highlight(lang, code);
  117. result = hl.value;
  118. } catch (e) {
  119. result = code;
  120. }
  121. } else {
  122. //result = hljs.highlightAuto(code);
  123. //callback(null, result.value);
  124. result = code;
  125. }
  126. return callback(null, result);
  127. },
  128. tables: true,
  129. breaks: true,
  130. pedantic: false,
  131. sanitize: false,
  132. smartLists: true,
  133. smartypants: false,
  134. langPrefix: 'lang-'
  135. });
  136. var contentHtml = Crowi.unescape(contentText);
  137. contentHtml = this.expandImage(contentHtml);
  138. contentHtml = this.link(contentHtml);
  139. var $body = this.$revisionBody;
  140. // Using async version of marked
  141. marked(contentHtml, {}, function (err, content) {
  142. if (err) {
  143. throw err;
  144. }
  145. $body.html(content);
  146. });
  147. },
  148. link: function (content) {
  149. return content
  150. //.replace(/\s(https?:\/\/[\S]+)/g, ' <a href="$1">$1</a>') // リンク
  151. .replace(/\s<((\/[^>]+?){2,})>/g, ' <a href="$1">$1</a>') // ページ間リンク: <> でかこまれてて / から始まり、 / が2個以上
  152. ;
  153. },
  154. expandImage: function (content) {
  155. return content.replace(/\s(https?:\/\/[\S]+\.(jpg|jpeg|gif|png))/g, ' <a href="$1"><img src="$1" class="auto-expanded-image"></a>');
  156. }
  157. };
  158. Crowi.renderer = function (contentText, revisionBody) {
  159. var $revisionBody = revisionBody || $('#revision-body-content');
  160. this.contentText = contentText;
  161. this.$revisionBody = $revisionBody;
  162. this.format = 'markdown'; // とりあえず
  163. this.renderer = Crowi.getRendererType();
  164. this.renderer.$revisionBody = this.$revisionBody;
  165. };
  166. Crowi.renderer.prototype = {
  167. render: function() {
  168. this.renderer.render(this.contentText);
  169. }
  170. };
  171. $(function() {
  172. Crowi.linkPath();
  173. $('[data-toggle="tooltip"]').tooltip();
  174. $('[data-tooltip-stay]').tooltip('show');
  175. $('.copy-link').on('click', function () {
  176. $(this).select();
  177. });
  178. $('#createMemo').on('shown.bs.modal', function (e) {
  179. $('#memoName').focus();
  180. });
  181. $('#createMemoForm').submit(function(e)
  182. {
  183. var prefix = $('[name=memoNamePrefix]', this).val();
  184. var name = $('[name=memoName]', this).val();
  185. if (name === '') {
  186. prefix = prefix.slice(0, -1);
  187. }
  188. top.location.href = prefix + name;
  189. return false;
  190. });
  191. $('#renamePage').on('shown.bs.modal', function (e) {
  192. $('#newPageName').focus();
  193. });
  194. $('#renamePageForm').submit(function(e) {
  195. var path = $('#pagePath').html();
  196. $.ajax({
  197. type: 'POST',
  198. url: '/_api/page_rename' + path,
  199. data: $('#renamePageForm').serialize(),
  200. dataType: 'json'
  201. }).done(function(data) {
  202. if (!data.status) {
  203. $('#newPageNameCheck').html('<i class="fa fa-times-circle"></i> ' + data.message);
  204. $('#newPageNameCheck').addClass('alert-danger');
  205. } else {
  206. $('#newPageNameCheck').removeClass('alert-danger');
  207. $('#newPageNameCheck').html('<img src="/images/loading_s.gif"> 移動しました。移動先にジャンプします。');
  208. setTimeout(function() {
  209. top.location.href = data.page.path + '?renamed=' + path;
  210. }, 1000);
  211. }
  212. });
  213. return false;
  214. });
  215. });