Provide a real-time playground to preview the code blocks in MDX files.
First, write the following config in the config file:
This plugin will set markdown.mdxRs
to false
. In the future, the Rspress team will port this plugin to the Rust version of the compiler.
It is recommended to use js or jsx to write sample code to avoid ts type errors in the editor.
The component code of internal components is declared in the mdx file. This plugin supports jsx and tsx. You can declare the following code block in the mdx file:
Otherwise, You can use direction
parameter to specify the playground layout, horizontal
and vertical
are supported:
It's worth noting that you need to export the component as default, and Rspress will automatically render this component.
But if you want to keep the style of the code block instead of rendering it as a component, you can add the pure
identifier to specify, the usage is as follows:
If you have set defaultRenderMode
to 'pure'
, Rspress will not render this component by default, but instead display it as a code block. If you want to render a code block as a component in this situation, you can do add the preview
identifier to specify, the usage is as follows:
Make sure the document ends with .mdx
.
In addition to writing the component code in the code block of the mdx file, you can also write the component code in an external file, and then import it in the mdx file through the code tag. For example
Same as internal components, external components also support direction
attribute:
External components also need to export the component as default. Through the src attribute of the code tag, you can specify the path of the external component. This plugin supports both relative paths and alias paths(alias).
For some more complex components, this way of using external components will be more convenient.
You can write playgroundDirection
in frontmatter to define the layout of the editor and preview area in the entire page.
Priority: Defined directly on the preview area > Page Definition > Configuration.
This plugin accepts an object parameter, the type is as follows:
monacoLoader
and monacoOptions
will be serialized to JSON, so some data types, such as functions and circularly referenced objects, are not supported.
You can customize the render file for rendering Playground. Please note that the file name must be Playground.(jsx?|tsx?)
In the custom playground, you can directly import the original editor and renderer, and import pre-packaged dependencies through _rspress_playground_imports
:
You can refer to the built-in Playground.tsx.
By default, this plugin will automatically scan all import statements in the demos, the packages not used in the demos cannot be used in the playground. If you want to add other packages to the playground, you can use the include
parameter:
Playground uses @babel/standalone
to compile the demo code, loaded from cdnjs.com by default.
You can modify the URL served to other CDNs, such as unpkg, jsdelivr, etc.
Configure monaco-loader behaviors, loaded from cdnjs.com by default.
You can modify the URL served to other CDNs, such as unpkg, jsdelivr, etc.
The full document can be found at suren-atoyan/monaco-loader
Configure monaco editor, corresponding to IStandaloneEditorConstructionOptions
The default mode of rendering internal code blocks that do not have the pure
or playground
identifiers declared by users, defaulting to playground
.
pure
: render as regular code blockplayground
: render as Playground component