Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mdx-loader): properly unwrap mdxAdmonitionTitle placeholder (#8246)
# Conflicts: # packages/docusaurus-theme-common/src/utils/admonitionUtils.tsx
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
packages/docusaurus-theme-common/src/utils/admonitionUtils.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,43 @@ | ||
/** | ||
* 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 ReactNode} from 'react'; | ||
|
||
// Workaround because it's difficult in MDX v1 to provide a MDX title as props | ||
// See https://github.com/facebook/docusaurus/pull/7152#issuecomment-1145779682 | ||
function extractMDXAdmonitionTitle(children: ReactNode): { | ||
mdxAdmonitionTitle: ReactNode | undefined; | ||
rest: ReactNode; | ||
} { | ||
const items = React.Children.toArray(children); | ||
const mdxAdmonitionTitle = items.find( | ||
(item) => | ||
React.isValidElement(item) && | ||
(item.props as {mdxType: string} | null)?.mdxType === | ||
'mdxAdmonitionTitle', | ||
) as JSX.Element | undefined; | ||
const rest = <>{items.filter((item) => item !== mdxAdmonitionTitle)}</>; | ||
return { | ||
mdxAdmonitionTitle: mdxAdmonitionTitle?.props.children, | ||
rest, | ||
}; | ||
} | ||
|
||
export function processAdmonitionProps< | ||
Props extends {readonly children: ReactNode; readonly title?: ReactNode}, | ||
>(props: Props): Props { | ||
const {mdxAdmonitionTitle, rest} = extractMDXAdmonitionTitle(props.children); | ||
const title = props.title ?? mdxAdmonitionTitle; | ||
return { | ||
...props, | ||
// Do not return "title: undefined" prop | ||
// this might create unwanted props overrides when merging props | ||
// For example: {...default,...props} | ||
...(title && {title}), | ||
children: rest, | ||
}; | ||
} |