GrowiRenderer.js 4.6 KB

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