import React from "react"; import ReactMarkdown from "react-markdown"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; // @ts-ignore: No declaration file for module (TODO: should be @ts-expect-error but GUI elements is used inside project with `noImplicitAny=false`) import rehypeExternalLinks from "rehype-external-links"; import rehypeRaw from "rehype-raw"; import { remarkDefinitionList } from "remark-definition-list"; import remarkGfm from "remark-gfm"; import { PluggableList } from "unified"; import { TestableComponent } from "../../components"; import { HtmlContentBlock, HtmlContentBlockProps } from "../../components/Typography"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import { truncateMarkdown } from "./truncateMarkdown"; const DEFAULT_CUTOFF_SUFFIX = "…"; export interface MarkdownProps extends TestableComponent { children: string; /** * Allow HTML as partial content, otherwise escape HTML tags. * Use with caution! */ allowHtml?: boolean; /** * Return an object that only contains simple text without any HTML. */ removeMarkup?: boolean; /** * If defined, only elements from this list will be rendered. * This overwrites the removeMarkup parameter if both are set. */ allowedElements?: string[]; /** * Do not wrap content in a `HtmlContentBlock` component. * This option is ignored if `htmlContentBlockProps` or `data-test-id` is given. */ inheritBlock?: boolean; /** * Additional reHype plugins to execute. * @see https://github.com/remarkjs/react-markdown#architecture */ reHypePlugins?: PluggableList; /** * Name for browser target where links withing the Markdown content are opened. * Set to `false` to disable this feature. */ linkTargetName?: false | string; /** * Configure the `HtmlContentBlock` component that is automatically used as wrapper for the parsed Markdown content. */ htmlContentBlockProps?: Omit; /** * Maximum number of raw Markdown characters to render. * Content exceeding this limit is truncated at the nearest safe paragraph * boundary (or word boundary as fallback) to preserve Markdown structure. * No truncation when absent or ≤ 0. */ cutOff?: number; /** * Text appended as a trailing paragraph when content is truncated by `cutOff`. * Set to `""` to suppress the indicator entirely. * Defaults to `…` (…). */ cutOffSuffix?: string; } export const markdownAllowedInlineElements = [ // default markdown "a", "code", "em", "img", "strong", // gfm (Github Flavoured Markdown) extensions "del", // other stuff "mark", ]; export const markdownAllowedBlockElements = [ // default markdown "blockquote", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "li", "ol", "p", "pre", "ul", // gfm (Github Flavoured Markdown) extensions "input", "table", "tbody", "td", "th", "thead", "tr", // other stuff "dl", "dt", "dd", ]; const configDefault = { /* Using React Markdown configuration @see https://github.com/remarkjs/react-markdown#api */ // @see https://github.com/remarkjs/remark/blob/main/doc/plugins.md#list-of-plugins remarkPlugins: [remarkGfm, remarkDefinitionList] as PluggableList, // @see https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins rehypePlugins: [] as PluggableList, allowedElements: [...markdownAllowedInlineElements, ...markdownAllowedBlockElements], // remove all unwanted HTML markup unwrapDisallowed: true, // show escaped HTML skipHtml: false, }; /** Renders a markdown string. */ export const Markdown = ({ children, allowHtml = false, removeMarkup = false, inheritBlock = false, allowedElements, reHypePlugins, linkTargetName = "_mdref", htmlContentBlockProps, cutOff, cutOffSuffix = DEFAULT_CUTOFF_SUFFIX, ...otherProps }: MarkdownProps) => { const configHtmlExternalLinks = { rel: ["nofollow"], target: linkTargetName, }; configDefault.rehypePlugins = configDefault.rehypePlugins.concat([[rehypeExternalLinks, configHtmlExternalLinks]]); const configHtml = allowHtml ? { rehypePlugins: [...configDefault.rehypePlugins].concat([rehypeRaw]), // switch from allowed list to disallowed list allowedElements: undefined, disallowedElements: ["applet", "script", "style", "link", "iframe", "form", "button"], } : {}; const configTextOnly = removeMarkup ? { skipHtml: true, allowedElements: [], disallowedElements: undefined, } : {}; const renderContent = cutOff ? truncateMarkdown(children, cutOff, cutOffSuffix) : children; const reactMarkdownProperties = { children: renderContent.trim(), ...configDefault, ...configHtml, ...configTextOnly, components: { // eslint-disable-next-line @typescript-eslint/no-explicit-any code(props: any) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { children, className, node, inline, ...rest } = props; const match = /language-(\w+)/.exec(className || ""); return match ? ( ) : ( {children} ); }, }, allowedElements, }; if (reHypePlugins) { reactMarkdownProperties.rehypePlugins = reactMarkdownProperties.rehypePlugins.concat(reHypePlugins); } const markdownDisplay = ; return inheritBlock && !(otherProps["data-test-id"] || htmlContentBlockProps) ? ( markdownDisplay ) : ( {markdownDisplay} ); };