renderer.tsx 10 KB

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