GrowiRenderer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import MarkdownIt from 'markdown-it';
  2. import xss from 'xss';
  3. import Linker from './PreProcessor/Linker';
  4. import CsvToTable from './PreProcessor/CsvToTable';
  5. import XssFilter from './PreProcessor/XssFilter';
  6. import CrowiTemplate from './PostProcessor/CrowiTemplate';
  7. import EmojiConfigurer from './markdown-it/emoji';
  8. import FooternoteConfigurer from './markdown-it/footernote';
  9. import HeaderLineNumberConfigurer from './markdown-it/header-line-number';
  10. import HeaderConfigurer from './markdown-it/header';
  11. import MathJaxConfigurer from './markdown-it/mathjax';
  12. import PlantUMLConfigurer from './markdown-it/plantuml';
  13. import TableConfigurer from './markdown-it/table';
  14. import TaskListsConfigurer from './markdown-it/task-lists';
  15. import TocAndAnchorConfigurer from './markdown-it/toc-and-anchor';
  16. import BlockdiagConfigurer from './markdown-it/blockdiag';
  17. import TableWithHandsontableButtonConfigurer from './markdown-it/table-with-handsontable-button';
  18. import HeaderWithEditLinkConfigurer from './markdown-it/header-with-edit-link';
  19. export default class GrowiRenderer {
  20. /**
  21. *
  22. * @param {Crowi} crowi
  23. * @param {GrowiRenderer} originRenderer may be customized by plugins
  24. * @param {object} options
  25. */
  26. constructor(crowi, originRenderer, options) {
  27. this.crowi = crowi;
  28. this.originRenderer = originRenderer || {};
  29. this.options = Object.assign( // merge options
  30. { isAutoSetup: true }, // default options
  31. options || {}); // specified options
  32. this.xssFilterForCode = new xss.FilterXSS();
  33. // initialize processors
  34. // that will be retrieved if originRenderer exists
  35. this.preProcessors = this.originRenderer.preProcessors || [
  36. new Linker(crowi),
  37. new CsvToTable(crowi),
  38. new XssFilter(crowi),
  39. ];
  40. this.postProcessors = this.originRenderer.postProcessors || [
  41. new CrowiTemplate(crowi),
  42. ];
  43. this.initMarkdownItConfigurers = this.initMarkdownItConfigurers.bind(this);
  44. this.setup = this.setup.bind(this);
  45. this.process = this.process.bind(this);
  46. this.codeRenderer = this.codeRenderer.bind(this);
  47. // init markdown-it
  48. this.md = new MarkdownIt({
  49. html: true,
  50. linkify: true,
  51. highlight: this.codeRenderer,
  52. });
  53. this.initMarkdownItConfigurers(options);
  54. // auto setup
  55. if (this.options.isAutoSetup) {
  56. this.setup(crowi.getConfig());
  57. }
  58. }
  59. initMarkdownItConfigurers(options) {
  60. const crowi = this.crowi;
  61. this.isMarkdownItConfigured = false;
  62. this.markdownItConfigurers = [
  63. new TaskListsConfigurer(crowi),
  64. new HeaderConfigurer(crowi),
  65. new EmojiConfigurer(crowi),
  66. new MathJaxConfigurer(crowi),
  67. new PlantUMLConfigurer(crowi),
  68. new BlockdiagConfigurer(crowi),
  69. ];
  70. // add configurers according to mode
  71. const mode = options.mode;
  72. switch (mode) {
  73. case 'page':
  74. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  75. new FooternoteConfigurer(crowi),
  76. new TocAndAnchorConfigurer(crowi, options.renderToc),
  77. new HeaderLineNumberConfigurer(crowi),
  78. new HeaderWithEditLinkConfigurer(crowi),
  79. new TableWithHandsontableButtonConfigurer(crowi)
  80. ]);
  81. break;
  82. case 'editor':
  83. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  84. new FooternoteConfigurer(crowi),
  85. new HeaderLineNumberConfigurer(crowi),
  86. new TableConfigurer(crowi)
  87. ]);
  88. break;
  89. // case 'comment':
  90. // break;
  91. default:
  92. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  93. new TableConfigurer(crowi)
  94. ]);
  95. break;
  96. }
  97. }
  98. /**
  99. * setup with crowi config
  100. */
  101. setup() {
  102. const crowiConfig = this.crowi.config;
  103. let isEnabledLinebreaks = undefined;
  104. switch (this.options.mode) {
  105. case 'comment':
  106. isEnabledLinebreaks = crowiConfig.isEnabledLinebreaksInComments;
  107. break;
  108. default:
  109. isEnabledLinebreaks = crowiConfig.isEnabledLinebreaks;
  110. break;
  111. }
  112. this.md.set({
  113. breaks: isEnabledLinebreaks,
  114. });
  115. if (!this.isMarkdownItConfigured) {
  116. this.markdownItConfigurers.forEach((configurer) => {
  117. configurer.configure(this.md);
  118. });
  119. }
  120. }
  121. preProcess(markdown) {
  122. for (let i = 0; i < this.preProcessors.length; i++) {
  123. if (!this.preProcessors[i].process) {
  124. continue;
  125. }
  126. markdown = this.preProcessors[i].process(markdown);
  127. }
  128. return markdown;
  129. }
  130. process(markdown) {
  131. return this.md.render(markdown);
  132. }
  133. postProcess(html) {
  134. for (let i = 0; i < this.postProcessors.length; i++) {
  135. if (!this.postProcessors[i].process) {
  136. continue;
  137. }
  138. html = this.postProcessors[i].process(html);
  139. }
  140. return html;
  141. }
  142. codeRenderer(code, langExt) {
  143. const config = this.crowi.getConfig();
  144. const noborder = (!config.highlightJsStyleBorder) ? 'hljs-no-border' : '';
  145. if (langExt) {
  146. const langAndFn = langExt.split(':');
  147. const lang = langAndFn[0];
  148. const langFn = langAndFn[1] || null;
  149. const citeTag = (langFn) ? `<cite>${langFn}</cite>` : '';
  150. if (hljs.getLanguage(lang)) {
  151. try {
  152. return `<pre class="hljs ${noborder}">${citeTag}<code class="language-${lang}">${hljs.highlight(lang, code, true).value}</code></pre>`;
  153. }
  154. catch (__) {
  155. return `<pre class="hljs ${noborder}">${citeTag}<code class="language-${lang}">${code}}</code></pre>`;
  156. }
  157. }
  158. else {
  159. const escapedCode = this.xssFilterForCode.process(code);
  160. return `<pre class="hljs ${noborder}">${citeTag}<code>${escapedCode}</code></pre>`;
  161. }
  162. }
  163. const escapedCode = this.xssFilterForCode.process(code);
  164. return `<pre class="hljs ${noborder}"><code>${escapedCode}</code></pre>`;
  165. }
  166. }