|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { ComponentProps, isValidElement, ReactElement } from 'react'; |
| 9 | +import Head from '@docusaurus/Head'; |
| 10 | +import Link from '@docusaurus/Link'; |
| 11 | +import CodeBlock, { Props } from '@theme/CodeBlock'; |
| 12 | +import Heading from '@theme/Heading'; |
| 13 | +import Details from '@theme/Details'; |
| 14 | +import type { MDXComponentsObject } from '@theme/MDXComponents'; |
| 15 | +import Question from '@site/src/components/Question'; |
| 16 | + |
| 17 | +import './styles.css'; |
| 18 | + |
| 19 | +// MDX elements are wrapped through the MDX pragma |
| 20 | +// In some cases (notably usage with Head/Helmet) we need to unwrap those elements. |
| 21 | +function unwrapMDXElement(element: ReactElement) { |
| 22 | + if (element?.props?.mdxType && element?.props?.originalType) { |
| 23 | + const { mdxType, originalType, ...newProps } = element.props; |
| 24 | + return React.createElement(element.props.originalType, newProps); |
| 25 | + } |
| 26 | + return element; |
| 27 | +} |
| 28 | + |
| 29 | +const MDXComponents: MDXComponentsObject & { |
| 30 | + question: (props) => JSX.Element; |
| 31 | +} = { |
| 32 | + head: (props) => { |
| 33 | + const unwrappedChildren = React.Children.map(props.children, (child) => |
| 34 | + unwrapMDXElement(child as ReactElement), |
| 35 | + ); |
| 36 | + return <Head {...props}>{unwrappedChildren}</Head>; |
| 37 | + }, |
| 38 | + code: (props) => { |
| 39 | + const { children } = props; |
| 40 | + |
| 41 | + // For retrocompatibility purposes (pretty rare use case) |
| 42 | + // See https://github.com/facebook/docusaurus/pull/1584 |
| 43 | + if (isValidElement(children)) { |
| 44 | + return children; |
| 45 | + } |
| 46 | + |
| 47 | + return !children.includes('\n') ? ( |
| 48 | + <code {...props} /> |
| 49 | + ) : ( |
| 50 | + <CodeBlock {...props} /> |
| 51 | + ); |
| 52 | + }, |
| 53 | + a: (props) => <Link {...props} />, |
| 54 | + pre: (props) => { |
| 55 | + const { children } = props; |
| 56 | + |
| 57 | + // See comment for `code` above |
| 58 | + if (isValidElement(children) && isValidElement(children?.props?.children)) { |
| 59 | + return children.props.children; |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <CodeBlock |
| 64 | + {...((isValidElement(children) |
| 65 | + ? children?.props |
| 66 | + : { ...props }) as Props)} |
| 67 | + /> |
| 68 | + ); |
| 69 | + }, |
| 70 | + details: (props): JSX.Element => { |
| 71 | + const items = React.Children.toArray(props.children) as ReactElement[]; |
| 72 | + // Split summary item from the rest to pass it as a separate prop to the Detais theme component |
| 73 | + const summary: ReactElement<ComponentProps<'summary'>> = items.find( |
| 74 | + (item) => item?.props?.mdxType === 'summary', |
| 75 | + )!; |
| 76 | + const children = <>{items.filter((item) => item !== summary)}</>; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Details {...props} summary={summary}> |
| 80 | + {children} |
| 81 | + </Details> |
| 82 | + ); |
| 83 | + }, |
| 84 | + question: (props) => <Question {...props} />, |
| 85 | + h1: Heading('h1'), |
| 86 | + h2: Heading('h2'), |
| 87 | + h3: Heading('h3'), |
| 88 | + h4: Heading('h4'), |
| 89 | + h5: Heading('h5'), |
| 90 | + h6: Heading('h6'), |
| 91 | +}; |
| 92 | + |
| 93 | +export default MDXComponents; |
0 commit comments