2
0

crowi.js 6.5 KB

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