fix(theme-classic): allow code tags containing inline elements to stay inline (#6767)

This commit is contained in:
Joshua Chen 2022-03-02 20:01:58 +08:00 committed by GitHub
parent 2e3eec2d08
commit c1fb3deace
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -37,8 +37,22 @@ const MDXComponents: MDXComponentsObject = {
return <Head {...props}>{unwrappedChildren}</Head>; return <Head {...props}>{unwrappedChildren}</Head>;
}, },
code: (props) => { code: (props) => {
const inlineElements = [
'a',
'b',
'big',
'i',
'span',
'em',
'strong',
'sup',
'sub',
'small',
];
const shouldBeInline = React.Children.toArray(props.children).every( const shouldBeInline = React.Children.toArray(props.children).every(
(el) => typeof el === 'string' && !el.includes('\n'), (el) =>
(typeof el === 'string' && !el.includes('\n')) ||
(React.isValidElement(el) && inlineElements.includes(el.props.mdxType)),
); );
return shouldBeInline ? <code {...props} /> : <CodeBlock {...props} />; return shouldBeInline ? <code {...props} /> : <CodeBlock {...props} />;