CrowiRenderer.js 3.3 KB

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