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 via route.excludeConvention.

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';
import Button from '../../components/button';

<ButtonFragment />
<Button />

It will be rendered as:

button

This is some text

Escape Hatch: writing document content using tsx or html

_escape-hatch.tsx
export default () => {
  return (
    <p className="rp-doc">
      This is content in tsx, but the styles are the same as in the
      documentation, such as <code>@rspress/core</code>. However, this text with
      className="rp-not-doc"
      <code className="rp-not-doc">@rspress/core</code> will not take effect
    </p>
  );
};

It will be rendered as:

This is content in tsx, but the styles are the same as in the documentation, such as @rspress/core. However, this text with className="rp-not-doc"@rspress/core will not take effect

WARNING

tsx and html syntax can make it difficult to extract 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.