GrowiRenderer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import MarkdownIt from 'markdown-it';
  2. import * as entities from 'entities';
  3. import Linker from './PreProcessor/Linker';
  4. import CsvToTable from './PreProcessor/CsvToTable';
  5. import XssFilter from './PreProcessor/XssFilter';
  6. import Template from './LangProcessor/Template';
  7. import CommonPluginsConfigurer from './markdown-it/common-plugins';
  8. import EmojiConfigurer from './markdown-it/emoji';
  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 TocAndAnchorConfigurer from './markdown-it/toc-and-anchor';
  15. export default class GrowiRenderer {
  16. /**
  17. *
  18. * @param {Crowi} crowi
  19. * @param {GrowiRenderer} originRenderer may be customized by plugins
  20. * @param {object} options
  21. */
  22. constructor(crowi, originRenderer, options) {
  23. this.crowi = crowi;
  24. this.originRenderer = originRenderer || {};
  25. this.options = Object.assign( // merge options
  26. { isAutoSetup: true }, // default options
  27. options || {}); // specified options
  28. // initialize processors
  29. // that will be retrieved if originRenderer exists
  30. this.preProcessors = this.originRenderer.preProcessors || [
  31. new Linker(crowi),
  32. new CsvToTable(crowi),
  33. new XssFilter(crowi),
  34. ];
  35. this.postProcessors = this.originRenderer.postProcessors || [
  36. ];
  37. this.langProcessors = this.originRenderer.langProcessors || {
  38. 'template': new Template(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 CommonPluginsConfigurer(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 TocAndAnchorConfigurer(crowi, options.renderToc),
  73. new HeaderLineNumberConfigurer(crowi),
  74. ]);
  75. break;
  76. case 'editor':
  77. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  78. new HeaderLineNumberConfigurer(crowi)
  79. ]);
  80. break;
  81. case 'comment':
  82. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  83. new HeaderLineNumberConfigurer(crowi),
  84. ]);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. /**
  91. * setup with crowi config
  92. * @param {any} config crowi config
  93. */
  94. setup(config) {
  95. this.md.set({
  96. breaks: config.isEnabledLineBreaks,
  97. });
  98. if (!this.isMarkdownItConfigured) {
  99. this.markdownItConfigurers.forEach((configurer) => {
  100. configurer.configure(this.md);
  101. });
  102. }
  103. }
  104. preProcess(markdown) {
  105. for (let i = 0; i < this.preProcessors.length; i++) {
  106. if (!this.preProcessors[i].process) {
  107. continue;
  108. }
  109. markdown = this.preProcessors[i].process(markdown);
  110. }
  111. return markdown;
  112. }
  113. process(markdown) {
  114. return this.md.render(markdown);
  115. }
  116. postProcess(html, dom) {
  117. for (let i = 0; i < this.postProcessors.length; i++) {
  118. if (!this.postProcessors[i].process) {
  119. continue;
  120. }
  121. html = this.postProcessors[i].process(html, dom);
  122. }
  123. return html;
  124. }
  125. codeRenderer(code, langExt) {
  126. if (langExt) {
  127. const langAndFn = langExt.split(':');
  128. const lang = langAndFn[0];
  129. const langFn = langAndFn[1] || null;
  130. // process langProcessors
  131. if (this.langProcessors[lang] != null) {
  132. return this.langProcessors[lang].process(code, langExt);
  133. }
  134. const citeTag = (langFn) ? `<cite>${langFn}</cite>` : '';
  135. if (hljs.getLanguage(lang)) {
  136. try {
  137. return `<pre class="hljs">${citeTag}<code class="language-${lang}">${hljs.highlight(lang, code, true).value}</code></pre>`;
  138. } catch (__) {}
  139. }
  140. else {
  141. return `<pre class="hljs">${citeTag}<code>${code}</code></pre>`;
  142. }
  143. }
  144. return `<pre class="hljs"><code>${code}</code></pre>`;
  145. }
  146. }