Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme-classic): MDXContent wrapper component #6842

Merged
merged 3 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/docusaurus-theme-classic/src/getSwizzleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export default function getSwizzleConfig(): SwizzleConfig {
description:
'The MDX components to use for rendering MDX files. Meant to be ejected.',
},
MDXContent: {
actions: {
eject: 'safe',
wrap: 'safe',
},
description:
'A component wrapping all MDX content and providing the MDXComponents to the MDX context',
},
// TODO should probably not even appear here
'NavbarItem/utils': {
actions: {
Expand Down
10 changes: 10 additions & 0 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ declare module '@theme/MDXComponents' {
export default MDXComponents;
}

declare module '@theme/MDXContent' {
import type {ReactNode} from 'react';

export interface Props {
readonly children: ReactNode;
}

export default function MDXContent(props: Props): JSX.Element;
}

declare module '@theme/Navbar' {
export default function Navbar(): JSX.Element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

import React from 'react';
import clsx from 'clsx';
import {MDXProvider} from '@mdx-js/react';
import Translate, {translate} from '@docusaurus/Translate';
import Link from '@docusaurus/Link';
import {useBaseUrlUtils} from '@docusaurus/useBaseUrl';
import {usePluralForm} from '@docusaurus/theme-common';
import {blogPostContainerID} from '@docusaurus/utils-common';
import MDXComponents from '@theme/MDXComponents';
import MDXContent from '@theme/MDXContent';
import EditThisPage from '@theme/EditThisPage';
import type {Props} from '@theme/BlogPostItem';

Expand Down Expand Up @@ -108,7 +107,7 @@ export default function BlogPostItem(props: Props): JSX.Element {
id={isBlogPostPage ? blogPostContainerID : undefined}
className="markdown"
itemProp="articleBody">
<MDXProvider components={MDXComponents}>{children}</MDXProvider>
<MDXContent>{children}</MDXContent>
</div>

{(tagsExists || truncated) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Heading from '@theme/Heading';
import styles from './styles.module.css';
import {ThemeClassNames, useWindowSize} from '@docusaurus/theme-common';
import DocBreadcrumbs from '@theme/DocBreadcrumbs';
import MDXContent from '@theme/MDXContent';

export default function DocItem(props: Props): JSX.Element {
const {content: DocContent} = props;
Expand Down Expand Up @@ -87,8 +88,9 @@ export default function DocItem(props: Props): JSX.Element {
<Heading as="h1">{title}</Heading>
</header>
)}

<DocContent />
<MDXContent>
<DocContent />
</MDXContent>
</div>

<DocItemFooter {...props} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
*/

import React, {type ReactNode, useState, useCallback} from 'react';
import {MDXProvider} from '@mdx-js/react';

import renderRoutes from '@docusaurus/renderRoutes';
import type {PropVersionMetadata} from '@docusaurus/plugin-content-docs';
import Layout from '@theme/Layout';
import DocSidebar from '@theme/DocSidebar';
import MDXComponents from '@theme/MDXComponents';
import NotFound from '@theme/NotFound';
import type {DocumentRoute} from '@theme/DocItem';
import type {Props} from '@theme/DocPage';
Expand Down Expand Up @@ -137,7 +134,7 @@ function DocPageContent({
[styles.docItemWrapperEnhanced]: hiddenSidebarContainer,
},
)}>
<MDXProvider components={MDXComponents}>{children}</MDXProvider>
{children}
</div>
</main>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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 {MDXProvider} from '@mdx-js/react';
import MDXComponents from '@theme/MDXComponents';
import type {Props} from '@theme/MDXContent';

export default function MDXContent({children}: Props): JSX.Element {
return <MDXProvider components={MDXComponents}>{children}</MDXProvider>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import React from 'react';
import clsx from 'clsx';
import Layout from '@theme/Layout';
import {MDXProvider} from '@mdx-js/react';
import MDXComponents from '@theme/MDXComponents';
import MDXContent from '@theme/MDXContent';
import type {Props} from '@theme/MDXPage';
import TOC from '@theme/TOC';
import {ThemeClassNames} from '@docusaurus/theme-common';
Expand All @@ -34,9 +33,9 @@ export default function MDXPage(props: Props): JSX.Element {
<main className="container container--fluid margin-vert--lg">
<div className={clsx('row', styles.mdxPageWrapper)}>
<div className={clsx('col', !hideTableOfContents && 'col--8')}>
<MDXProvider components={MDXComponents}>
<MDXContent>
<MDXPageContent />
</MDXProvider>
</MDXContent>
</div>
{!hideTableOfContents && MDXPageContent.toc && (
<div className="col col--2">
Expand Down