renderer.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 from 'rehype-sanitize';
  6. import slug from 'rehype-slug';
  7. import breaks from 'remark-breaks';
  8. import remarkDirective from 'remark-directive';
  9. import remarkFrontmatter from 'remark-frontmatter';
  10. import gfm from 'remark-gfm';
  11. import math from 'remark-math';
  12. import deepmerge from 'ts-deepmerge';
  13. import type { Pluggable, PluginTuple } from 'unified';
  14. import { CodeBlock } from '~/components/ReactMarkdownComponents/CodeBlock';
  15. import { NextLink } from '~/components/ReactMarkdownComponents/NextLink';
  16. import type { RendererOptions } from '~/interfaces/renderer-options';
  17. import { RehypeSanitizeType } from '~/interfaces/services/rehype-sanitize';
  18. import type { RendererConfig } from '~/interfaces/services/renderer';
  19. import loggerFactory from '~/utils/logger';
  20. import { tagNames as recommendedTagNames, attributes as recommendedAttributes } from './recommended-whitelist';
  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 * as codeBlock from './remark-plugins/codeblock';
  25. import * as echoDirective from './remark-plugins/echo-directive';
  26. import * as emoji from './remark-plugins/emoji';
  27. import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
  28. import * as xsvToTable from './remark-plugins/xsv-to-table';
  29. // import EasyGrid from './PreProcessor/EasyGrid';
  30. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  31. const logger = loggerFactory('growi:services:renderer');
  32. type SanitizePlugin = PluginTuple<[SanitizeOption]>;
  33. let currentInitializedSanitizeType: RehypeSanitizeType = RehypeSanitizeType.RECOMMENDED;
  34. let commonSanitizeOption: SanitizeOption;
  35. export const getCommonSanitizeOption = (config:RendererConfig): SanitizeOption => {
  36. if (commonSanitizeOption == null || config.sanitizeType !== currentInitializedSanitizeType) {
  37. // initialize
  38. commonSanitizeOption = deepmerge(
  39. {
  40. tagNames: config.sanitizeType === RehypeSanitizeType.RECOMMENDED
  41. ? recommendedTagNames
  42. : config.customTagWhitelist ?? recommendedTagNames,
  43. attributes: config.sanitizeType === RehypeSanitizeType.RECOMMENDED
  44. ? recommendedAttributes
  45. : config.customAttrWhitelist ?? recommendedAttributes,
  46. clobberPrefix: '', // remove clobber prefix
  47. },
  48. codeBlock.sanitizeOption,
  49. );
  50. currentInitializedSanitizeType = config.sanitizeType;
  51. }
  52. return commonSanitizeOption;
  53. };
  54. const isSanitizePlugin = (pluggable: Pluggable): pluggable is SanitizePlugin => {
  55. if (!Array.isArray(pluggable) || pluggable.length < 2) {
  56. return false;
  57. }
  58. const sanitizeOption = pluggable[1];
  59. return 'tagNames' in sanitizeOption && 'attributes' in sanitizeOption;
  60. };
  61. const hasSanitizePlugin = (options: RendererOptions, shouldBeTheLastItem: boolean): boolean => {
  62. const { rehypePlugins } = options;
  63. if (rehypePlugins == null || rehypePlugins.length === 0) {
  64. return false;
  65. }
  66. return shouldBeTheLastItem
  67. ? isSanitizePlugin(rehypePlugins.slice(-1)[0]) // evaluate the last one
  68. : rehypePlugins.some(rehypePlugin => isSanitizePlugin(rehypePlugin));
  69. };
  70. export const verifySanitizePlugin = (options: RendererOptions, shouldBeTheLastItem = true): void => {
  71. if (hasSanitizePlugin(options, shouldBeTheLastItem)) {
  72. return;
  73. }
  74. throw new Error('The specified options does not have sanitize plugin in \'rehypePlugins\'');
  75. };
  76. export const generateCommonOptions = (pagePath: string|undefined): RendererOptions => {
  77. return {
  78. remarkPlugins: [
  79. gfm,
  80. emoji.remarkPlugin,
  81. pukiwikiLikeLinker,
  82. growiDirective,
  83. remarkDirective,
  84. echoDirective.remarkPlugin,
  85. remarkFrontmatter,
  86. codeBlock.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. ],
  100. components: {
  101. a: NextLink,
  102. code: CodeBlock,
  103. },
  104. };
  105. };
  106. export const generateSSRViewOptions = (
  107. config: RendererConfig,
  108. pagePath: string,
  109. ): RendererOptions => {
  110. const options = generateCommonOptions(pagePath);
  111. const { remarkPlugins, rehypePlugins } = options;
  112. // add remark plugins
  113. remarkPlugins.push(
  114. math,
  115. xsvToTable.remarkPlugin,
  116. );
  117. const isEnabledLinebreaks = config.isEnabledLinebreaks;
  118. if (isEnabledLinebreaks) {
  119. remarkPlugins.push(breaks);
  120. }
  121. const rehypeSanitizePlugin: Pluggable | (() => void) = config.isEnabledXssPrevention
  122. ? [sanitize, getCommonSanitizeOption(config)]
  123. : () => {};
  124. // add rehype plugins
  125. rehypePlugins.push(
  126. slug,
  127. rehypeSanitizePlugin,
  128. katex,
  129. );
  130. // add components
  131. // if (components != null) {
  132. // }
  133. if (config.isEnabledXssPrevention) {
  134. verifySanitizePlugin(options, false);
  135. }
  136. return options;
  137. };