Provide a real-time playground to preview the code blocks in Markdown/MDX files.
npm add @rspress/plugin-playground -D
First, write the following config in the config file:
import { defineConfig } from 'rspress/config';
import { pluginPlayground } from '@rspress/plugin-playground';
export default defineConfig({
plugins: [pluginPlayground()],
});
markdown.experimentalMdxRs
config, otherwise it will cause the plugin to fail.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:
```jsx
import React from 'react';
function App() {
return Hello World;
}
export default App;
```
Otherwise, You can use direction
parameter to specify the playground layout, horizontal
and vertical
are supported:
```jsx direction=vertical
import React from 'react';
function App() {
return Hello World;
}
export default App;
```
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:
```tsx pure
import React from 'react';
function App() {
return Hello World;
}
export default App;
```
Make sure the document ends with .mdx
.
In this way, the code block will not be rendered as a component, but will be kept as it is.
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
<code src="./Demo.jsx" />
import React from 'react';
export default function App() {
return <div>Hello World</div>;
}
Same as internal components, external components also support direction
attribute:
<code src="./Demo.jsx" direction="vertical" />
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.
This plugin accepts an object parameter, the type is as follows:
interface PlaygroundOptions {
render: string;
include: Array<string | [string, string]>;
// Define the default playground layout (horizontal/vertical)
defaultDirection: 'horizontal' | 'vertical';
// Define the position of the editor in horizontal layout (left/right)
editorPosition: 'left' | 'right';
// Default editor (monaco) partial configuration
monacoLoader: Parameters<typeof loader.config>[0];
monacoOptions: MonacoEditorProps['options'];
}
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?)
pluginPlayground({
render: '/path/to/render/Playground.tsx',
});
In the custom playground, you can directly import the original editor and renderer, and import pre-packaged dependencies through _rspress_playground_imports
:
import getImport from '_rspress_playground_imports';
import { Render, Editor } from '@rspress/plugin-playground/web';
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:
pluginPlayground({
include: [
// Add dayjs package
'dayjs',
// Add a package named "my-package", and it actual reference to "/path/to/package/index.js"
['my-package', '/path/to/package/index.js'],
],
});
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