CrowiRenderer.js 3.5 KB

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