CrowiRenderer.js 3.8 KB

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