renderer.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // allow only types to import from react
  2. import { 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 { Schema as SanitizeOption } from 'hast-util-sanitize';
  9. import { SpecialComponents } from 'react-markdown/lib/ast-to-react';
  10. import { NormalComponents } from 'react-markdown/lib/complex-types';
  11. import { 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 { 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 { 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 { 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. rehypePlugins: [
  107. [relativeLinksByPukiwikiLikeLinker, { pagePath }],
  108. [relativeLinks, { pagePath }],
  109. raw,
  110. [addClass.rehypePlugin, {
  111. table: 'table table-bordered',
  112. }],
  113. ],
  114. components: {
  115. a: NextLink,
  116. code: CodeBlock,
  117. },
  118. };
  119. };
  120. export const generateViewOptions = (
  121. pagePath: string,
  122. config: RendererConfig,
  123. storeTocNode: (toc: HtmlElementNode) => void,
  124. ): RendererOptions => {
  125. const options = generateCommonOptions(pagePath);
  126. const { remarkPlugins, rehypePlugins, components } = options;
  127. // add remark plugins
  128. remarkPlugins.push(
  129. math,
  130. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  131. drawioPlugin.remarkPlugin,
  132. xsvToTable.remarkPlugin,
  133. lsxGrowiPlugin.remarkPlugin,
  134. );
  135. if (config.isEnabledLinebreaks) {
  136. remarkPlugins.push(breaks);
  137. }
  138. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  139. injectCustomSanitizeOption(config);
  140. }
  141. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  142. ? [sanitize, deepmerge(
  143. commonSanitizeOption,
  144. drawioPlugin.sanitizeOption,
  145. lsxGrowiPlugin.sanitizeOption,
  146. )]
  147. : () => {};
  148. // add rehype plugins
  149. rehypePlugins.push(
  150. slug,
  151. [lsxGrowiPlugin.rehypePlugin, { pagePath }],
  152. rehypeSanitizePlugin,
  153. katex,
  154. [toc.rehypePluginStore, { storeTocNode }],
  155. );
  156. // add components
  157. if (components != null) {
  158. components.h1 = Header;
  159. components.h2 = Header;
  160. components.h3 = Header;
  161. components.lsx = Lsx;
  162. components.drawio = DrawioViewerWithEditButton;
  163. components.table = TableWithEditButton;
  164. }
  165. if (config.isEnabledXssPrevention) {
  166. verifySanitizePlugin(options, false);
  167. }
  168. return options;
  169. };
  170. export const generateTocOptions = (config: RendererConfig, tocNode: HtmlElementNode | undefined): RendererOptions => {
  171. const options = generateCommonOptions(undefined);
  172. const { rehypePlugins } = options;
  173. // add remark plugins
  174. // remarkPlugins.push();
  175. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  176. injectCustomSanitizeOption(config);
  177. }
  178. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  179. ? [sanitize, deepmerge(
  180. commonSanitizeOption,
  181. )]
  182. : () => {};
  183. // add rehype plugins
  184. rehypePlugins.push(
  185. [toc.rehypePluginRestore, { tocNode }],
  186. rehypeSanitizePlugin,
  187. );
  188. if (config.isEnabledXssPrevention) {
  189. verifySanitizePlugin(options);
  190. }
  191. return options;
  192. };
  193. export const generateSimpleViewOptions = (
  194. config: RendererConfig,
  195. pagePath: string,
  196. highlightKeywords?: string | string[],
  197. overrideIsEnabledLinebreaks?: boolean,
  198. ): RendererOptions => {
  199. const options = generateCommonOptions(pagePath);
  200. const { remarkPlugins, rehypePlugins, components } = options;
  201. // add remark plugins
  202. remarkPlugins.push(
  203. math,
  204. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  205. drawioPlugin.remarkPlugin,
  206. xsvToTable.remarkPlugin,
  207. lsxGrowiPlugin.remarkPlugin,
  208. table.remarkPlugin,
  209. );
  210. const isEnabledLinebreaks = overrideIsEnabledLinebreaks ?? config.isEnabledLinebreaks;
  211. if (isEnabledLinebreaks) {
  212. remarkPlugins.push(breaks);
  213. }
  214. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  215. injectCustomSanitizeOption(config);
  216. }
  217. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  218. ? [sanitize, deepmerge(
  219. commonSanitizeOption,
  220. drawioPlugin.sanitizeOption,
  221. lsxGrowiPlugin.sanitizeOption,
  222. )]
  223. : () => {};
  224. // add rehype plugins
  225. rehypePlugins.push(
  226. [lsxGrowiPlugin.rehypePlugin, { pagePath }],
  227. [keywordHighlighter.rehypePlugin, { keywords: highlightKeywords }],
  228. rehypeSanitizePlugin,
  229. katex,
  230. );
  231. // add components
  232. if (components != null) {
  233. components.lsx = LsxImmutable;
  234. components.drawio = drawioPlugin.DrawioViewer;
  235. components.table = Table;
  236. }
  237. if (config.isEnabledXssPrevention) {
  238. verifySanitizePlugin(options, false);
  239. }
  240. return options;
  241. };
  242. export const generateSSRViewOptions = (
  243. config: RendererConfig,
  244. pagePath: string,
  245. ): RendererOptions => {
  246. const options = generateCommonOptions(pagePath);
  247. const { remarkPlugins, rehypePlugins, components } = options;
  248. // add remark plugins
  249. remarkPlugins.push(
  250. math,
  251. xsvToTable.remarkPlugin,
  252. lsxGrowiPlugin.remarkPlugin,
  253. table.remarkPlugin,
  254. );
  255. const isEnabledLinebreaks = config.isEnabledLinebreaks;
  256. if (isEnabledLinebreaks) {
  257. remarkPlugins.push(breaks);
  258. }
  259. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  260. injectCustomSanitizeOption(config);
  261. }
  262. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  263. ? [sanitize, deepmerge(
  264. commonSanitizeOption,
  265. lsxGrowiPlugin.sanitizeOption,
  266. )]
  267. : () => {};
  268. // add rehype plugins
  269. rehypePlugins.push(
  270. [lsxGrowiPlugin.rehypePlugin, { pagePath }],
  271. rehypeSanitizePlugin,
  272. katex,
  273. );
  274. // add components
  275. if (components != null) {
  276. components.lsx = LsxImmutable;
  277. components.table = Table;
  278. }
  279. if (config.isEnabledXssPrevention) {
  280. verifySanitizePlugin(options, false);
  281. }
  282. return options;
  283. };
  284. export const generatePreviewOptions = (config: RendererConfig, pagePath: string): RendererOptions => {
  285. const options = generateCommonOptions(pagePath);
  286. const { remarkPlugins, rehypePlugins, components } = options;
  287. // add remark plugins
  288. remarkPlugins.push(
  289. math,
  290. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  291. drawioPlugin.remarkPlugin,
  292. xsvToTable.remarkPlugin,
  293. lsxGrowiPlugin.remarkPlugin,
  294. table.remarkPlugin,
  295. );
  296. if (config.isEnabledLinebreaks) {
  297. remarkPlugins.push(breaks);
  298. }
  299. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  300. injectCustomSanitizeOption(config);
  301. }
  302. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  303. ? [sanitize, deepmerge(
  304. commonSanitizeOption,
  305. lsxGrowiPlugin.sanitizeOption,
  306. drawioPlugin.sanitizeOption,
  307. addLineNumberAttribute.sanitizeOption,
  308. )]
  309. : () => {};
  310. // add rehype plugins
  311. rehypePlugins.push(
  312. [lsxGrowiPlugin.rehypePlugin, { pagePath }],
  313. addLineNumberAttribute.rehypePlugin,
  314. rehypeSanitizePlugin,
  315. katex,
  316. );
  317. // add components
  318. if (components != null) {
  319. components.lsx = LsxImmutable;
  320. components.drawio = drawioPlugin.DrawioViewer;
  321. components.table = Table;
  322. }
  323. if (config.isEnabledXssPrevention) {
  324. verifySanitizePlugin(options, false);
  325. }
  326. return options;
  327. };
  328. // register to facade
  329. if (isClient()) {
  330. registerGrowiFacade({
  331. markdownRenderer: {
  332. optionsGenerators: {
  333. generateViewOptions,
  334. generatePreviewOptions,
  335. },
  336. },
  337. });
  338. }