You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is a best practice file structure that includes [MDX fragments and React components](/guide/use-mdx/components#component), [custom themes](/guide/basic/custom-theme), [conventional routing](/guide/basic/conventional-route), and [internationalization](/guide/basic/i18n):
2
+
3
+
```tree
4
+
├── docs
5
+
│ ├── components # React components used in documentation
6
+
│ │ └── Example.tsx
7
+
│ ├── zh
8
+
│ │ ├── fragments # zh document fragments
9
+
│ │ │ └── example.mdx
10
+
│ │ └── index.mdx
11
+
│ └── en
12
+
│ ├── fragments # en document fragments
13
+
│ │ └── example.mdx
14
+
│ └── index.mdx
15
+
└── theme
16
+
├── components # React components for theme
17
+
│ └── DocFooter.tsx
18
+
└── index.tsx
19
+
```
20
+
21
+
```ts title="rspress.config.ts"
22
+
import { defineConfig } from'@rspress/core';
23
+
24
+
exportdefaultdefineConfig({
25
+
route: {
26
+
exclude: ['*/components/**/*', '*/fragments/**/*'], // Files in these directories won't be registered as routes
In the [docs directory](/api/config/config-basic#root), MDX fragments or React components need to be excluded from routing through the [route.exclude](/api/config/config-basic#routeexclude) configuration. For convenience, files starting with "\_" are excluded by default via [route.excludeConvention](/api/config/config-basic#routeexcludeconvention).
5
+
6
+
You can also place components in adjacent directories outside the docs directory, for example:
7
+
8
+
```tree
9
+
docs
10
+
├── _button.mdx
11
+
└── index.mdx
12
+
components
13
+
└── button.tsx
14
+
```
15
+
16
+
<Tabs>
17
+
<Tablabel="docs/index.mdx">
18
+
19
+
```mdx
20
+
importButtonFragmentfrom'./_button.mdx';
21
+
importButtonfrom'../../components/button';
22
+
23
+
<ButtonFragment />
24
+
<Button />
25
+
```
26
+
27
+
</Tab>
28
+
<Tablabel="docs/_button.mdx">
29
+
30
+
```mdx file="./_button.mdx"
31
+
32
+
```
33
+
34
+
</Tab>
35
+
<Tablabel="components/button.tsx">
36
+
37
+
```tsx
38
+
const Button = () => <button>This is a button from tsx</button>;
Do not have files and folders with the same name, which will cause routing conflicts. For example, the following file structure is not allowed:
36
+
37
+
```tree
38
+
docs
39
+
├── foo
40
+
│ └── index.md
41
+
└── foo.md
42
+
```
43
+
44
+
:::
30
45
31
46
## Component routing
32
47
@@ -59,24 +74,63 @@ import { defineConfig } from '@rspress/core';
59
74
60
75
exportdefaultdefineConfig({
61
76
route: {
62
-
// These files will be excluded from the routing (support glob pattern)
63
-
exclude: ['component/**/*'],
64
-
// These files will be included in the routing (support glob pattern)
77
+
// These files will be registered as routes (supports glob pattern)
65
78
include: ['other-dir/**/*'],
79
+
// These files will not be registered as routes (supports glob pattern)
80
+
exclude: ['components/**/*', 'fragments/**/*'],
66
81
},
67
82
});
68
83
```
69
84
70
85
## Best practices
71
86
72
-
We recommend that you place documentation files in the `docs` directory to make your project more clear. For non-documentation content, such as custom components, util functions, etc., they can be maintained outside the `docs` directory. For example:
87
+
We recommend that you place documentation files in the `docs` directory to make your project more clear. For non-documentation content, such as custom components, util functions, etc., they can be maintained outside the `docs` directory.
88
+
89
+
:::tip
90
+
91
+
If you want to place `.tsx` or `.mdx` files in the `docs` directory, you need to use `route.exclude` together, otherwise these files will be automatically registered as routes, which may cause some unexpected issues.
92
+
93
+
:::
94
+
95
+
Here is a best practice that uses `route.exclude` to make your project structure clearer:
73
96
74
97
```tree
75
-
docs
76
-
└── foo.mdx
77
-
src
78
-
├── components
79
-
│ └── CustomComponent.tsx
80
-
└── utils
81
-
└── utils.ts
98
+
├── docs
99
+
│ ├── components # For React components used in documentation
100
+
│ │ └── Example.tsx
101
+
│ ├── fragments # For document fragments
102
+
│ │ └── example.mdx
103
+
│ └── index.mdx
104
+
└── theme
105
+
├── components # For theme-related React components
106
+
| └── DocFooter.tsx
107
+
└── index.tsx
108
+
```
109
+
110
+
```ts title="rspress.config.ts"
111
+
import { defineConfig } from'@rspress/core';
112
+
113
+
exportdefaultdefineConfig({
114
+
route: {
115
+
exclude: ['components/**/*', 'fragments/**/*'], // Files in these directories will not be registered as routes
Copy file name to clipboardExpand all lines: website/docs/en/guide/use-mdx/components.mdx
+7-47Lines changed: 7 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,20 +2,20 @@
2
2
3
3
Rspress supports [MDX](https://mdxjs.com/), a powerful way to develop content.
4
4
5
-
## Markdown
5
+
## MDX
6
6
7
7
MDX is a superset of Markdown, which means you can write Markdown files as usual. For example:
8
8
9
-
```md
9
+
```mdx
10
10
# Hello world
11
11
```
12
12
13
-
## Componentization
13
+
## Componentization\{#component}
14
14
15
15
In MDX, every `.mdx` file is compiled into a React component, which means it can be imported like any component and can freely render React components. For example:
@@ -68,51 +68,11 @@ You can use [built-in components](/ui/components/index.mdx) provided by Rspress
68
68
69
69
## Routing convention
70
70
71
-
In the [docs directory](/api/config/config-basic#root), MDX fragments or React components need to be excluded from routing through the [route.exclude](/api/config/config-basic#routeexclude) configuration. For convenience, files starting with "\_" are excluded by default via [route.excludeConvention](/api/config/config-basic#routeexcludeconvention).
72
-
73
-
You can also place components in adjacent directories outside the docs directory, for example:
0 commit comments