MDX and React components

Componentization

In MDX, all .mdx files are compiled into React components, which means they can be used as React components and can freely use React components. For example:

docs/index.mdx
docs/_mdx-fragment.mdx
docs/_tsx-component.tsx
import MdxFragment from './_mdx-fragment.mdx';
import TsxComponent from './_tsx-component';

Testing the use of MDX fragments and React components.

<MdxFragment />

<TsxComponent />

It will be rendered as:

Testing the use of MDX fragments and React components.

This is mdx fragment.

This is a component from tsx

You can use built-in components provided by Rspress or install some React component libraries to enrich your documentation in .mdx files.

Routing convention

In the docs directory, MDX fragments or React components need to be excluded from routing through the route.exclude configuration. For convenience, we agree that files starting with "_" will be excluded by default.

You can also place components in adjacent directories outside the docs directory, for example:

docsite/docs/index.mdx
docsite/docs/_button.mdx
docsite/components/button.tsx
import ButtonFragment from './_button.mdx';

<ButtonFragment />
<Button />

It will be rendered as:

button

This is some text

Escape Hatch: using MDX styles in TSX components

_escape-hatch.tsx
import { getCustomMDXComponent } from '@theme';

export default () => {
  const { code: Code, p: P } = getCustomMDXComponent();
  return (
    <P>
      This is content in tsx, but you can use <Code>code block</Code> styles.
    </P>
  );
};

It will be rendered as:

This is content in tsx, but you can use code block styles.

WARNING

.tsx components can make it difficult to extract some static information, such as local search indexes.

It is more recommended to use .mdx files to write document content and use .tsx files to write interactive dynamic content.