renderer.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import growiDirective from '@growi/remark-growi-directive';
  2. import type { Schema as SanitizeOption } from 'hast-util-sanitize';
  3. import katex from 'rehype-katex';
  4. import raw from 'rehype-raw';
  5. import sanitize, { defaultSchema as rehypeSanitizeDefaultSchema } from 'rehype-sanitize';
  6. import slug from 'rehype-slug';
  7. import breaks from 'remark-breaks';
  8. import emoji from 'remark-emoji';
  9. import remarkFrontmatter from 'remark-frontmatter';
  10. import gfm from 'remark-gfm';
  11. import math from 'remark-math';
  12. import toc from 'remark-toc';
  13. import deepmerge from 'ts-deepmerge';
  14. import type { Pluggable, PluginTuple } from 'unified';
  15. import { CodeBlock } from '~/components/ReactMarkdownComponents/CodeBlock';
  16. import { NextLink } from '~/components/ReactMarkdownComponents/NextLink';
  17. import * as frontmatterHider from '~/features/frontmatter-hider';
  18. import { RehypeSanitizeOption } from '~/interfaces/rehype';
  19. import type { RendererOptions } from '~/interfaces/renderer-options';
  20. import type { RendererConfig } from '~/interfaces/services/renderer';
  21. import loggerFactory from '~/utils/logger';
  22. import * as addClass from './rehype-plugins/add-class';
  23. import { relativeLinks } from './rehype-plugins/relative-links';
  24. import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
  25. import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
  26. import * as xsvToTable from './remark-plugins/xsv-to-table';
  27. // import EasyGrid from './PreProcessor/EasyGrid';
  28. const logger = loggerFactory('growi:services:renderer');
  29. type SanitizePlugin = PluginTuple<[SanitizeOption]>;
  30. const baseSanitizeSchema = {
  31. tagNames: ['iframe', 'section', 'video'],
  32. attributes: {
  33. iframe: ['allow', 'referrerpolicy', 'sandbox', 'src', 'srcdoc'],
  34. video: ['controls', 'src', 'muted', 'preload', 'width', 'height', 'autoplay'],
  35. // The special value 'data*' as a property name can be used to allow all data properties.
  36. // see: https://github.com/syntax-tree/hast-util-sanitize/
  37. '*': ['key', 'class', 'className', 'style', 'data*'],
  38. },
  39. };
  40. export const commonSanitizeOption: SanitizeOption = deepmerge(
  41. rehypeSanitizeDefaultSchema,
  42. baseSanitizeSchema,
  43. {
  44. clobberPrefix: '', // remove clobber prefix
  45. },
  46. );
  47. let isInjectedCustomSanitaizeOption = false;
  48. export const injectCustomSanitizeOption = (config: RendererConfig): void => {
  49. if (!isInjectedCustomSanitaizeOption && config.isEnabledXssPrevention && config.xssOption === RehypeSanitizeOption.CUSTOM) {
  50. commonSanitizeOption.tagNames = baseSanitizeSchema.tagNames.concat(config.tagWhitelist ?? []);
  51. commonSanitizeOption.attributes = deepmerge(baseSanitizeSchema.attributes, config.attrWhitelist ?? {});
  52. isInjectedCustomSanitaizeOption = true;
  53. }
  54. };
  55. const isSanitizePlugin = (pluggable: Pluggable): pluggable is SanitizePlugin => {
  56. if (!Array.isArray(pluggable) || pluggable.length < 2) {
  57. return false;
  58. }
  59. const sanitizeOption = pluggable[1];
  60. return 'tagNames' in sanitizeOption && 'attributes' in sanitizeOption;
  61. };
  62. const hasSanitizePlugin = (options: RendererOptions, shouldBeTheLastItem: boolean): boolean => {
  63. const { rehypePlugins } = options;
  64. if (rehypePlugins == null || rehypePlugins.length === 0) {
  65. return false;
  66. }
  67. return shouldBeTheLastItem
  68. ? isSanitizePlugin(rehypePlugins.slice(-1)[0]) // evaluate the last one
  69. : rehypePlugins.some(rehypePlugin => isSanitizePlugin(rehypePlugin));
  70. };
  71. export const verifySanitizePlugin = (options: RendererOptions, shouldBeTheLastItem = true): void => {
  72. if (hasSanitizePlugin(options, shouldBeTheLastItem)) {
  73. return;
  74. }
  75. throw new Error('The specified options does not have sanitize plugin in \'rehypePlugins\'');
  76. };
  77. export const generateCommonOptions = (pagePath: string|undefined): RendererOptions => {
  78. return {
  79. remarkPlugins: [
  80. [toc, { maxDepth: 3, tight: true }],
  81. gfm,
  82. emoji,
  83. pukiwikiLikeLinker,
  84. growiDirective,
  85. remarkFrontmatter,
  86. // frontmatterHider.remarkPlugin,
  87. ],
  88. remarkRehypeOptions: {
  89. clobberPrefix: '', // remove clobber prefix
  90. allowDangerousHtml: true,
  91. },
  92. rehypePlugins: [
  93. [relativeLinksByPukiwikiLikeLinker, { pagePath }],
  94. [relativeLinks, { pagePath }],
  95. raw,
  96. [addClass.rehypePlugin, {
  97. table: 'table table-bordered',
  98. }],
  99. // frontmatterHider.rehypePlugin,
  100. ],
  101. components: {
  102. a: NextLink,
  103. code: CodeBlock,
  104. },
  105. };
  106. };
  107. export const generateSSRViewOptions = (
  108. config: RendererConfig,
  109. pagePath: string,
  110. ): RendererOptions => {
  111. const options = generateCommonOptions(pagePath);
  112. const { remarkPlugins, rehypePlugins, components } = options;
  113. // add remark plugins
  114. remarkPlugins.push(
  115. math,
  116. xsvToTable.remarkPlugin,
  117. );
  118. const isEnabledLinebreaks = config.isEnabledLinebreaks;
  119. if (isEnabledLinebreaks) {
  120. remarkPlugins.push(breaks);
  121. }
  122. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  123. injectCustomSanitizeOption(config);
  124. }
  125. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  126. ? [sanitize, deepmerge(
  127. commonSanitizeOption,
  128. )]
  129. : () => {};
  130. // add rehype plugins
  131. rehypePlugins.push(
  132. slug,
  133. rehypeSanitizePlugin,
  134. katex,
  135. );
  136. // add components
  137. // if (components != null) {
  138. // }
  139. if (config.isEnabledXssPrevention) {
  140. verifySanitizePlugin(options, false);
  141. }
  142. return options;
  143. };