renderer.tsx 5.0 KB

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