|
| 1 | +import { |
| 2 | + Card, |
| 3 | + Details, |
| 4 | + DetailsContent, |
| 5 | + DetailsSummary, |
| 6 | + Heading, |
| 7 | + Link, |
| 8 | + ListOrdered, |
| 9 | + ListUnordered, |
| 10 | + Paragraph, |
| 11 | + type ParagraphProps, |
| 12 | + Table, |
| 13 | + TableBody, |
| 14 | + TableCell, |
| 15 | + TableFoot, |
| 16 | + TableHead, |
| 17 | + TableHeaderCell, |
| 18 | + type TableProps, |
| 19 | + TableRow, |
| 20 | +} from '@digdir/designsystemet-react'; |
| 21 | +import { getMDXComponent } from 'mdx-bundler/dist/client'; |
| 22 | +import { type ComponentType, type JSX, useMemo } from 'react'; |
| 23 | +import { useTranslation } from 'react-i18next'; |
| 24 | +import { Link as RRLink } from 'react-router'; |
| 25 | +import { CodeBlock } from '../code-block/code-block'; |
| 26 | +import classes from './mdx-components.module.css'; |
| 27 | + |
| 28 | +const defaultComponents = { |
| 29 | + Details, |
| 30 | + DetailsContent, |
| 31 | + DetailsSummary, |
| 32 | + Card, |
| 33 | + Table: (props: TableProps) => ( |
| 34 | + <div className={classes.tableWrapper}> |
| 35 | + <Table {...props} /> |
| 36 | + </div> |
| 37 | + ), |
| 38 | + TableHead, |
| 39 | + TableRow, |
| 40 | + TableHeaderCell, |
| 41 | + TableBody, |
| 42 | + TableFoot, |
| 43 | + TableCell, |
| 44 | + h1: (props: JSX.IntrinsicElements['h1']) => ( |
| 45 | + <Heading className={classes.heading} level={1} data-size='xl' {...props} /> |
| 46 | + ), |
| 47 | + h2: (props: JSX.IntrinsicElements['h2']) => ( |
| 48 | + <Heading className={classes.heading} level={2} data-size='md' {...props} /> |
| 49 | + ), |
| 50 | + h3: (props: JSX.IntrinsicElements['h3']) => ( |
| 51 | + <Heading className={classes.heading} level={3} data-size='sm' {...props} /> |
| 52 | + ), |
| 53 | + h4: (props: JSX.IntrinsicElements['h4']) => ( |
| 54 | + <Heading className={classes.heading} level={4} data-size='xs' {...props} /> |
| 55 | + ), |
| 56 | + h5: (props: JSX.IntrinsicElements['h5']) => ( |
| 57 | + <Heading className={classes.heading} level={5} data-size='xs' {...props} /> |
| 58 | + ), |
| 59 | + h6: (props: JSX.IntrinsicElements['h6']) => ( |
| 60 | + <Heading className={classes.heading} level={6} data-size='xs' {...props} /> |
| 61 | + ), |
| 62 | + ol: (props: JSX.IntrinsicElements['ol']) => <ListOrdered {...props} />, |
| 63 | + ul: (props: JSX.IntrinsicElements['ul']) => <ListUnordered {...props} />, |
| 64 | + p: (props: ParagraphProps) => <Paragraph {...props} />, |
| 65 | + Link: ({ href, ...props }: JSX.IntrinsicElements['a']) => ( |
| 66 | + <Link {...props} asChild> |
| 67 | + <RRLink to={href || ''}>{props.children}</RRLink> |
| 68 | + </Link> |
| 69 | + ), |
| 70 | + a: ({ href, ...props }: JSX.IntrinsicElements['a']) => ( |
| 71 | + <Link {...props} asChild> |
| 72 | + <RRLink to={href || ''}>{props.children}</RRLink> |
| 73 | + </Link> |
| 74 | + ), |
| 75 | + pre: ({ |
| 76 | + children: { |
| 77 | + props: { children = '', className = '' }, |
| 78 | + }, |
| 79 | + }) => { |
| 80 | + return ( |
| 81 | + <CodeBlock language={className.replace('language-', '')}> |
| 82 | + {children} |
| 83 | + </CodeBlock> |
| 84 | + ); |
| 85 | + }, |
| 86 | + table: (props: TableProps) => ( |
| 87 | + <div className={classes.tableWrapper}> |
| 88 | + <Table data-color='neutral' border zebra {...props} /> |
| 89 | + </div> |
| 90 | + ), |
| 91 | +}; |
| 92 | + |
| 93 | +export const MDXComponents = ({ |
| 94 | + components, |
| 95 | + code, |
| 96 | +}: { |
| 97 | + components?: Record<string, ComponentType<unknown>>; |
| 98 | + code?: string; |
| 99 | +}) => { |
| 100 | + const { t } = useTranslation(); |
| 101 | + const Component = useMemo(() => { |
| 102 | + if (!code) return null; |
| 103 | + return getMDXComponent(code); |
| 104 | + }, [code]); |
| 105 | + |
| 106 | + return ( |
| 107 | + <> |
| 108 | + {Component ? ( |
| 109 | + <Component |
| 110 | + components={{ |
| 111 | + ...defaultComponents, |
| 112 | + ...components, |
| 113 | + }} |
| 114 | + /> |
| 115 | + ) : ( |
| 116 | + t('mdx.error.loading') |
| 117 | + )} |
| 118 | + </> |
| 119 | + ); |
| 120 | +}; |
0 commit comments