renderer.tsx 9.6 KB

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