GrowiRenderer.js 4.3 KB

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