renderer.tsx 4.9 KB

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