GrowiRenderer.js 5.8 KB

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