GrowiRenderer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import MarkdownIt from 'markdown-it';
  2. import Linker from './PreProcessor/Linker';
  3. import CsvToTable from './PreProcessor/CsvToTable';
  4. import XssFilter from './PreProcessor/XssFilter';
  5. import CrowiTemplate from './PostProcessor/CrowiTemplate';
  6. import EmojiConfigurer from './markdown-it/emoji';
  7. import FooternoteConfigurer from './markdown-it/footernote';
  8. import HeaderLineNumberConfigurer from './markdown-it/header-line-number';
  9. import HeaderConfigurer from './markdown-it/header';
  10. import MathJaxConfigurer from './markdown-it/mathjax';
  11. import PlantUMLConfigurer from './markdown-it/plantuml';
  12. import TableConfigurer from './markdown-it/table';
  13. import TaskListsConfigurer from './markdown-it/task-lists';
  14. import TocAndAnchorConfigurer from './markdown-it/toc-and-anchor';
  15. import BlockdiagConfigurer from './markdown-it/blockdiag';
  16. import TableWithHandsontableButtonConfigurer from './markdown-it/table-with-handsontable-button';
  17. import HeaderWithEditLinkConfigurer from './markdown-it/header-with-edit-link';
  18. const logger = require('@alias/logger')('growi:util:GrowiRenderer');
  19. export default class GrowiRenderer {
  20. /**
  21. *
  22. * @param {Crowi} crowi
  23. * @param {GrowiRenderer} originRenderer may be customized by plugins
  24. * @param {object} options
  25. */
  26. constructor(crowi, originRenderer, options) {
  27. this.crowi = crowi;
  28. this.originRenderer = originRenderer || {};
  29. this.options = Object.assign( // merge options
  30. { isAutoSetup: true }, // default options
  31. options || {}); // specified options
  32. // initialize processors
  33. // that will be retrieved if originRenderer exists
  34. this.preProcessors = this.originRenderer.preProcessors || [
  35. new Linker(crowi),
  36. new CsvToTable(crowi),
  37. new XssFilter(crowi),
  38. ];
  39. this.postProcessors = this.originRenderer.postProcessors || [
  40. new CrowiTemplate(crowi),
  41. ];
  42. this.initMarkdownItConfigurers = this.initMarkdownItConfigurers.bind(this);
  43. this.setup = this.setup.bind(this);
  44. this.process = this.process.bind(this);
  45. this.codeRenderer = this.codeRenderer.bind(this);
  46. // init markdown-it
  47. this.md = new MarkdownIt({
  48. html: true,
  49. linkify: true,
  50. highlight: this.codeRenderer,
  51. });
  52. this.initMarkdownItConfigurers(options);
  53. // auto setup
  54. if (this.options.isAutoSetup) {
  55. this.setup(crowi.getConfig());
  56. }
  57. }
  58. initMarkdownItConfigurers(options) {
  59. const crowi = this.crowi;
  60. this.isMarkdownItConfigured = false;
  61. this.markdownItConfigurers = [
  62. new TaskListsConfigurer(crowi),
  63. new HeaderConfigurer(crowi),
  64. new EmojiConfigurer(crowi),
  65. new MathJaxConfigurer(crowi),
  66. new PlantUMLConfigurer(crowi),
  67. new BlockdiagConfigurer(crowi),
  68. ];
  69. // add configurers according to mode
  70. const mode = options.mode;
  71. switch (mode) {
  72. case 'page':
  73. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  74. new FooternoteConfigurer(crowi),
  75. new TocAndAnchorConfigurer(crowi, options.renderToc),
  76. new HeaderLineNumberConfigurer(crowi),
  77. new HeaderWithEditLinkConfigurer(crowi),
  78. new TableWithHandsontableButtonConfigurer(crowi)
  79. ]);
  80. break;
  81. case 'editor':
  82. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  83. new FooternoteConfigurer(crowi),
  84. new HeaderLineNumberConfigurer(crowi),
  85. new TableConfigurer(crowi)
  86. ]);
  87. break;
  88. // case 'comment':
  89. // break;
  90. default:
  91. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  92. new TableConfigurer(crowi)
  93. ]);
  94. break;
  95. }
  96. }
  97. /**
  98. * setup with crowi config
  99. */
  100. setup() {
  101. const crowiConfig = this.crowi.config;
  102. let isEnabledLinebreaks = undefined;
  103. switch (this.options.mode) {
  104. case 'comment':
  105. isEnabledLinebreaks = crowiConfig.isEnabledLinebreaksInComments;
  106. break;
  107. default:
  108. isEnabledLinebreaks = crowiConfig.isEnabledLinebreaks;
  109. break;
  110. }
  111. this.md.set({
  112. breaks: isEnabledLinebreaks,
  113. });
  114. if (!this.isMarkdownItConfigured) {
  115. this.markdownItConfigurers.forEach((configurer) => {
  116. configurer.configure(this.md);
  117. });
  118. }
  119. }
  120. preProcess(markdown) {
  121. for (let i = 0; i < this.preProcessors.length; i++) {
  122. if (!this.preProcessors[i].process) {
  123. continue;
  124. }
  125. markdown = this.preProcessors[i].process(markdown);
  126. }
  127. return markdown;
  128. }
  129. process(markdown) {
  130. return this.md.render(markdown);
  131. }
  132. postProcess(html) {
  133. for (let i = 0; i < this.postProcessors.length; i++) {
  134. if (!this.postProcessors[i].process) {
  135. continue;
  136. }
  137. html = this.postProcessors[i].process(html);
  138. }
  139. return html;
  140. }
  141. codeRenderer(code, langExt) {
  142. const config = this.crowi.getConfig();
  143. const noborder = (!config.highlightJsStyleBorder) ? 'hljs-no-border' : '';
  144. let citeTag = '';
  145. let hljsLang = 'plaintext';
  146. let showLinenumbers = false;
  147. if (langExt) {
  148. // https://regex101.com/r/qGs7eZ/3
  149. const match = langExt.match(/^([^:=\n]+)?(=([^:=\n]*))?(:([^:=\n]*))?(=([^:=\n]*))?$/);
  150. const lang = match[1];
  151. const fileName = match[5] || null;
  152. showLinenumbers = (match[2] != null) || (match[6] != null);
  153. if (fileName != null) {
  154. citeTag = `<cite>${fileName}</cite>`;
  155. }
  156. if (hljs.getLanguage(lang)) {
  157. hljsLang = lang;
  158. }
  159. }
  160. let highlightCode = code;
  161. try {
  162. highlightCode = hljs.highlight(hljsLang, code, true).value;
  163. // add line numbers
  164. if (showLinenumbers) {
  165. highlightCode = hljs.lineNumbersValue((highlightCode));
  166. }
  167. }
  168. catch (err) {
  169. logger.error(err);
  170. }
  171. return `<pre class="hljs ${noborder}">${citeTag}<code>${highlightCode}</code></pre>`;
  172. }
  173. highlightCode(code, lang) {
  174. }
  175. }