renderer.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import assert from 'assert';
  2. import { isClient } from '@growi/core/dist/utils/browser-utils';
  3. import * as presentation from '@growi/presentation/dist/client/services/sanitize-option';
  4. import * as refsGrowiDirective from '@growi/remark-attachment-refs/dist/client';
  5. import * as drawio from '@growi/remark-drawio';
  6. import * as lsxGrowiDirective from '@growi/remark-lsx/dist/client';
  7. import katex from 'rehype-katex';
  8. import sanitize from 'rehype-sanitize';
  9. import slug from 'rehype-slug';
  10. import type { HtmlElementNode } from 'rehype-toc';
  11. import breaks from 'remark-breaks';
  12. import remarkGithubAdmonitionsToDirectives from 'remark-github-admonitions-to-directives';
  13. import math from 'remark-math';
  14. import deepmerge from 'ts-deepmerge';
  15. import type { Pluggable } from 'unified';
  16. import { DrawioViewerWithEditButton } from '~/client/components/ReactMarkdownComponents/DrawioViewerWithEditButton';
  17. import { Header } from '~/client/components/ReactMarkdownComponents/Header';
  18. import { LightBox } from '~/client/components/ReactMarkdownComponents/LightBox';
  19. import { RichAttachment } from '~/client/components/ReactMarkdownComponents/RichAttachment';
  20. import { TableWithEditButton } from '~/client/components/ReactMarkdownComponents/TableWithEditButton';
  21. import * as callout from '~/features/callout';
  22. import * as mermaid from '~/features/mermaid';
  23. import * as plantuml from '~/features/plantuml';
  24. import type { RendererOptions } from '~/interfaces/renderer-options';
  25. import { type RendererConfigExt } 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 codeBlock from '~/services/renderer/remark-plugins/codeblock';
  31. import * as xsvToTable from '~/services/renderer/remark-plugins/xsv-to-table';
  32. import {
  33. getCommonSanitizeOption, generateCommonOptions, 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: RendererConfigExt,
  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, isDarkMode: config.isDarkMode }],
  52. drawio.remarkPlugin,
  53. mermaid.remarkPlugin,
  54. xsvToTable.remarkPlugin,
  55. attachment.remarkPlugin,
  56. remarkGithubAdmonitionsToDirectives,
  57. callout.remarkPlugin,
  58. lsxGrowiDirective.remarkPlugin,
  59. refsGrowiDirective.remarkPlugin,
  60. );
  61. if (config.isEnabledLinebreaks) {
  62. remarkPlugins.push(breaks);
  63. }
  64. const rehypeSanitizePlugin: Pluggable | (() => void) = config.isEnabledXssPrevention
  65. ? [sanitize, deepmerge(
  66. getCommonSanitizeOption(config),
  67. presentation.sanitizeOption,
  68. drawio.sanitizeOption,
  69. mermaid.sanitizeOption,
  70. callout.sanitizeOption,
  71. attachment.sanitizeOption,
  72. lsxGrowiDirective.sanitizeOption,
  73. refsGrowiDirective.sanitizeOption,
  74. codeBlock.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 = props => <DrawioViewerWithEditButton isDarkMode={config.isDarkMode} {...props} />;
  101. components.table = TableWithEditButton;
  102. components.mermaid = mermaid.MermaidViewer;
  103. components.callout = callout.CalloutViewer;
  104. components.attachment = RichAttachment;
  105. components.img = LightBox;
  106. }
  107. if (config.isEnabledXssPrevention) {
  108. verifySanitizePlugin(options, false);
  109. }
  110. return options;
  111. };
  112. export const generateTocOptions = (config: RendererConfigExt, tocNode: HtmlElementNode | undefined): RendererOptions => {
  113. const options = generateCommonOptions(undefined);
  114. const { rehypePlugins } = options;
  115. // add remark plugins
  116. // remarkPlugins.push();
  117. const rehypeSanitizePlugin: Pluggable | (() => void) = config.isEnabledXssPrevention
  118. ? [sanitize, deepmerge(
  119. getCommonSanitizeOption(config),
  120. codeBlock.sanitizeOption,
  121. )]
  122. : () => {};
  123. // add rehype plugins
  124. rehypePlugins.push(
  125. [relocateToc.rehypePluginRestore, { tocNode }],
  126. rehypeSanitizePlugin,
  127. );
  128. if (config.isEnabledXssPrevention) {
  129. verifySanitizePlugin(options);
  130. }
  131. return options;
  132. };
  133. export const generateSimpleViewOptions = (
  134. config: RendererConfigExt,
  135. pagePath: string,
  136. highlightKeywords?: string | string[],
  137. overrideIsEnabledLinebreaks?: boolean,
  138. ): RendererOptions => {
  139. const options = generateCommonOptions(pagePath);
  140. const { remarkPlugins, rehypePlugins, components } = options;
  141. // add remark plugins
  142. remarkPlugins.push(
  143. math,
  144. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri, isDarkMode: config.isDarkMode }],
  145. drawio.remarkPlugin,
  146. mermaid.remarkPlugin,
  147. xsvToTable.remarkPlugin,
  148. attachment.remarkPlugin,
  149. remarkGithubAdmonitionsToDirectives,
  150. callout.remarkPlugin,
  151. lsxGrowiDirective.remarkPlugin,
  152. refsGrowiDirective.remarkPlugin,
  153. );
  154. const isEnabledLinebreaks = overrideIsEnabledLinebreaks ?? config.isEnabledLinebreaks;
  155. if (isEnabledLinebreaks) {
  156. remarkPlugins.push(breaks);
  157. }
  158. const rehypeSanitizePlugin: Pluggable | (() => void) = config.isEnabledXssPrevention
  159. ? [sanitize, deepmerge(
  160. getCommonSanitizeOption(config),
  161. presentation.sanitizeOption,
  162. drawio.sanitizeOption,
  163. mermaid.sanitizeOption,
  164. callout.sanitizeOption,
  165. attachment.sanitizeOption,
  166. lsxGrowiDirective.sanitizeOption,
  167. refsGrowiDirective.sanitizeOption,
  168. codeBlock.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 = props => <drawio.DrawioViewer {...props} isDarkMode={config.isDarkMode} />;
  188. components.mermaid = mermaid.MermaidViewer;
  189. components.callout = callout.CalloutViewer;
  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: RendererConfigExt,
  200. pagePath: string,
  201. ): RendererOptions => {
  202. // based on simple view options
  203. const options = generateSimpleViewOptions(config, pagePath);
  204. const { rehypePlugins } = options;
  205. const rehypeSanitizePlugin: Pluggable | (() => void) = config.isEnabledXssPrevention
  206. ? [sanitize, deepmerge(
  207. addLineNumberAttribute.sanitizeOption,
  208. )]
  209. : () => {};
  210. // add rehype plugins
  211. rehypePlugins.push(
  212. addLineNumberAttribute.rehypePlugin,
  213. rehypeSanitizePlugin,
  214. );
  215. if (config.isEnabledXssPrevention) {
  216. verifySanitizePlugin(options, false);
  217. }
  218. return options;
  219. };
  220. export const generatePreviewOptions = (config: RendererConfigExt, pagePath: string): RendererOptions => {
  221. const options = generateCommonOptions(pagePath);
  222. const { remarkPlugins, rehypePlugins, components } = options;
  223. // add remark plugins
  224. remarkPlugins.push(
  225. math,
  226. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri, isDarkMode: config.isDarkMode }],
  227. drawio.remarkPlugin,
  228. mermaid.remarkPlugin,
  229. xsvToTable.remarkPlugin,
  230. attachment.remarkPlugin,
  231. remarkGithubAdmonitionsToDirectives,
  232. callout.remarkPlugin,
  233. lsxGrowiDirective.remarkPlugin,
  234. refsGrowiDirective.remarkPlugin,
  235. );
  236. if (config.isEnabledLinebreaks) {
  237. remarkPlugins.push(breaks);
  238. }
  239. const rehypeSanitizePlugin: Pluggable | (() => void) = config.isEnabledXssPrevention
  240. ? [sanitize, deepmerge(
  241. getCommonSanitizeOption(config),
  242. drawio.sanitizeOption,
  243. mermaid.sanitizeOption,
  244. callout.sanitizeOption,
  245. attachment.sanitizeOption,
  246. lsxGrowiDirective.sanitizeOption,
  247. refsGrowiDirective.sanitizeOption,
  248. addLineNumberAttribute.sanitizeOption,
  249. codeBlock.sanitizeOption,
  250. )]
  251. : () => {};
  252. // add rehype plugins
  253. rehypePlugins.push(
  254. [lsxGrowiDirective.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  255. [refsGrowiDirective.rehypePlugin, { pagePath }],
  256. addLineNumberAttribute.rehypePlugin,
  257. rehypeSanitizePlugin,
  258. katex,
  259. );
  260. // add components
  261. if (components != null) {
  262. components.lsx = lsxGrowiDirective.LsxImmutable;
  263. components.ref = refsGrowiDirective.RefImmutable;
  264. components.refs = refsGrowiDirective.RefsImmutable;
  265. components.refimg = refsGrowiDirective.RefImgImmutable;
  266. components.refsimg = refsGrowiDirective.RefsImgImmutable;
  267. components.gallery = refsGrowiDirective.GalleryImmutable;
  268. components.drawio = props => <drawio.DrawioViewer {...props} isDarkMode={config.isDarkMode} />;
  269. components.mermaid = mermaid.MermaidViewer;
  270. components.callout = callout.CalloutViewer;
  271. components.attachment = RichAttachment;
  272. components.img = LightBox;
  273. }
  274. if (config.isEnabledXssPrevention) {
  275. verifySanitizePlugin(options, false);
  276. }
  277. return options;
  278. };