Skip to content

Commit

Permalink
After review, deleted swizzled components and reswizzled MDXComponent…
Browse files Browse the repository at this point in the history
…s with current Docusaurus version (v3.1.0) as context. The pre.js file is now updated to the proper structure and codeblocks display correctly by default.

Signed-off-by: Sunil Singh <[email protected]>
  • Loading branch information
sunilarjun committed Apr 29, 2024
1 parent 6ba468b commit 060b7ac
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 58 deletions.
54 changes: 16 additions & 38 deletions src/theme/MDXComponents/Code.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
import React, {isValidElement} from 'react';
import React from 'react';
import CodeBlock from '@theme/CodeBlock';
import CodeInline from '@theme/CodeInline';
function shouldBeInline(props) {
return (
// empty code blocks have no props.children,
// see https://github.com/facebook/docusaurus/pull/9704
typeof props.children !== 'undefined' &&
React.Children.toArray(props.children).every(
(el) => typeof el === 'string' && !el.includes('\n'),
)
);
}
export default function MDXCode(props) {
const inlineElements = [
'a',
'abbr',
'b',
'br',
'button',
'cite',
'code',
'del',
'dfn',
'em',
'i',
'img',
'input',
'ins',
'kbd',
'label',
'object',
'output',
'q',
'ruby',
's',
'small',
'span',
'strong',
'sub',
'sup',
'time',
'u',
'var',
'wbr',
];
const shouldBeInline = React.Children.toArray(props.children).every(
(el) =>
(typeof el === 'string' && !el.includes('\n')) ||
(isValidElement(el) && inlineElements.includes(el.props?.mdxType)),
return shouldBeInline(props) ? (
<CodeInline {...props} />
) : (
<CodeBlock {...props} />
);
return shouldBeInline ? <code {...props} /> : <CodeBlock {...props} />;
}
5 changes: 2 additions & 3 deletions src/theme/MDXComponents/Details.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export default function MDXDetails(props) {
// Split summary item from the rest to pass it as a separate prop to the
// Details theme component
const summary = items.find(
(item) => React.isValidElement(item) && item.props?.mdxType === 'summary',

);
(item) => React.isValidElement(item) && item.type === 'summary',
);
const children = <>{items.filter((item) => item !== summary)}</>;
return (
<Details {...props} summary={summary}>
Expand Down
17 changes: 0 additions & 17 deletions src/theme/MDXComponents/Head.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/theme/MDXComponents/Img/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import clsx from 'clsx';
import styles from './styles.module.css';
function transformImgClassName(className) {
return clsx(className, styles.img);
}
export default function MDXImg(props) {
return (
// eslint-disable-next-line jsx-a11y/alt-text
<img
loading="lazy"
{...props}
className={transformImgClassName(props.className)}
/>
);
}
3 changes: 3 additions & 0 deletions src/theme/MDXComponents/Img/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.img {
height: auto;
}
6 changes: 6 additions & 0 deletions src/theme/MDXComponents/Pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
export default function MDXPre(props) {
// With MDX 2, this element is only used for fenced code blocks
// It always receives a MDXComponents/Code as children
return <>{props.children}</>;
}
4 changes: 4 additions & 0 deletions src/theme/MDXComponents/Ul/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import React from 'react';
import clsx from 'clsx';
import styles from './styles.module.css';
function transformUlClassName(className) {
// Fix https://github.com/facebook/docusaurus/issues/9098
if (typeof className === 'undefined') {
return undefined;
}
return clsx(
className,
// This class is set globally by GitHub/MDX. We keep the global class, and
Expand Down

0 comments on commit 060b7ac

Please sign in to comment.