crowi.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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++ + '-' + $(this).text().replace(/;|:|\/|\(|\)|\s|\?|\!|\.|\+|\*|\-|\=|\#|\~|\&|\^/g, '');
  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, '&quot;')
  83. ;
  84. return s;
  85. };
  86. Crowi.unescape = function(s) {
  87. s = s.replace(/&nbsp;/g, ' ')
  88. .replace(/&amp;/g, '&')
  89. .replace(/&lt;/g, '<')
  90. .replace(/&gt;/g, '>')
  91. .replace(/&quot;/g, '"')
  92. ;
  93. return s;
  94. };
  95. Crowi.getRendererType = function() {
  96. return new Crowi.rendererType.markdown();
  97. };
  98. Crowi.rendererType = {};
  99. Crowi.rendererType.markdown = function(){};
  100. Crowi.rendererType.markdown.prototype = {
  101. render: function(contentText) {
  102. marked.setOptions({
  103. gfm: true,
  104. highlight: function (code, lang, callback) {
  105. var result;
  106. if (lang) {
  107. result = hljs.highlight(lang, code);
  108. } else {
  109. result = hljs.highlightAuto(code);
  110. }
  111. callback(null, result.value);
  112. },
  113. tables: true,
  114. breaks: true,
  115. pedantic: false,
  116. sanitize: false,
  117. smartLists: true,
  118. smartypants: false,
  119. langPrefix: 'lang-'
  120. });
  121. var contentHtml = Crowi.unescape(contentText);
  122. contentHtml = this.expandImage(contentHtml);
  123. contentHtml = this.link(contentHtml);
  124. var $body = this.$revisionBody;
  125. // Using async version of marked
  126. marked(contentHtml, {}, function (err, content) {
  127. if (err) {
  128. throw err;
  129. }
  130. $body.html(content);
  131. });
  132. },
  133. link: function (content) {
  134. return content
  135. //.replace(/\s(https?:\/\/[\S]+)/g, ' <a href="$1">$1</a>') // リンク
  136. .replace(/\s<((\/[^>]+?){2,})>/g, ' <a href="$1">$1</a>') // ページ間リンク: <> でかこまれてて / から始まり、 / が2個以上
  137. ;
  138. },
  139. expandImage: function (content) {
  140. return content.replace(/\s(https?:\/\/[\S]+\.(jpg|jpeg|gif|png))/g, ' <a href="$1"><img src="$1" class="auto-expanded-image"></a>');
  141. }
  142. };
  143. Crowi.renderer = function (contentText, revisionBody) {
  144. var $revisionBody = revisionBody || $('#revision-body-content');
  145. this.contentText = contentText;
  146. this.$revisionBody = $revisionBody;
  147. this.format = 'markdown'; // とりあえず
  148. this.renderer = Crowi.getRendererType();
  149. this.renderer.$revisionBody = this.$revisionBody;
  150. };
  151. Crowi.renderer.prototype = {
  152. render: function() {
  153. this.renderer.render(this.contentText);
  154. }
  155. };
  156. $(function() {
  157. Crowi.linkPath();
  158. $('[data-toggle="tooltip"]').tooltip();
  159. $('[data-tooltip-stay]').tooltip('show');
  160. $('.copy-link').on('click', function () {
  161. $(this).select();
  162. });
  163. $('#createMemo').on('shown.bs.modal', function (e) {
  164. $('#memoName').focus();
  165. });
  166. $('#createMemoForm').submit(function(e)
  167. {
  168. var prefix = $('[name=memoNamePrefix]', this).val();
  169. var name = $('[name=memoName]', this).val();
  170. if (name === '') {
  171. prefix = prefix.slice(0, -1);
  172. }
  173. top.location.href = prefix + name;
  174. return false;
  175. });
  176. $('#renamePage').on('shown.bs.modal', function (e) {
  177. $('#newPageName').focus();
  178. });
  179. $('#renamePageForm').submit(function(e) {
  180. var path = $('#pagePath').html();
  181. $.ajax({
  182. type: 'POST',
  183. url: '/_api/page_rename' + path,
  184. data: $('#renamePageForm').serialize(),
  185. dataType: 'json'
  186. }).done(function(data) {
  187. if (!data.status) {
  188. $('#newPageNameCheck').html('<i class="fa fa-times-circle"></i> ' + data.message);
  189. $('#newPageNameCheck').addClass('alert-danger');
  190. } else {
  191. $('#newPageNameCheck').removeClass('alert-danger');
  192. $('#newPageNameCheck').html('<img src="/images/loading_s.gif"> 移動しました。移動先にジャンプします。');
  193. setTimeout(function() {
  194. top.location.href = data.page.path + '?renamed=' + path;
  195. }, 1000);
  196. }
  197. });
  198. return false;
  199. });
  200. });