GrowiRenderer.js 5.5 KB

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