| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- // allow only types to import from react
- import type { ComponentType } from 'react';
- import { isClient } from '@growi/core';
- import * as drawioPlugin from '@growi/remark-drawio';
- import growiDirective from '@growi/remark-growi-directive';
- import { Lsx, LsxImmutable } from '@growi/remark-lsx/components';
- import * as lsxGrowiPlugin from '@growi/remark-lsx/services/renderer';
- import type { Schema as SanitizeOption } from 'hast-util-sanitize';
- import type { SpecialComponents } from 'react-markdown-customkeyprop/lib/ast-to-react';
- import type { NormalComponents } from 'react-markdown-customkeyprop/lib/complex-types';
- import type { ReactMarkdownOptions } from 'react-markdown-customkeyprop/lib/react-markdown';
- import katex from 'rehype-katex';
- import raw from 'rehype-raw';
- import sanitize, { defaultSchema as rehypeSanitizeDefaultSchema } from 'rehype-sanitize';
- import slug from 'rehype-slug';
- import type { HtmlElementNode } from 'rehype-toc';
- import breaks from 'remark-breaks';
- import emoji from 'remark-emoji';
- import gfm from 'remark-gfm';
- import math from 'remark-math';
- import toc from 'remark-toc';
- import deepmerge from 'ts-deepmerge';
- import type { PluggableList, Pluggable, PluginTuple } from 'unified';
- import { CodeBlock } from '~/components/ReactMarkdownComponents/CodeBlock';
- import { DrawioViewerWithEditButton } from '~/components/ReactMarkdownComponents/DrawioViewerWithEditButton';
- import { Header } from '~/components/ReactMarkdownComponents/Header';
- import { NextLink } from '~/components/ReactMarkdownComponents/NextLink';
- import { TableWithEditButton } from '~/components/ReactMarkdownComponents/TableWithEditButton';
- import { RehypeSanitizeOption } from '~/interfaces/rehype';
- import type { RendererConfig } from '~/interfaces/services/renderer';
- import { registerGrowiFacade } from '~/utils/growi-facade';
- import loggerFactory from '~/utils/logger';
- import * as addClass from './rehype-plugins/add-class';
- import * as addLineNumberAttribute from './rehype-plugins/add-line-number-attribute';
- import * as keywordHighlighter from './rehype-plugins/keyword-highlighter';
- import { relativeLinks } from './rehype-plugins/relative-links';
- import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
- import * as relocateToc from './rehype-plugins/relocate-toc';
- import * as plantuml from './remark-plugins/plantuml';
- import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
- import * as xsvToTable from './remark-plugins/xsv-to-table';
- // import EasyGrid from './PreProcessor/EasyGrid';
- // import BlockdiagConfigurer from './markdown-it/blockdiag';
- const logger = loggerFactory('growi:util:GrowiRenderer');
- type SanitizePlugin = PluginTuple<[SanitizeOption]>;
- export type RendererOptions = Omit<ReactMarkdownOptions, 'remarkPlugins' | 'rehypePlugins' | 'components' | 'children'> & {
- remarkPlugins: PluggableList,
- rehypePlugins: PluggableList,
- components?:
- | Partial<
- Omit<NormalComponents, keyof SpecialComponents>
- & SpecialComponents
- & {
- [elem: string]: ComponentType<any>,
- }
- >
- | undefined
- };
- const baseSanitizeSchema = {
- tagNames: ['iframe', 'section'],
- attributes: {
- iframe: ['allow', 'referrerpolicy', 'sandbox', 'src', 'srcdoc'],
- // The special value 'data*' as a property name can be used to allow all data properties.
- // see: https://github.com/syntax-tree/hast-util-sanitize/
- '*': ['key', 'class', 'className', 'style', 'data*'],
- },
- };
- const commonSanitizeOption: SanitizeOption = deepmerge(
- rehypeSanitizeDefaultSchema,
- baseSanitizeSchema,
- {
- clobberPrefix: 'mdcont-',
- },
- );
- let isInjectedCustomSanitaizeOption = false;
- const injectCustomSanitizeOption = (config: RendererConfig) => {
- if (!isInjectedCustomSanitaizeOption && config.isEnabledXssPrevention && config.xssOption === RehypeSanitizeOption.CUSTOM) {
- commonSanitizeOption.tagNames = baseSanitizeSchema.tagNames.concat(config.tagWhiteList ?? []);
- commonSanitizeOption.attributes = deepmerge(baseSanitizeSchema.attributes, config.attrWhiteList ?? {});
- isInjectedCustomSanitaizeOption = true;
- }
- };
- const isSanitizePlugin = (pluggable: Pluggable): pluggable is SanitizePlugin => {
- if (!Array.isArray(pluggable) || pluggable.length < 2) {
- return false;
- }
- const sanitizeOption = pluggable[1];
- return 'tagNames' in sanitizeOption && 'attributes' in sanitizeOption;
- };
- const hasSanitizePlugin = (options: RendererOptions, shouldBeTheLastItem: boolean): boolean => {
- const { rehypePlugins } = options;
- if (rehypePlugins == null || rehypePlugins.length === 0) {
- return false;
- }
- return shouldBeTheLastItem
- ? isSanitizePlugin(rehypePlugins.slice(-1)[0]) // evaluate the last one
- : rehypePlugins.some(rehypePlugin => isSanitizePlugin(rehypePlugin));
- };
- const verifySanitizePlugin = (options: RendererOptions, shouldBeTheLastItem = true): void => {
- if (hasSanitizePlugin(options, shouldBeTheLastItem)) {
- return;
- }
- throw new Error('The specified options does not have sanitize plugin in \'rehypePlugins\'');
- };
- const generateCommonOptions = (pagePath: string|undefined): RendererOptions => {
- return {
- remarkPlugins: [
- [toc, { maxDepth: 3, tight: true, prefix: 'mdcont-' }],
- gfm,
- emoji,
- pukiwikiLikeLinker,
- growiDirective,
- ],
- remarkRehypeOptions: {
- clobberPrefix: 'mdcont-',
- allowDangerousHtml: true,
- },
- rehypePlugins: [
- [relativeLinksByPukiwikiLikeLinker, { pagePath }],
- [relativeLinks, { pagePath }],
- raw,
- [addClass.rehypePlugin, {
- table: 'table table-bordered',
- }],
- ],
- components: {
- a: NextLink,
- code: CodeBlock,
- },
- };
- };
- export const generateViewOptions = (
- pagePath: string,
- config: RendererConfig,
- storeTocNode: (toc: HtmlElementNode) => void,
- ): RendererOptions => {
- const options = generateCommonOptions(pagePath);
- const { remarkPlugins, rehypePlugins, components } = options;
- // add remark plugins
- remarkPlugins.push(
- math,
- [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
- drawioPlugin.remarkPlugin,
- xsvToTable.remarkPlugin,
- lsxGrowiPlugin.remarkPlugin,
- );
- if (config.isEnabledLinebreaks) {
- remarkPlugins.push(breaks);
- }
- if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
- injectCustomSanitizeOption(config);
- }
- const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
- ? [sanitize, deepmerge(
- commonSanitizeOption,
- drawioPlugin.sanitizeOption,
- lsxGrowiPlugin.sanitizeOption,
- )]
- : () => {};
- // add rehype plugins
- rehypePlugins.push(
- slug,
- [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
- rehypeSanitizePlugin,
- katex,
- [relocateToc.rehypePluginStore, { storeTocNode }],
- );
- // add components
- if (components != null) {
- components.h1 = Header;
- components.h2 = Header;
- components.h3 = Header;
- components.h4 = Header;
- components.h5 = Header;
- components.h6 = Header;
- components.lsx = Lsx;
- components.drawio = DrawioViewerWithEditButton;
- components.table = TableWithEditButton;
- }
- if (config.isEnabledXssPrevention) {
- verifySanitizePlugin(options, false);
- }
- return options;
- };
- export const generateTocOptions = (config: RendererConfig, tocNode: HtmlElementNode | undefined): RendererOptions => {
- const options = generateCommonOptions(undefined);
- const { rehypePlugins } = options;
- // add remark plugins
- // remarkPlugins.push();
- if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
- injectCustomSanitizeOption(config);
- }
- const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
- ? [sanitize, deepmerge(
- commonSanitizeOption,
- )]
- : () => {};
- // add rehype plugins
- rehypePlugins.push(
- [relocateToc.rehypePluginRestore, { tocNode }],
- rehypeSanitizePlugin,
- );
- if (config.isEnabledXssPrevention) {
- verifySanitizePlugin(options);
- }
- return options;
- };
- export const generateSimpleViewOptions = (
- config: RendererConfig,
- pagePath: string,
- highlightKeywords?: string | string[],
- overrideIsEnabledLinebreaks?: boolean,
- ): RendererOptions => {
- const options = generateCommonOptions(pagePath);
- const { remarkPlugins, rehypePlugins, components } = options;
- // add remark plugins
- remarkPlugins.push(
- math,
- [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
- drawioPlugin.remarkPlugin,
- xsvToTable.remarkPlugin,
- lsxGrowiPlugin.remarkPlugin,
- // table.remarkPlugin,
- );
- const isEnabledLinebreaks = overrideIsEnabledLinebreaks ?? config.isEnabledLinebreaks;
- if (isEnabledLinebreaks) {
- remarkPlugins.push(breaks);
- }
- if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
- injectCustomSanitizeOption(config);
- }
- const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
- ? [sanitize, deepmerge(
- commonSanitizeOption,
- drawioPlugin.sanitizeOption,
- lsxGrowiPlugin.sanitizeOption,
- )]
- : () => {};
- // add rehype plugins
- rehypePlugins.push(
- [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
- [keywordHighlighter.rehypePlugin, { keywords: highlightKeywords }],
- rehypeSanitizePlugin,
- katex,
- );
- // add components
- if (components != null) {
- components.lsx = LsxImmutable;
- components.drawio = drawioPlugin.DrawioViewer;
- }
- if (config.isEnabledXssPrevention) {
- verifySanitizePlugin(options, false);
- }
- return options;
- };
- export const generatePresentationViewOptions = (
- config: RendererConfig,
- pagePath: string,
- ): RendererOptions => {
- // based on simple view options
- const options = generateSimpleViewOptions(config, pagePath);
- if (config.isEnabledXssPrevention) {
- verifySanitizePlugin(options, false);
- }
- return options;
- };
- export const generateSSRViewOptions = (
- config: RendererConfig,
- pagePath: string,
- ): RendererOptions => {
- const options = generateCommonOptions(pagePath);
- const { remarkPlugins, rehypePlugins, components } = options;
- // add remark plugins
- remarkPlugins.push(
- math,
- xsvToTable.remarkPlugin,
- lsxGrowiPlugin.remarkPlugin,
- // table.remarkPlugin,
- );
- const isEnabledLinebreaks = config.isEnabledLinebreaks;
- if (isEnabledLinebreaks) {
- remarkPlugins.push(breaks);
- }
- if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
- injectCustomSanitizeOption(config);
- }
- const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
- ? [sanitize, deepmerge(
- commonSanitizeOption,
- lsxGrowiPlugin.sanitizeOption,
- )]
- : () => {};
- // add rehype plugins
- rehypePlugins.push(
- slug,
- [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
- rehypeSanitizePlugin,
- katex,
- );
- // add components
- if (components != null) {
- components.lsx = LsxImmutable;
- }
- if (config.isEnabledXssPrevention) {
- verifySanitizePlugin(options, false);
- }
- return options;
- };
- export const generatePreviewOptions = (config: RendererConfig, pagePath: string): RendererOptions => {
- const options = generateCommonOptions(pagePath);
- const { remarkPlugins, rehypePlugins, components } = options;
- // add remark plugins
- remarkPlugins.push(
- math,
- [plantuml.remarkPlugin, { plantumlUri: config.plantumlUri }],
- drawioPlugin.remarkPlugin,
- xsvToTable.remarkPlugin,
- lsxGrowiPlugin.remarkPlugin,
- // table.remarkPlugin,
- );
- if (config.isEnabledLinebreaks) {
- remarkPlugins.push(breaks);
- }
- if (config.xssOption === RehypeSanitizeOption.CUSTOM) {
- injectCustomSanitizeOption(config);
- }
- const rehypeSanitizePlugin: Pluggable<any[]> | (() => void) = config.isEnabledXssPrevention
- ? [sanitize, deepmerge(
- commonSanitizeOption,
- lsxGrowiPlugin.sanitizeOption,
- drawioPlugin.sanitizeOption,
- addLineNumberAttribute.sanitizeOption,
- )]
- : () => {};
- // add rehype plugins
- rehypePlugins.push(
- [lsxGrowiPlugin.rehypePlugin, { pagePath, isSharedPage: config.isSharedPage }],
- addLineNumberAttribute.rehypePlugin,
- rehypeSanitizePlugin,
- katex,
- );
- // add components
- if (components != null) {
- components.lsx = LsxImmutable;
- components.drawio = drawioPlugin.DrawioViewer;
- }
- if (config.isEnabledXssPrevention) {
- verifySanitizePlugin(options, false);
- }
- return options;
- };
- // register to facade
- if (isClient()) {
- registerGrowiFacade({
- markdownRenderer: {
- optionsGenerators: {
- generateViewOptions,
- generatePreviewOptions,
- },
- },
- });
- }
|