-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract MDX components (#6989)
- Loading branch information
Showing
14 changed files
with
368 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/docusaurus-theme-classic/src/theme/MDXComponents/A.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import Link from '@docusaurus/Link'; | ||
import type {Props} from '@theme/MDXComponents/A'; | ||
|
||
export default function MDXA(props: Props): JSX.Element { | ||
return <Link {...props} />; | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import type {ComponentProps} from 'react'; | ||
import React, {isValidElement} from 'react'; | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import type {Props} from '@theme/MDXComponents/Code'; | ||
|
||
export default function MDXCode(props: Props): JSX.Element { | ||
const inlineElements = [ | ||
'a', | ||
'b', | ||
'big', | ||
'i', | ||
'span', | ||
'em', | ||
'strong', | ||
'sup', | ||
'sub', | ||
'small', | ||
]; | ||
const shouldBeInline = React.Children.toArray(props.children).every( | ||
(el) => | ||
(typeof el === 'string' && !el.includes('\n')) || | ||
(isValidElement(el) && inlineElements.includes(el.props.mdxType)), | ||
); | ||
|
||
return shouldBeInline ? ( | ||
<code {...props} /> | ||
) : ( | ||
<CodeBlock {...(props as ComponentProps<typeof CodeBlock>)} /> | ||
); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/docusaurus-theme-classic/src/theme/MDXComponents/Details.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {type ComponentProps, type ReactElement} from 'react'; | ||
import Details from '@theme/Details'; | ||
import type {Props} from '@theme/MDXComponents/Details'; | ||
|
||
export default function MDXDetails(props: Props): JSX.Element { | ||
const items = React.Children.toArray(props.children) as ReactElement[]; | ||
// Split summary item from the rest to pass it as a separate prop to the | ||
// Details theme component | ||
const summary: ReactElement<ComponentProps<'summary'>> = items.find( | ||
(item) => item?.props?.mdxType === 'summary', | ||
)!; | ||
const children = <>{items.filter((item) => item !== summary)}</>; | ||
|
||
return ( | ||
<Details {...props} summary={summary}> | ||
{children} | ||
</Details> | ||
); | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/docusaurus-theme-classic/src/theme/MDXComponents/Head.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {type ReactElement, type ComponentProps} from 'react'; | ||
import Head from '@docusaurus/Head'; | ||
import type {Props} from '@theme/MDXComponents/Head'; | ||
|
||
// MDX elements are wrapped through the MDX pragma. In some cases (notably usage | ||
// with Head/Helmet) we need to unwrap those elements. | ||
function unwrapMDXElement(element: ReactElement) { | ||
if (element?.props?.mdxType && element?.props?.originalType) { | ||
const {mdxType, originalType, ...newProps} = element.props; | ||
return React.createElement(element.props.originalType, newProps); | ||
} | ||
return element; | ||
} | ||
|
||
export default function MDXHead(props: Props): JSX.Element { | ||
const unwrappedChildren = React.Children.map(props.children, (child) => | ||
unwrapMDXElement(child as ReactElement), | ||
); | ||
return ( | ||
<Head {...(props as ComponentProps<typeof Head>)}>{unwrappedChildren}</Head> | ||
); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/docusaurus-theme-classic/src/theme/MDXComponents/Heading.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import Heading from '@theme/Heading'; | ||
import type {Props} from '@theme/MDXComponents/Heading'; | ||
|
||
export default function MDXHeading(props: Props): JSX.Element { | ||
return <Heading {...props} />; | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/docusaurus-theme-classic/src/theme/MDXComponents/Img.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
.img { | ||
height: auto; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/docusaurus-theme-classic/src/theme/MDXComponents/Img.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import type {Props} from '@theme/MDXComponents/Img'; | ||
import styles from './Img.module.css'; | ||
import clsx from 'clsx'; | ||
|
||
function transformImgClassName(className?: string): string { | ||
return clsx(className, styles.img); | ||
} | ||
|
||
export default function MDXImg(props: Props): JSX.Element { | ||
// eslint-disable-next-line jsx-a11y/alt-text | ||
return <img {...props} className={transformImgClassName(props.className)} />; | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/docusaurus-theme-classic/src/theme/MDXComponents/Pre.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {isValidElement} from 'react'; | ||
import CodeBlock from '@theme/CodeBlock'; | ||
|
||
export default function MDXPre(props: any) { | ||
return ( | ||
<CodeBlock | ||
// If this pre is created by a ``` fenced codeblock, unwrap the children | ||
{...(isValidElement(props.children) && | ||
props.children.props.originalType === 'code' | ||
? props.children?.props | ||
: {...props})} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/docusaurus-theme-classic/src/theme/MDXComponents/Ul.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
import type {Props} from '@theme/MDXComponents/Ul'; | ||
|
||
import styles from './Ul.module.css'; | ||
|
||
const containsClassListLocalClass = styles['contains-task-list']; | ||
|
||
function transformUlClassName(className?: string): string { | ||
return clsx( | ||
className, | ||
// This class is set globally by GitHub/MDX | ||
// We keep the global class, but apply scoped CSS | ||
// See https://github.com/syntax-tree/mdast-util-to-hast/issues/28 | ||
className?.includes('contains-task-list') && containsClassListLocalClass, | ||
); | ||
} | ||
|
||
export default function MDXUl(props: Props): JSX.Element { | ||
return <ul {...props} className={transformUlClassName(props.className)} />; | ||
} |
Oops, something went wrong.