GrowiRenderer.js 5.5 KB

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