CrowiRenderer.js 3.9 KB

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