GrowiRenderer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * @param {any} config crowi config
  96. */
  97. setup(config) {
  98. this.md.set({
  99. breaks: config.isEnabledLineBreaks,
  100. });
  101. if (!this.isMarkdownItConfigured) {
  102. this.markdownItConfigurers.forEach((configurer) => {
  103. configurer.configure(this.md);
  104. });
  105. }
  106. }
  107. preProcess(markdown) {
  108. for (let i = 0; i < this.preProcessors.length; i++) {
  109. if (!this.preProcessors[i].process) {
  110. continue;
  111. }
  112. markdown = this.preProcessors[i].process(markdown);
  113. }
  114. return markdown;
  115. }
  116. process(markdown) {
  117. return this.md.render(markdown);
  118. }
  119. postProcess(html, dom) {
  120. for (let i = 0; i < this.postProcessors.length; i++) {
  121. if (!this.postProcessors[i].process) {
  122. continue;
  123. }
  124. html = this.postProcessors[i].process(html, dom);
  125. }
  126. return html;
  127. }
  128. codeRenderer(code, langExt) {
  129. const config = this.crowi.getConfig();
  130. const noborder = (!config.highlightJsStyleBorder) ? 'hljs-no-border' : '';
  131. if (langExt) {
  132. const langAndFn = langExt.split(':');
  133. const lang = langAndFn[0];
  134. const langFn = langAndFn[1] || null;
  135. const citeTag = (langFn) ? `<cite>${langFn}</cite>` : '';
  136. if (hljs.getLanguage(lang)) {
  137. try {
  138. return `<pre class="hljs ${noborder}">${citeTag}<code class="language-${lang}">${hljs.highlight(lang, code, true).value}</code></pre>`;
  139. }
  140. catch (__) {
  141. return `<pre class="hljs ${noborder}">${citeTag}<code class="language-${lang}">${code}}</code></pre>`;
  142. }
  143. }
  144. else {
  145. const escapedCode = this.xssFilterForCode.process(code);
  146. return `<pre class="hljs ${noborder}">${citeTag}<code>${escapedCode}</code></pre>`;
  147. }
  148. }
  149. const escapedCode = this.xssFilterForCode.process(code);
  150. return `<pre class="hljs ${noborder}"><code>${escapedCode}</code></pre>`;
  151. }
  152. }