renderer.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 { RehypeSanitizeOption } from '~/interfaces/rehype';
  18. import type { RendererOptions } from '~/interfaces/renderer-options';
  19. import type { RendererConfig } from '~/interfaces/services/renderer';
  20. import loggerFactory from '~/utils/logger';
  21. import * as addClass from './rehype-plugins/add-class';
  22. import { relativeLinks } from './rehype-plugins/relative-links';
  23. import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
  24. import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
  25. import * as xsvToTable from './remark-plugins/xsv-to-table';
  26. // import EasyGrid from './PreProcessor/EasyGrid';
  27. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  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. ],
  87. remarkRehypeOptions: {
  88. clobberPrefix: '', // remove clobber prefix
  89. allowDangerousHtml: true,
  90. },
  91. rehypePlugins: [
  92. [relativeLinksByPukiwikiLikeLinker, { pagePath }],
  93. [relativeLinks, { pagePath }],
  94. raw,
  95. [addClass.rehypePlugin, {
  96. table: 'table table-bordered',
  97. }],
  98. ],
  99. components: {
  100. a: NextLink,
  101. code: CodeBlock,
  102. },
  103. };
  104. };
  105. export const generateSSRViewOptions = (
  106. config: RendererConfig,
  107. pagePath: string,
  108. ): RendererOptions => {
  109. const options = generateCommonOptions(pagePath);
  110. const { remarkPlugins, rehypePlugins } = options;
  111. // add remark plugins
  112. remarkPlugins.push(
  113. math,
  114. xsvToTable.remarkPlugin,
  115. );
  116. const isEnabledLinebreaks = config.isEnabledLinebreaks;
  117. if (isEnabledLinebreaks) {
  118. remarkPlugins.push(breaks);
  119. }
  120. if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
  121. injectCustomSanitizeOption(config);
  122. }
  123. const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
  124. ? [sanitize, deepmerge(
  125. commonSanitizeOption,
  126. )]
  127. : () => {};
  128. // add rehype plugins
  129. rehypePlugins.push(
  130. slug,
  131. rehypeSanitizePlugin,
  132. katex,
  133. );
  134. // add components
  135. // if (components != null) {
  136. // }
  137. if (config.isEnabledXssPrevention) {
  138. verifySanitizePlugin(options, false);
  139. }
  140. return options;
  141. };