CrowiRenderer.js 3.8 KB

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