CrowiRenderer.js 4.0 KB

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