GrowiRenderer.js 5.7 KB

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