GrowiRenderer.js 4.5 KB

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