GrowiRenderer.js 5.9 KB

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