GrowiRenderer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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(appContainer),
  38. new Linker(appContainer),
  39. new CsvToTable(appContainer),
  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(appContainer),
  63. new EmojiConfigurer(appContainer),
  64. new MathJaxConfigurer(appContainer),
  65. new DrawioViewerConfigurer(appContainer),
  66. new PlantUMLConfigurer(appContainer),
  67. new BlockdiagConfigurer(appContainer),
  68. ];
  69. // add configurers according to mode
  70. switch (mode) {
  71. case 'page': {
  72. const pageContainer = appContainer.getContainer('PageContainer');
  73. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  74. new FooternoteConfigurer(appContainer),
  75. new TocAndAnchorConfigurer(appContainer, pageContainer.setTocHtml),
  76. new HeaderLineNumberConfigurer(appContainer),
  77. new HeaderWithEditLinkConfigurer(appContainer),
  78. new TableWithHandsontableButtonConfigurer(appContainer),
  79. ]);
  80. break;
  81. }
  82. case 'editor':
  83. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  84. new FooternoteConfigurer(appContainer),
  85. new HeaderLineNumberConfigurer(appContainer),
  86. new TableConfigurer(appContainer),
  87. ]);
  88. break;
  89. // case 'comment':
  90. // break;
  91. default:
  92. this.markdownItConfigurers = this.markdownItConfigurers.concat([
  93. new TableConfigurer(appContainer),
  94. ]);
  95. break;
  96. }
  97. }
  98. /**
  99. * setup with crowi config
  100. */
  101. setup(mode) {
  102. const crowiConfig = this.appContainer.config;
  103. let isEnabledLinebreaks;
  104. switch (mode) {
  105. case 'comment':
  106. isEnabledLinebreaks = crowiConfig.isEnabledLinebreaksInComments;
  107. break;
  108. default:
  109. isEnabledLinebreaks = crowiConfig.isEnabledLinebreaks;
  110. break;
  111. }
  112. this.md.set({
  113. breaks: isEnabledLinebreaks,
  114. });
  115. if (!this.isMarkdownItConfigured) {
  116. this.markdownItConfigurers.forEach((configurer) => {
  117. configurer.configure(this.md);
  118. });
  119. }
  120. }
  121. preProcess(markdown, context) {
  122. let processed = markdown;
  123. for (let i = 0; i < this.preProcessors.length; i++) {
  124. if (!this.preProcessors[i].process) {
  125. continue;
  126. }
  127. processed = this.preProcessors[i].process(processed, context);
  128. }
  129. return processed;
  130. }
  131. process(markdown, context) {
  132. return this.md.render(markdown, context);
  133. }
  134. postProcess(html, context) {
  135. let processed = html;
  136. for (let i = 0; i < this.postProcessors.length; i++) {
  137. if (!this.postProcessors[i].process) {
  138. continue;
  139. }
  140. processed = this.postProcessors[i].process(processed, context);
  141. }
  142. return processed;
  143. }
  144. codeRenderer(code, langExt) {
  145. const config = this.appContainer.getConfig();
  146. const noborder = (!config.highlightJsStyleBorder) ? 'hljs-no-border' : '';
  147. let citeTag = '';
  148. let hljsLang = 'plaintext';
  149. let showLinenumbers = false;
  150. if (langExt) {
  151. // https://regex101.com/r/qGs7eZ/3
  152. const match = langExt.match(/^([^:=\n]+)?(=([^:=\n]*))?(:([^:=\n]*))?(=([^:=\n]*))?$/);
  153. const lang = match[1];
  154. const fileName = match[5] || null;
  155. showLinenumbers = (match[2] != null) || (match[6] != null);
  156. if (fileName != null) {
  157. citeTag = `<cite>${fileName}</cite>`;
  158. }
  159. if (hljs.getLanguage(lang)) {
  160. hljsLang = lang;
  161. }
  162. }
  163. let highlightCode = code;
  164. try {
  165. highlightCode = hljs.highlight(hljsLang, code, true).value;
  166. // add line numbers
  167. if (showLinenumbers) {
  168. highlightCode = hljs.lineNumbersValue((highlightCode));
  169. }
  170. }
  171. catch (err) {
  172. logger.error(err);
  173. }
  174. return `<pre class="hljs ${noborder}">${citeTag}<code>${highlightCode}</code></pre>`;
  175. }
  176. highlightCode(code, lang) {
  177. }
  178. }