2
0

GrowiRenderer.js 5.0 KB

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