2
0

renderer.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // allow only types to import from react
  2. import type { ComponentType } from 'react';
  3. import { isClient } from '@growi/core';
  4. import * as drawioPlugin from '@growi/remark-drawio';
  5. import growiDirective from '@growi/remark-growi-directive';
  6. import { Lsx, LsxImmutable } from '@growi/remark-lsx/components';
  7. import * as lsxGrowiPlugin from '@growi/remark-lsx/services/renderer';
  8. import type { Schema as SanitizeOption } from 'hast-util-sanitize';
  9. import type { SpecialComponents } from 'react-markdown/lib/ast-to-react';
  10. import type { NormalComponents } from 'react-markdown/lib/complex-types';
  11. import type { ReactMarkdownOptions } from 'react-markdown/lib/react-markdown';
  12. import katex from 'rehype-katex';
  13. import raw from 'rehype-raw';
  14. import sanitize, { defaultSchema as sanitizeDefaultSchema } from 'rehype-sanitize';
  15. import slug from 'rehype-slug';
  16. import type { HtmlElementNode } from 'rehype-toc';
  17. import breaks from 'remark-breaks';
  18. import emoji from 'remark-emoji';
  19. import gfm from 'remark-gfm';
  20. import math from 'remark-math';
  21. import deepmerge from 'ts-deepmerge';
  22. import type { PluggableList, Pluggable, PluginTuple } from 'unified';
  23. import { CodeBlock } from '~/components/ReactMarkdownComponents/CodeBlock';
  24. import { DrawioViewerWithEditButton } from '~/components/ReactMarkdownComponents/DrawioViewerWithEditButton';
  25. import { Header } from '~/components/ReactMarkdownComponents/Header';
  26. import { NextLink } from '~/components/ReactMarkdownComponents/NextLink';
  27. import { Table } from '~/components/ReactMarkdownComponents/Table';
  28. import { TableWithEditButton } from '~/components/ReactMarkdownComponents/TableWithEditButton';
  29. import { RehypeSanitizeOption } from '~/interfaces/rehype';
  30. import type { RendererConfig } from '~/interfaces/services/renderer';
  31. import { registerGrowiFacade } from '~/utils/growi-facade';
  32. import loggerFactory from '~/utils/logger';
  33. import * as addClass from './rehype-plugins/add-class';
  34. import * as addLineNumberAttribute from './rehype-plugins/add-line-number-attribute';
  35. import * as keywordHighlighter from './rehype-plugins/keyword-highlighter';
  36. import { relativeLinks } from './rehype-plugins/relative-links';
  37. import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
  38. import * as toc from './rehype-plugins/relocate-toc';
  39. import * as plantuml from './remark-plugins/plantuml';
  40. import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
  41. import * as table from './remark-plugins/table';
  42. import * as xsvToTable from './remark-plugins/xsv-to-table';
  43. // import EasyGrid from './PreProcessor/EasyGrid';
  44. // import BlockdiagConfigurer from './markdown-it/blockdiag';
  45. const logger = loggerFactory('growi:util:GrowiRenderer');
  46. type SanitizePlugin = PluginTuple<[SanitizeOption]>;
  47. export type RendererOptions = Omit<ReactMarkdownOptions, 'remarkPlugins' | 'rehypePlugins' | 'components' | 'children'> & {
  48. remarkPlugins: PluggableList,
  49. rehypePlugins: PluggableList,
  50. components?:
  51. | Partial<
  52. Omit<NormalComponents, keyof SpecialComponents>
  53. & SpecialComponents
  54. & {
  55. [elem: string]: ComponentType<any>,
  56. }
  57. >
  58. | undefined
  59. };
  60. const commonSanitizeAttributes = { '*': ['class', 'className', 'style'] };
  61. const commonSanitizeOption: SanitizeOption = deepmerge(
  62. sanitizeDefaultSchema,
  63. {
  64. clobberPrefix: 'mdcont-',
  65. attributes: commonSanitizeAttributes,
  66. },
  67. );
  68. let isInjectedCustomSanitaizeOption = false;
  69. const injectCustomSanitizeOption = (config: RendererConfig) => {
  70. if (!isInjectedCustomSanitaizeOption && config.isEnabledXssPrevention && config.xssOption === RehypeSanitizeOption.CUSTOM) {
  71. commonSanitizeOption.tagNames = config.tagWhiteList;
  72. commonSanitizeOption.attributes = deepmerge(commonSanitizeAttributes, config.attrWhiteList ?? {});
  73. isInjectedCustomSanitaizeOption = true;
  74. }
  75. };
  76. const isSanitizePlugin = (pluggable: Pluggable): pluggable is SanitizePlugin => {
  77. if (!Array.isArray(pluggable) || pluggable.length < 2) {
  78. return false;
  79. }
  80. const sanitizeOption = pluggable[1];
  81. return 'tagNames' in sanitizeOption && 'attributes' in sanitizeOption;
  82. };
  83. const hasSanitizePlugin = (options: RendererOptions, shouldBeTheLastItem: boolean): boolean => {
  84. const { rehypePlugins } = options;
  85. if (rehypePlugins == null || rehypePlugins.length === 0) {
  86. return false;
  87. }
  88. return shouldBeTheLastItem
  89. ? isSanitizePlugin(rehypePlugins.slice(-1)[0]) // evaluate the last one
  90. : rehypePlugins.some(rehypePlugin => isSanitizePlugin(rehypePlugin));
  91. };
  92. const verifySanitizePlugin = (options: RendererOptions, shouldBeTheLastItem = true): void => {
  93. if (hasSanitizePlugin(options, shouldBeTheLastItem)) {
  94. return;
  95. }
  96. throw new Error('The specified options does not have sanitize plugin in \'rehypePlugins\'');
  97. };
  98. const generateCommonOptions = (pagePath: string|undefined): RendererOptions => {
  99. return {
  100. remarkPlugins: [
  101. gfm,
  102. emoji,
  103. pukiwikiLikeLinker,
  104. growiDirective,
  105. ],
  106. remarkRehypeOptions: {
  107. clobberPrefix: 'mdcont-',
  108. allowDangerousHtml: true,
  109. },
  110. rehypePlugins: [
  111. [relativeLinksByPukiwikiLikeLinker, { pagePath }],
  112. [relativeLinks, { pagePath }],
  113. raw,
  114. [addClass.rehypePlugin, {
  115. table: 'table table-bordered',
  116. }],
  117. ],
  118. components: {
  119. a: NextLink,
  120. code: CodeBlock,
  121. },
  122. };
  123. };
  124. export const generateViewOptions = (
  125. pagePath: string,
  126. config: RendererConfig,
  127. storeTocNode: (toc: HtmlElementNode) => void,
  128. ): RendererOptions => {
  129. const options = generateCommonOptions(pagePath);
  130. const { remarkPlugins, rehypePlugins, components } = options;
  131. // add remark plugins
  132. remarkPlugins.push(
  133. math,
  134. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  135. drawioPlugin.remarkPlugin,
  136. xsvToTable.remarkPlugin,
  137. lsxGrowiPlugin.remarkPlugin,
  138. );
  139. if (config.isEnabledLinebreaks) {
  140. remarkPlugins.push(breaks);
  141. }
  142. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  143. injectCustomSanitizeOption(config);
  144. }
  145. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  146. ? [sanitize, deepmerge(
  147. commonSanitizeOption,
  148. drawioPlugin.sanitizeOption,
  149. lsxGrowiPlugin.sanitizeOption,
  150. )]
  151. : () => {};
  152. // add rehype plugins
  153. rehypePlugins.push(
  154. slug,
  155. [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  156. rehypeSanitizePlugin,
  157. katex,
  158. [toc.rehypePluginStore, { storeTocNode }],
  159. );
  160. // add components
  161. if (components != null) {
  162. components.h1 = Header;
  163. components.h2 = Header;
  164. components.h3 = Header;
  165. components.lsx = Lsx;
  166. components.drawio = DrawioViewerWithEditButton;
  167. components.table = TableWithEditButton;
  168. }
  169. if (config.isEnabledXssPrevention) {
  170. verifySanitizePlugin(options, false);
  171. }
  172. return options;
  173. };
  174. export const generateTocOptions = (config: RendererConfig, tocNode: HtmlElementNode | undefined): RendererOptions => {
  175. const options = generateCommonOptions(undefined);
  176. const { rehypePlugins } = options;
  177. // add remark plugins
  178. // remarkPlugins.push();
  179. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  180. injectCustomSanitizeOption(config);
  181. }
  182. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  183. ? [sanitize, deepmerge(
  184. commonSanitizeOption,
  185. )]
  186. : () => {};
  187. // add rehype plugins
  188. rehypePlugins.push(
  189. [toc.rehypePluginRestore, { tocNode }],
  190. rehypeSanitizePlugin,
  191. );
  192. if (config.isEnabledXssPrevention) {
  193. verifySanitizePlugin(options);
  194. }
  195. return options;
  196. };
  197. export const generateSimpleViewOptions = (
  198. config: RendererConfig,
  199. pagePath: string,
  200. highlightKeywords?: string | string[],
  201. overrideIsEnabledLinebreaks?: boolean,
  202. ): RendererOptions => {
  203. const options = generateCommonOptions(pagePath);
  204. const { remarkPlugins, rehypePlugins, components } = options;
  205. // add remark plugins
  206. remarkPlugins.push(
  207. math,
  208. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  209. drawioPlugin.remarkPlugin,
  210. xsvToTable.remarkPlugin,
  211. lsxGrowiPlugin.remarkPlugin,
  212. table.remarkPlugin,
  213. );
  214. const isEnabledLinebreaks = overrideIsEnabledLinebreaks ?? config.isEnabledLinebreaks;
  215. if (isEnabledLinebreaks) {
  216. remarkPlugins.push(breaks);
  217. }
  218. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  219. injectCustomSanitizeOption(config);
  220. }
  221. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  222. ? [sanitize, deepmerge(
  223. commonSanitizeOption,
  224. drawioPlugin.sanitizeOption,
  225. lsxGrowiPlugin.sanitizeOption,
  226. )]
  227. : () => {};
  228. // add rehype plugins
  229. rehypePlugins.push(
  230. [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  231. [keywordHighlighter.rehypePlugin, { keywords: highlightKeywords }],
  232. rehypeSanitizePlugin,
  233. katex,
  234. );
  235. // add components
  236. if (components != null) {
  237. components.lsx = LsxImmutable;
  238. components.drawio = drawioPlugin.DrawioViewer;
  239. components.table = Table;
  240. }
  241. if (config.isEnabledXssPrevention) {
  242. verifySanitizePlugin(options, false);
  243. }
  244. return options;
  245. };
  246. export const generatePresentationViewOptions = (
  247. config: RendererConfig,
  248. pagePath: string,
  249. ): RendererOptions => {
  250. // based on simple view options
  251. const options = generateSimpleViewOptions(config, pagePath);
  252. if (config.isEnabledXssPrevention) {
  253. verifySanitizePlugin(options, false);
  254. }
  255. return options;
  256. };
  257. export const generateSSRViewOptions = (
  258. config: RendererConfig,
  259. pagePath: string,
  260. ): RendererOptions => {
  261. const options = generateCommonOptions(pagePath);
  262. const { remarkPlugins, rehypePlugins, components } = options;
  263. // add remark plugins
  264. remarkPlugins.push(
  265. math,
  266. xsvToTable.remarkPlugin,
  267. lsxGrowiPlugin.remarkPlugin,
  268. table.remarkPlugin,
  269. );
  270. const isEnabledLinebreaks = config.isEnabledLinebreaks;
  271. if (isEnabledLinebreaks) {
  272. remarkPlugins.push(breaks);
  273. }
  274. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  275. injectCustomSanitizeOption(config);
  276. }
  277. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  278. ? [sanitize, deepmerge(
  279. commonSanitizeOption,
  280. lsxGrowiPlugin.sanitizeOption,
  281. )]
  282. : () => {};
  283. // add rehype plugins
  284. rehypePlugins.push(
  285. slug,
  286. [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  287. rehypeSanitizePlugin,
  288. katex,
  289. );
  290. // add components
  291. if (components != null) {
  292. components.lsx = LsxImmutable;
  293. components.table = Table;
  294. }
  295. if (config.isEnabledXssPrevention) {
  296. verifySanitizePlugin(options, false);
  297. }
  298. return options;
  299. };
  300. export const generatePreviewOptions = (config: RendererConfig, pagePath: string): RendererOptions => {
  301. const options = generateCommonOptions(pagePath);
  302. const { remarkPlugins, rehypePlugins, components } = options;
  303. // add remark plugins
  304. remarkPlugins.push(
  305. math,
  306. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  307. drawioPlugin.remarkPlugin,
  308. xsvToTable.remarkPlugin,
  309. lsxGrowiPlugin.remarkPlugin,
  310. table.remarkPlugin,
  311. );
  312. if (config.isEnabledLinebreaks) {
  313. remarkPlugins.push(breaks);
  314. }
  315. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  316. injectCustomSanitizeOption(config);
  317. }
  318. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  319. ? [sanitize, deepmerge(
  320. commonSanitizeOption,
  321. lsxGrowiPlugin.sanitizeOption,
  322. drawioPlugin.sanitizeOption,
  323. addLineNumberAttribute.sanitizeOption,
  324. )]
  325. : () => {};
  326. // add rehype plugins
  327. rehypePlugins.push(
  328. [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
  329. addLineNumberAttribute.rehypePlugin,
  330. rehypeSanitizePlugin,
  331. katex,
  332. );
  333. // add components
  334. if (components != null) {
  335. components.lsx = LsxImmutable;
  336. components.drawio = drawioPlugin.DrawioViewer;
  337. components.table = Table;
  338. }
  339. if (config.isEnabledXssPrevention) {
  340. verifySanitizePlugin(options, false);
  341. }
  342. return options;
  343. };
  344. // register to facade
  345. if (isClient()) {
  346. registerGrowiFacade({
  347. markdownRenderer: {
  348. optionsGenerators: {
  349. generateViewOptions,
  350. generatePreviewOptions,
  351. },
  352. },
  353. });
  354. }