renderer.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 { RendererConfig } from '~/interfaces/services/renderer';
  30. import { registerGrowiFacade } from '~/utils/growi-facade';
  31. import loggerFactory from '~/utils/logger';
  32. import * as addClass from './rehype-plugins/add-class';
  33. import * as addLineNumberAttribute from './rehype-plugins/add-line-number-attribute';
  34. import * as keywordHighlighter from './rehype-plugins/keyword-highlighter';
  35. import { relativeLinks } from './rehype-plugins/relative-links';
  36. import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
  37. import * as toc from './rehype-plugins/relocate-toc';
  38. import * as plantuml from './remark-plugins/plantuml';
  39. import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
  40. import * as table from './remark-plugins/table';
  41. import * as xsvToTable from './remark-plugins/xsv-to-table';
  42. // import EasyGrid from './PreProcessor/EasyGrid';
  43. // import BlockdiagConfigurer from './markdown-it/blockdiag';
  44. const logger = loggerFactory('growi:util:GrowiRenderer');
  45. type SanitizePlugin = PluginTuple<[SanitizeOption]>;
  46. export type RendererOptions = Omit<ReactMarkdownOptions, 'remarkPlugins' | 'rehypePlugins' | 'components' | 'children'> & {
  47. remarkPlugins: PluggableList,
  48. rehypePlugins: PluggableList,
  49. components?:
  50. | Partial<
  51. Omit<NormalComponents, keyof SpecialComponents>
  52. & SpecialComponents
  53. & {
  54. [elem: string]: ComponentType<any>,
  55. }
  56. >
  57. | undefined
  58. };
  59. const commonSanitizeOption: SanitizeOption = deepmerge(
  60. sanitizeDefaultSchema,
  61. {
  62. clobberPrefix: 'mdcont-',
  63. attributes: {
  64. '*': ['class', 'className', 'style'],
  65. },
  66. },
  67. );
  68. const isSanitizePlugin = (pluggable: Pluggable): pluggable is SanitizePlugin => {
  69. if (!Array.isArray(pluggable) || pluggable.length < 2) {
  70. return false;
  71. }
  72. const sanitizeOption = pluggable[1];
  73. return 'tagNames' in sanitizeOption && 'attributes' in sanitizeOption;
  74. };
  75. const hasSanitizePlugin = (options: RendererOptions, shouldBeTheLastItem: boolean): boolean => {
  76. const { rehypePlugins } = options;
  77. if (rehypePlugins == null || rehypePlugins.length === 0) {
  78. return false;
  79. }
  80. return shouldBeTheLastItem
  81. ? isSanitizePlugin(rehypePlugins.slice(-1)[0]) // evaluate the last one
  82. : rehypePlugins.some(rehypePlugin => isSanitizePlugin(rehypePlugin));
  83. };
  84. const verifySanitizePlugin = (options: RendererOptions, shouldBeTheLastItem = true): void => {
  85. if (hasSanitizePlugin(options, shouldBeTheLastItem)) {
  86. return;
  87. }
  88. throw new Error('The specified options does not have sanitize plugin in \'rehypePlugins\'');
  89. };
  90. const generateCommonOptions = (pagePath: string|undefined): RendererOptions => {
  91. return {
  92. remarkPlugins: [
  93. gfm,
  94. emoji,
  95. pukiwikiLikeLinker,
  96. growiDirective,
  97. ],
  98. rehypePlugins: [
  99. [relativeLinksByPukiwikiLikeLinker, { pagePath }],
  100. [relativeLinks, { pagePath }],
  101. raw,
  102. [addClass.rehypePlugin, {
  103. table: 'table table-bordered',
  104. }],
  105. ],
  106. components: {
  107. a: NextLink,
  108. code: CodeBlock,
  109. },
  110. };
  111. };
  112. export const generateViewOptions = (
  113. pagePath: string,
  114. config: RendererConfig,
  115. storeTocNode: (toc: HtmlElementNode) => void,
  116. ): RendererOptions => {
  117. const options = generateCommonOptions(pagePath);
  118. const { remarkPlugins, rehypePlugins, components } = options;
  119. // add remark plugins
  120. remarkPlugins.push(
  121. math,
  122. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  123. drawioPlugin.remarkPlugin,
  124. xsvToTable.remarkPlugin,
  125. lsxGrowiPlugin.remarkPlugin,
  126. );
  127. if (config.isEnabledLinebreaks) {
  128. remarkPlugins.push(breaks);
  129. }
  130. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  131. ? [sanitize, deepmerge(
  132. commonSanitizeOption,
  133. drawioPlugin.sanitizeOption,
  134. lsxGrowiPlugin.sanitizeOption,
  135. )]
  136. : () => {};
  137. // add rehype plugins
  138. rehypePlugins.push(
  139. slug,
  140. [lsxGrowiPlugin.rehypePlugin, { pagePath }],
  141. rehypeSanitizePlugin,
  142. katex,
  143. [toc.rehypePluginStore, { storeTocNode }],
  144. );
  145. // add components
  146. if (components != null) {
  147. components.h1 = Header;
  148. components.h2 = Header;
  149. components.h3 = Header;
  150. components.lsx = Lsx;
  151. components.drawio = DrawioViewerWithEditButton;
  152. components.table = TableWithEditButton;
  153. }
  154. if (config.isEnabledXssPrevention) {
  155. verifySanitizePlugin(options, false);
  156. }
  157. return options;
  158. };
  159. export const generateTocOptions = (config: RendererConfig, tocNode: HtmlElementNode | undefined): RendererOptions => {
  160. const options = generateCommonOptions(undefined);
  161. const { rehypePlugins } = options;
  162. // add remark plugins
  163. // remarkPlugins.push();
  164. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  165. ? [sanitize, deepmerge(
  166. commonSanitizeOption,
  167. )]
  168. : () => {};
  169. // add rehype plugins
  170. rehypePlugins.push(
  171. [toc.rehypePluginRestore, { tocNode }],
  172. rehypeSanitizePlugin,
  173. );
  174. if (config.isEnabledXssPrevention) {
  175. verifySanitizePlugin(options);
  176. }
  177. return options;
  178. };
  179. export const generateSimpleViewOptions = (
  180. config: RendererConfig,
  181. pagePath: string,
  182. highlightKeywords?: string | string[],
  183. overrideIsEnabledLinebreaks?: boolean,
  184. ): RendererOptions => {
  185. const options = generateCommonOptions(pagePath);
  186. const { remarkPlugins, rehypePlugins, components } = options;
  187. // add remark plugins
  188. remarkPlugins.push(
  189. math,
  190. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  191. drawioPlugin.remarkPlugin,
  192. xsvToTable.remarkPlugin,
  193. lsxGrowiPlugin.remarkPlugin,
  194. table.remarkPlugin,
  195. );
  196. const isEnabledLinebreaks = overrideIsEnabledLinebreaks ?? config.isEnabledLinebreaks;
  197. if (isEnabledLinebreaks) {
  198. remarkPlugins.push(breaks);
  199. }
  200. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  201. ? [sanitize, deepmerge(
  202. commonSanitizeOption,
  203. drawioPlugin.sanitizeOption,
  204. lsxGrowiPlugin.sanitizeOption,
  205. )]
  206. : () => {};
  207. // add rehype plugins
  208. rehypePlugins.push(
  209. [lsxGrowiPlugin.rehypePlugin, { pagePath }],
  210. [keywordHighlighter.rehypePlugin, { keywords: highlightKeywords }],
  211. rehypeSanitizePlugin,
  212. katex,
  213. );
  214. // add components
  215. if (components != null) {
  216. components.lsx = LsxImmutable;
  217. components.drawio = drawioPlugin.DrawioViewer;
  218. components.table = Table;
  219. }
  220. if (config.isEnabledXssPrevention) {
  221. verifySanitizePlugin(options, false);
  222. }
  223. return options;
  224. };
  225. export const generatePreviewOptions = (config: RendererConfig, pagePath: string): RendererOptions => {
  226. const options = generateCommonOptions(pagePath);
  227. const { remarkPlugins, rehypePlugins, components } = options;
  228. // add remark plugins
  229. remarkPlugins.push(
  230. math,
  231. [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
  232. drawioPlugin.remarkPlugin,
  233. xsvToTable.remarkPlugin,
  234. lsxGrowiPlugin.remarkPlugin,
  235. table.remarkPlugin,
  236. );
  237. if (config.isEnabledLinebreaks) {
  238. remarkPlugins.push(breaks);
  239. }
  240. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  241. ? [sanitize, deepmerge(
  242. commonSanitizeOption,
  243. lsxGrowiPlugin.sanitizeOption,
  244. drawioPlugin.sanitizeOption,
  245. addLineNumberAttribute.sanitizeOption,
  246. )]
  247. : () => {};
  248. // add rehype plugins
  249. rehypePlugins.push(
  250. [lsxGrowiPlugin.rehypePlugin, { pagePath }],
  251. addLineNumberAttribute.rehypePlugin,
  252. rehypeSanitizePlugin,
  253. katex,
  254. );
  255. // add components
  256. if (components != null) {
  257. components.lsx = LsxImmutable;
  258. components.drawio = drawioPlugin.DrawioViewer;
  259. components.table = Table;
  260. }
  261. if (config.isEnabledXssPrevention) {
  262. verifySanitizePlugin(options, false);
  263. }
  264. return options;
  265. };
  266. // register to facade
  267. if (isClient()) {
  268. registerGrowiFacade({
  269. markdownRenderer: {
  270. optionsGenerators: {
  271. generateViewOptions,
  272. generatePreviewOptions,
  273. },
  274. },
  275. });
  276. }