GrowiRenderer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 'markdown-it-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.configurePlugins(this.crowi.getConfig());
  19. this.preProcessors = [
  20. new Linker(crowi),
  21. new ImageExpander(crowi),
  22. new XssFilter(crowi),
  23. ];
  24. this.postProcessors = [
  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. /**
  49. * configure markdown-it plugins
  50. * @param {any} config
  51. */
  52. configurePlugins(config) {
  53. this.md
  54. .use(emoji);
  55. // integrate markdown-it-emoji and emojione
  56. this.md.renderer.rules.emoji = (token, idx) => {
  57. const shortname = `:${token[idx].markup}:`;
  58. return emojione.shortnameToImage(shortname);
  59. };
  60. }
  61. preProcess(markdown, dom) {
  62. for (let i = 0; i < this.preProcessors.length; i++) {
  63. if (!this.preProcessors[i].process) {
  64. continue;
  65. }
  66. markdown = this.preProcessors[i].process(markdown, dom);
  67. }
  68. return markdown;
  69. }
  70. postProcess(html, dom) {
  71. for (let i = 0; i < this.postProcessors.length; i++) {
  72. if (!this.postProcessors[i].process) {
  73. continue;
  74. }
  75. html = this.postProcessors[i].process(html, dom);
  76. }
  77. return html;
  78. }
  79. codeRenderer(code, lang, escaped) {
  80. let result = '', hl;
  81. // if (lang) {
  82. // const langAndFn = lang.split(':');
  83. // const langPattern = langAndFn[0];
  84. // const langFn = langAndFn[1] || null;
  85. // if (this.langProcessors[langPattern]) {
  86. // return this.langProcessors[langPattern].process(code, lang);
  87. // }
  88. // try {
  89. // hl = hljs.highlight(langPattern, code);
  90. // result = hl.value;
  91. // escaped = true;
  92. // } catch (e) {
  93. // result = code;
  94. // }
  95. // result = (escape ? result : entities.encodeHTML(result));
  96. // let citeTag = '';
  97. // if (langFn) {
  98. // citeTag = `<cite>${langFn}</cite>`;
  99. // }
  100. // return `<pre class="wiki-code wiki-lang">${citeTag}<code class="lang-${lang}">${result}\n</code></pre>\n`;
  101. // }
  102. // no lang specified
  103. return `<pre class="wiki-code"><code>${entities.encodeHTML(code)}\n</code></pre>`;
  104. }
  105. parseMarkdown(markdown, dom, markedOpts) {
  106. let parsed = '';
  107. /*
  108. const markedRenderer = new marked.Renderer();
  109. markedRenderer.code = this.codeRenderer;
  110. try {
  111. // concat
  112. let concatMarkedOpts = Object.assign({
  113. gfm: true,
  114. tables: true,
  115. breaks: true,
  116. pedantic: false,
  117. sanitize: false,
  118. smartLists: true,
  119. smartypants: false,
  120. renderer: markedRenderer,
  121. }, markedOpts);
  122. marked.setOptions(concatMarkedOpts);
  123. // override
  124. marked.Lexer.lex = function(src, options) {
  125. var lexer = new marked.Lexer(options);
  126. // this is maybe not an official way
  127. if (lexer.rules) {
  128. lexer.rules.fences = /^ *(`{3,}|~{3,})[ \.]*([^\r\n]+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/;
  129. }
  130. return lexer.lex(src);
  131. };
  132. parsed = marked(markdown);
  133. } catch (e) { console.log(e, e.stack); }
  134. */
  135. parsed = this.md.render(markdown);
  136. return parsed;
  137. }
  138. /**
  139. * render
  140. *
  141. * @param {string} markdown
  142. * @param {Element} dom
  143. * @returns
  144. *
  145. * @memberOf CrowiRenderer
  146. */
  147. render(markdown, dom) {
  148. let html = '';
  149. html = this.parseMarkdown(markdown, dom);
  150. html = this.postProcess(html, dom);
  151. return html;
  152. }
  153. }