GrowiRenderer.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import MarkdownIt from 'markdown-it';
  2. // import hljs from 'highlight.js';
  3. import * as entities from 'entities';
  4. import MarkdownFixer from './PreProcessor/MarkdownFixer';
  5. import Linker from './PreProcessor/Linker';
  6. import ImageExpander from './PreProcessor/ImageExpander';
  7. import XssFilter from './PreProcessor/XssFilter';
  8. import Emoji from './PostProcessor/Emoji';
  9. import Mathjax from './PostProcessor/Mathjax';
  10. import Tsv2Table from './LangProcessor/Tsv2Table';
  11. import Template from './LangProcessor/Template';
  12. import PlantUML from './LangProcessor/PlantUML';
  13. export default class GrowiRenderer {
  14. constructor(crowi) {
  15. this.crowi = crowi;
  16. this.md = new MarkdownIt();
  17. this.configure(this.crowi.getConfig());
  18. this.preProcessors = [
  19. new Linker(crowi),
  20. new ImageExpander(crowi),
  21. new XssFilter(crowi),
  22. ];
  23. this.postProcessors = [
  24. new Emoji(crowi),
  25. new Mathjax(crowi),
  26. ];
  27. this.langProcessors = {
  28. 'tsv': new Tsv2Table(crowi),
  29. 'tsv-h': new Tsv2Table(crowi, {header: true}),
  30. 'template': new Template(crowi),
  31. 'plantuml': new PlantUML(crowi),
  32. };
  33. this.configure = this.configure.bind(this);
  34. this.parseMarkdown = this.parseMarkdown.bind(this);
  35. this.codeRenderer = this.codeRenderer.bind(this);
  36. }
  37. /**
  38. * configure markdown-it
  39. * @param {any} config
  40. */
  41. configure(config) {
  42. this.md.set({
  43. html: true,
  44. linkify: true,
  45. breaks: config.isEnabledLineBreaks,
  46. });
  47. }
  48. preProcess(markdown, dom) {
  49. for (let i = 0; i < this.preProcessors.length; i++) {
  50. if (!this.preProcessors[i].process) {
  51. continue;
  52. }
  53. markdown = this.preProcessors[i].process(markdown, dom);
  54. }
  55. return markdown;
  56. }
  57. postProcess(html, dom) {
  58. for (let i = 0; i < this.postProcessors.length; i++) {
  59. if (!this.postProcessors[i].process) {
  60. continue;
  61. }
  62. html = this.postProcessors[i].process(html, dom);
  63. }
  64. return html;
  65. }
  66. codeRenderer(code, lang, escaped) {
  67. let result = '', hl;
  68. // if (lang) {
  69. // const langAndFn = lang.split(':');
  70. // const langPattern = langAndFn[0];
  71. // const langFn = langAndFn[1] || null;
  72. // if (this.langProcessors[langPattern]) {
  73. // return this.langProcessors[langPattern].process(code, lang);
  74. // }
  75. // try {
  76. // hl = hljs.highlight(langPattern, code);
  77. // result = hl.value;
  78. // escaped = true;
  79. // } catch (e) {
  80. // result = code;
  81. // }
  82. // result = (escape ? result : entities.encodeHTML(result));
  83. // let citeTag = '';
  84. // if (langFn) {
  85. // citeTag = `<cite>${langFn}</cite>`;
  86. // }
  87. // return `<pre class="wiki-code wiki-lang">${citeTag}<code class="lang-${lang}">${result}\n</code></pre>\n`;
  88. // }
  89. // no lang specified
  90. return `<pre class="wiki-code"><code>${entities.encodeHTML(code)}\n</code></pre>`;
  91. }
  92. parseMarkdown(markdown, dom, markedOpts) {
  93. let parsed = '';
  94. /*
  95. const markedRenderer = new marked.Renderer();
  96. markedRenderer.code = this.codeRenderer;
  97. try {
  98. // concat
  99. let concatMarkedOpts = Object.assign({
  100. gfm: true,
  101. tables: true,
  102. breaks: true,
  103. pedantic: false,
  104. sanitize: false,
  105. smartLists: true,
  106. smartypants: false,
  107. renderer: markedRenderer,
  108. }, markedOpts);
  109. marked.setOptions(concatMarkedOpts);
  110. // override
  111. marked.Lexer.lex = function(src, options) {
  112. var lexer = new marked.Lexer(options);
  113. // this is maybe not an official way
  114. if (lexer.rules) {
  115. lexer.rules.fences = /^ *(`{3,}|~{3,})[ \.]*([^\r\n]+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/;
  116. }
  117. return lexer.lex(src);
  118. };
  119. parsed = marked(markdown);
  120. } catch (e) { console.log(e, e.stack); }
  121. */
  122. parsed = this.md.render(markdown);
  123. return parsed;
  124. }
  125. /**
  126. * render
  127. *
  128. * @param {string} markdown
  129. * @param {Element} dom
  130. * @returns
  131. *
  132. * @memberOf CrowiRenderer
  133. */
  134. render(markdown, dom) {
  135. let html = '';
  136. html = this.parseMarkdown(markdown, dom);
  137. html = this.postProcess(html, dom);
  138. return html;
  139. }
  140. }