import React, { Fragment, useMemo } from 'react'; import Link from '../Link'; import { parseMarkup } from './utils/parseMarkup'; import { type AllowLinksMode, isHrefAllowedByMode, sanitiseHref } from './utils/sanitise'; import type { MarkupActionHandler, MarkupNode, MarkupProps } from './Markup.types'; const PLACEHOLDER_REGEX = /\{\{(\w+)\}\}/g; type MarkupData = Record; function substituteData(text: string, data: MarkupData): string { return text.replace(PLACEHOLDER_REGEX, (match, key: string) => Object.prototype.hasOwnProperty.call(data, key) ? String(data[key]) : match, ); } type OnAction = MarkupActionHandler | undefined; function renderNodes( nodes: MarkupNode[], onAction: OnAction, data: MarkupData | undefined, allowLinks: AllowLinksMode, ): React.ReactNode[] { return nodes.map((node, index) => renderNode(node, onAction, data, allowLinks, index)); } function renderLinkNode( node: Extract, onAction: OnAction, data: MarkupData | undefined, allowLinks: AllowLinksMode, key: number, ): React.ReactNode { const children = renderNodes(node.children, onAction, data, allowLinks); if (node.action && onAction) { const actionId = node.action; return ( // eslint-disable-next-line jsx-a11y/click-events-have-key-events onAction(actionId)}> {children} ); } const sanitisedHref = node.href ? sanitiseHref(node.href) : undefined; if (sanitisedHref && isHrefAllowedByMode(sanitisedHref, allowLinks)) { return ( {children} ); } return {children}; } function renderNode( node: MarkupNode, onAction: OnAction, data: MarkupData | undefined, allowLinks: AllowLinksMode, key: number, ): React.ReactNode { switch (node.type) { case 'text': return ( {data ? substituteData(node.content, data) : node.content} ); case 'newline': return
; case 'important': case 'positive': case 'negative': return ( {renderNodes(node.children, onAction, data, allowLinks)} ); case 'strong': return ( {renderNodes(node.children, onAction, data, allowLinks)} ); case 'strikethrough': return ( {renderNodes(node.children, onAction, data, allowLinks)} ); case 'paragraph': return (

{renderNodes(node.children, onAction, data, allowLinks)}

); case 'link': return renderLinkNode(node, onAction, data, allowLinks, key); } } function hasParagraph(nodes: MarkupNode[]): boolean { for (const node of nodes) { if (node.type === 'paragraph') { return true; } if ('children' in node && hasParagraph(node.children)) { return true; } } return false; } /** * Parses a markup string and renders it as React elements. Supports ``, * ``, ``, ``, ``, `` (alias `

`), * and `` (alias ``) with secure href validation. * * It only processes `string` input, while `ReactNode` is passed-through without processing. * * > The component supersedes [Emphasis](?path=/docs/typography-emphasis--docs), which will be * eventually deprecated. * * @see http://storybook.wise.design/?path=/docs/typography-markup--docs * @link ../emphasis/Emphasis | Emphasis */ export const Markup: React.FC = ({ children, data, allowLinks = 'internal', onAction, as, className, 'data-testid': testId, }) => { const tree = useMemo( () => (typeof children === 'string' ? parseMarkup(children) : []), [children], ); if (typeof children !== 'string') { if (children == null) { return null; } return ( {children} ); } if (tree.length === 0) { return null; } const Element = as ?? (hasParagraph(tree) ? 'div' : 'span'); return ( {renderNodes(tree, onAction, data, allowLinks)} ); }; export default Markup;