renderer.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import assert from 'assert';
  2. import { isClient } from '@growi/core/dist/utils/browser-utils';
  3. import * as slides from '@growi/presentation';
  4. import * as refsGrowiDirective from '@growi/remark-attachment-refs/dist/client';
  5. import * as drawio from '@growi/remark-drawio';
  6. // eslint-disable-next-line import/extensions
  7. import * as lsxGrowiDirective from '@growi/remark-lsx/dist/client';
  8. import katex from 'rehype-katex';
  9. import sanitize from 'rehype-sanitize';
  10. import slug from 'rehype-slug';
  11. import type { HtmlElementNode } from 'rehype-toc';
  12. import breaks from 'remark-breaks';
  13. import math from 'remark-math';
  14. import deepmerge from 'ts-deepmerge';
  15. import type { Pluggable } from 'unified';
  16. import { DrawioViewerWithEditButton } from '~/components/ReactMarkdownComponents/DrawioViewerWithEditButton';
  17. import { Header } from '~/components/ReactMarkdownComponents/Header';
  18. import { LightBox } from '~/components/ReactMarkdownComponents/LightBox';
  19. import { RichAttachment } from '~/components/ReactMarkdownComponents/RichAttachment';
  20. // eslint-disable-next-line import/no-cycle
  21. import { SlideViewer } from '~/components/ReactMarkdownComponents/SlideViewer';
  22. import { TableWithEditButton } from '~/components/ReactMarkdownComponents/TableWithEditButton';
  23. import * as mermaid from '~/features/mermaid';
  24. import { RehypeSanitizeOption } from '~/interfaces/rehype';
  25. import type { RendererOptions } from '~/interfaces/renderer-options';
  26. import type { RendererConfig } from '~/interfaces/services/renderer';
  27. import * as addLineNumberAttribute from '~/services/renderer/rehype-plugins/add-line-number-attribute';
  28. import * as keywordHighlighter from '~/services/renderer/rehype-plugins/keyword-highlighter';
  29. import * as relocateToc from '~/services/renderer/rehype-plugins/relocate-toc';
  30. import * as attachment from '~/services/renderer/remark-plugins/attachment';
  31. import * as plantuml from '~/services/renderer/remark-plugins/plantuml';
  32. import * as xsvToTable from '~/services/renderer/remark-plugins/xsv-to-table';
  33. import {
  34. commonSanitizeOption, generateCommonOptions, injectCustomSanitizeOption, verifySanitizePlugin,
  35. } from '~/services/renderer/renderer';
  36. import loggerFactory from '~/utils/logger';
  37. // import EasyGrid from './PreProcessor/EasyGrid';
  38. import '@growi/remark-lsx/dist/client/style.css';
  39. import '@growi/remark-attachment-refs/dist/client/style.css';
  40. const logger = loggerFactory('growi:cli:services:renderer');
  41. assert(isClient(), 'This module must be loaded only from client modules.');
  42. export const generateViewOptions = (
  43. pagePath: string,
  44. config: RendererConfig,
  45. storeTocNode: (toc: HtmlElementNode) => void,
  46. ): RendererOptions => {
  47. const options = generateCommonOptions(pagePath);
  48. const { remarkPlugins, rehypePlugins, components } = options;
  49. // add remark plugins
  50. remarkPlugins.push(
  51. math,
  52. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  53. drawio.remarkPlugin,
  54. mermaid.remarkPlugin,
  55. xsvToTable.remarkPlugin,
  56. attachment.remarkPlugin,
  57. lsxGrowiDirective.remarkPlugin,
  58. refsGrowiDirective.remarkPlugin,
  59. [slides.remarkPlugin, { isEnabledMarp: config.isEnabledMarp }],
  60. );
  61. if (config.isEnabledLinebreaks) {
  62. remarkPlugins.push(breaks);
  63. }
  64. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  65. injectCustomSanitizeOption(config);
  66. }
  67. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  68. ? [sanitize, deepmerge(
  69. commonSanitizeOption,
  70. drawio.sanitizeOption,
  71. mermaid.sanitizeOption,
  72. attachment.sanitizeOption,
  73. slides.sanitizeOption,
  74. lsxGrowiDirective.sanitizeOption,
  75. refsGrowiDirective.sanitizeOption,
  76. )]
  77. : () => {};
  78. // add rehype plugins
  79. rehypePlugins.push(
  80. slug,
  81. [lsxGrowiDirective.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  82. [refsGrowiDirective.rehypePlugin, { pagePath }],
  83. rehypeSanitizePlugin,
  84. katex,
  85. [relocateToc.rehypePluginStore, { storeTocNode }],
  86. );
  87. // add components
  88. if (components != null) {
  89. components.h1 = Header;
  90. components.h2 = Header;
  91. components.h3 = Header;
  92. components.h4 = Header;
  93. components.h5 = Header;
  94. components.h6 = Header;
  95. components.lsx = lsxGrowiDirective.Lsx;
  96. components.ref = refsGrowiDirective.Ref;
  97. components.refs = refsGrowiDirective.Refs;
  98. components.refimg = refsGrowiDirective.RefImg;
  99. components.refsimg = refsGrowiDirective.RefsImg;
  100. components.gallery = refsGrowiDirective.Gallery;
  101. components.drawio = DrawioViewerWithEditButton;
  102. components.table = TableWithEditButton;
  103. components.mermaid = mermaid.MermaidViewer;
  104. components.attachment = RichAttachment;
  105. components.img = LightBox;
  106. components.slide = SlideViewer;
  107. }
  108. if (config.isEnabledXssPrevention) {
  109. verifySanitizePlugin(options, false);
  110. }
  111. return options;
  112. };
  113. export const generateTocOptions = (config: RendererConfig, tocNode: HtmlElementNode | undefined): RendererOptions => {
  114. const options = generateCommonOptions(undefined);
  115. const { rehypePlugins } = options;
  116. // add remark plugins
  117. // remarkPlugins.push();
  118. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  119. injectCustomSanitizeOption(config);
  120. }
  121. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  122. ? [sanitize, deepmerge(
  123. commonSanitizeOption,
  124. )]
  125. : () => {};
  126. // add rehype plugins
  127. rehypePlugins.push(
  128. [relocateToc.rehypePluginRestore, { tocNode }],
  129. rehypeSanitizePlugin,
  130. );
  131. if (config.isEnabledXssPrevention) {
  132. verifySanitizePlugin(options);
  133. }
  134. return options;
  135. };
  136. export const generateSimpleViewOptions = (
  137. config: RendererConfig,
  138. pagePath: string,
  139. highlightKeywords?: string | string[],
  140. overrideIsEnabledLinebreaks?: boolean,
  141. ): RendererOptions => {
  142. const options = generateCommonOptions(pagePath);
  143. const { remarkPlugins, rehypePlugins, components } = options;
  144. // add remark plugins
  145. remarkPlugins.push(
  146. math,
  147. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  148. drawio.remarkPlugin,
  149. mermaid.remarkPlugin,
  150. xsvToTable.remarkPlugin,
  151. attachment.remarkPlugin,
  152. lsxGrowiDirective.remarkPlugin,
  153. refsGrowiDirective.remarkPlugin,
  154. );
  155. const isEnabledLinebreaks = overrideIsEnabledLinebreaks ?? config.isEnabledLinebreaks;
  156. if (isEnabledLinebreaks) {
  157. remarkPlugins.push(breaks);
  158. }
  159. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  160. injectCustomSanitizeOption(config);
  161. }
  162. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  163. ? [sanitize, deepmerge(
  164. commonSanitizeOption,
  165. drawio.sanitizeOption,
  166. mermaid.sanitizeOption,
  167. attachment.sanitizeOption,
  168. lsxGrowiDirective.sanitizeOption,
  169. refsGrowiDirective.sanitizeOption,
  170. )]
  171. : () => {};
  172. // add rehype plugins
  173. rehypePlugins.push(
  174. [lsxGrowiDirective.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  175. [refsGrowiDirective.rehypePlugin, { pagePath }],
  176. [keywordHighlighter.rehypePlugin, { keywords: highlightKeywords }],
  177. rehypeSanitizePlugin,
  178. katex,
  179. );
  180. // add components
  181. if (components != null) {
  182. components.lsx = lsxGrowiDirective.LsxImmutable;
  183. components.ref = refsGrowiDirective.RefImmutable;
  184. components.refs = refsGrowiDirective.RefsImmutable;
  185. components.refimg = refsGrowiDirective.RefImgImmutable;
  186. components.refsimg = refsGrowiDirective.RefsImgImmutable;
  187. components.gallery = refsGrowiDirective.GalleryImmutable;
  188. components.drawio = drawio.DrawioViewer;
  189. components.mermaid = mermaid.MermaidViewer;
  190. components.attachment = RichAttachment;
  191. components.img = LightBox;
  192. }
  193. if (config.isEnabledXssPrevention) {
  194. verifySanitizePlugin(options, false);
  195. }
  196. return options;
  197. };
  198. export const generatePresentationViewOptions = (
  199. config: RendererConfig,
  200. pagePath: string,
  201. ): RendererOptions => {
  202. // based on simple view options
  203. const options = generateSimpleViewOptions(config, pagePath);
  204. if (config.isEnabledXssPrevention) {
  205. verifySanitizePlugin(options, false);
  206. }
  207. return options;
  208. };
  209. export const generatePreviewOptions = (config: RendererConfig, pagePath: string): RendererOptions => {
  210. const options = generateCommonOptions(pagePath);
  211. const { remarkPlugins, rehypePlugins, components } = options;
  212. // add remark plugins
  213. remarkPlugins.push(
  214. math,
  215. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  216. drawio.remarkPlugin,
  217. mermaid.remarkPlugin,
  218. xsvToTable.remarkPlugin,
  219. attachment.remarkPlugin,
  220. lsxGrowiDirective.remarkPlugin,
  221. refsGrowiDirective.remarkPlugin,
  222. [slides.remarkPlugin, { isEnabledMarp: config.isEnabledMarp }],
  223. );
  224. if (config.isEnabledLinebreaks) {
  225. remarkPlugins.push(breaks);
  226. }
  227. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  228. injectCustomSanitizeOption(config);
  229. }
  230. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  231. ? [sanitize, deepmerge(
  232. commonSanitizeOption,
  233. drawio.sanitizeOption,
  234. mermaid.sanitizeOption,
  235. attachment.sanitizeOption,
  236. lsxGrowiDirective.sanitizeOption,
  237. refsGrowiDirective.sanitizeOption,
  238. addLineNumberAttribute.sanitizeOption,
  239. slides.sanitizeOption,
  240. )]
  241. : () => {};
  242. // add rehype plugins
  243. rehypePlugins.push(
  244. [lsxGrowiDirective.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  245. [refsGrowiDirective.rehypePlugin, { pagePath }],
  246. addLineNumberAttribute.rehypePlugin,
  247. rehypeSanitizePlugin,
  248. katex,
  249. );
  250. // add components
  251. if (components != null) {
  252. components.lsx = lsxGrowiDirective.LsxImmutable;
  253. components.ref = refsGrowiDirective.RefImmutable;
  254. components.refs = refsGrowiDirective.RefsImmutable;
  255. components.refimg = refsGrowiDirective.RefImgImmutable;
  256. components.refsimg = refsGrowiDirective.RefsImgImmutable;
  257. components.gallery = refsGrowiDirective.GalleryImmutable;
  258. components.drawio = drawio.DrawioViewer;
  259. components.mermaid = mermaid.MermaidViewer;
  260. components.attachment = RichAttachment;
  261. components.img = LightBox;
  262. components.slide = SlideViewer;
  263. }
  264. if (config.isEnabledXssPrevention) {
  265. verifySanitizePlugin(options, false);
  266. }
  267. return options;
  268. };