GrowiRenderer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 EmojiConfigurer from './markdown-it/emoji';
  8. import MathJaxConfigurer from './markdown-it/mathjax';
  9. import PlantUMLConfigurer from './markdown-it/plantuml';
  10. import TableConfigurer from './markdown-it/table';
  11. export default class GrowiRenderer {
  12. constructor(crowi) {
  13. this.crowi = crowi;
  14. this.preProcessors = [
  15. new Linker(crowi),
  16. new CsvToTable(crowi),
  17. new XssFilter(crowi),
  18. ];
  19. this.postProcessors = [
  20. ];
  21. this.markdownItConfigurers = [
  22. new EmojiConfigurer(crowi),
  23. new MathJaxConfigurer(crowi),
  24. new PlantUMLConfigurer(crowi),
  25. new TableConfigurer(crowi),
  26. ];
  27. this.langProcessors = {
  28. 'template': new Template(crowi),
  29. };
  30. this.configure = this.configure.bind(this);
  31. this.configurePlugins = this.configurePlugins.bind(this);
  32. this.parseMarkdown = this.parseMarkdown.bind(this);
  33. this.codeRenderer = this.codeRenderer.bind(this);
  34. this.md = new MarkdownIt();
  35. this.configure(this.crowi.getConfig());
  36. this.configurePlugins(this.crowi.getConfig());
  37. }
  38. /**
  39. * configure markdown-it
  40. * @param {any} config
  41. */
  42. configure(config) {
  43. this.md.set({
  44. html: true,
  45. linkify: true,
  46. breaks: config.isEnabledLineBreaks,
  47. highlight: this.codeRenderer,
  48. });
  49. }
  50. /**
  51. * configure markdown-it plugins
  52. * @param {any} config
  53. */
  54. configurePlugins(config) {
  55. this.markdownItConfigurers.forEach((configurer) => {
  56. configurer.configure(this.md);
  57. });
  58. }
  59. preProcess(markdown) {
  60. for (let i = 0; i < this.preProcessors.length; i++) {
  61. if (!this.preProcessors[i].process) {
  62. continue;
  63. }
  64. markdown = this.preProcessors[i].process(markdown);
  65. }
  66. return markdown;
  67. }
  68. postProcess(html, dom) {
  69. for (let i = 0; i < this.postProcessors.length; i++) {
  70. if (!this.postProcessors[i].process) {
  71. continue;
  72. }
  73. html = this.postProcessors[i].process(html, dom);
  74. }
  75. return html;
  76. }
  77. codeRenderer(code, langExt) {
  78. if (langExt) {
  79. const langAndFn = langExt.split(':');
  80. const lang = langAndFn[0];
  81. const langFn = langAndFn[1] || null;
  82. // process langProcessors
  83. if (this.langProcessors[lang] != null) {
  84. return this.langProcessors[lang].process(code, langExt);
  85. }
  86. if (hljs.getLanguage(lang)) {
  87. let citeTag = (langFn) ? `<cite>${langFn}</cite>` : '';
  88. try {
  89. return `<pre class="hljs">${citeTag}<code class="language-${lang}">${hljs.highlight(lang, code, true).value}</code></pre>`;
  90. } catch (__) {}
  91. }
  92. }
  93. return '';
  94. }
  95. parseMarkdown(markdown, dom, markedOpts) {
  96. let parsed = '';
  97. parsed = this.md.render(markdown);
  98. return parsed;
  99. }
  100. /**
  101. * render
  102. *
  103. * @param {string} markdown
  104. * @param {Element} dom
  105. * @returns
  106. *
  107. * @memberOf CrowiRenderer
  108. */
  109. render(markdown, dom) {
  110. let html = '';
  111. html = this.parseMarkdown(markdown, dom);
  112. html = this.postProcess(html, dom);
  113. return html;
  114. }
  115. }