GrowiRenderer.js 5.1 KB

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