GrowiRenderer.js 4.8 KB

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