GrowiRenderer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 'timeline':
  82. break;
  83. case 'searchresult':
  84. break;
  85. }
  86. }
  87. /**
  88. * setup with crowi config
  89. * @param {any} config crowi config
  90. */
  91. setup(config) {
  92. this.md.set({
  93. breaks: config.isEnabledLineBreaks,
  94. });
  95. if (!this.isMarkdownItConfigured) {
  96. this.markdownItConfigurers.forEach((configurer) => {
  97. configurer.configure(this.md);
  98. });
  99. }
  100. }
  101. preProcess(markdown) {
  102. for (let i = 0; i < this.preProcessors.length; i++) {
  103. if (!this.preProcessors[i].process) {
  104. continue;
  105. }
  106. markdown = this.preProcessors[i].process(markdown);
  107. }
  108. return markdown;
  109. }
  110. process(markdown) {
  111. return this.md.render(markdown);
  112. }
  113. postProcess(html, dom) {
  114. for (let i = 0; i < this.postProcessors.length; i++) {
  115. if (!this.postProcessors[i].process) {
  116. continue;
  117. }
  118. html = this.postProcessors[i].process(html, dom);
  119. }
  120. return html;
  121. }
  122. codeRenderer(code, langExt) {
  123. if (langExt) {
  124. const langAndFn = langExt.split(':');
  125. const lang = langAndFn[0];
  126. const langFn = langAndFn[1] || null;
  127. // process langProcessors
  128. if (this.langProcessors[lang] != null) {
  129. return this.langProcessors[lang].process(code, langExt);
  130. }
  131. const citeTag = (langFn) ? `<cite>${langFn}</cite>` : '';
  132. if (hljs.getLanguage(lang)) {
  133. try {
  134. return `<pre class="hljs">${citeTag}<code class="language-${lang}">${hljs.highlight(lang, code, true).value}</code></pre>`;
  135. } catch (__) {}
  136. }
  137. else {
  138. return `<pre class="hljs">${citeTag}<code>${code}</code></pre>`;
  139. }
  140. }
  141. return '';
  142. }
  143. }