---
title: Frontend Demo
icon: icon-park-outline:html-five
createTime: 2025/01/08 21:34:26
permalink: /en/guide/repl/frontend/
---
::: important Legacy Frontend Code Demo Deprecated - Please Migrate to New Solution
The legacy solution was provided by [vuepress-plugin-md-enhance](https://plugin-md-enhance.vuejs.press/zh/).
We appreciate its support for code demos in the past. The code demo functionality in
`vuepress-plugin-md-enhance` will be migrated to [vuepress/ecosystem](https://github.com/vuepress/ecosystem). For details, please refer to [vuepress/ecosystem#293](https://github.com/vuepress/ecosystem/pull/293).
:::
::: details Why Redesign?
Frontend code demos are a highly useful feature, but the legacy implementation did not align with actual usage scenario expectations.
In the legacy solution, for example, `vue-demo` only supported simple Vue component demonstrations
and could not directly import dependencies from the project. It relied on loading external scripts for
additional functionality and did not provide full support for Vue SFC, being limited to simple code demonstrations.
Furthermore, script code compilation occurred in the browser runtime. It required loading `babel` from a
CDN first, then performing the transformation via `babel`, which introduced additional waiting time.
Additionally, for internal enterprise projects within an intranet environment that cannot access external
resources, the demos failed to display properly.
In the new solution, all demo code is compiled and transformed during the Node.js runtime.
Therefore, demo code can be displayed directly in the browser runtime without extra waiting time.
Leveraging Node.js's powerful capabilities, it fully supports all features of Vue SFC and allows direct
import of project dependencies, enabling richer demonstrations that better match real-world usage scenarios.
:::
## Overview
This feature supports embedding code demonstration functionality within pages. A frontend code demo consists of two main areas:
==Render Area== and ==Code Area==.
The **Render Area** displays the execution results of the code, including UI rendering and interactions.
The **Code Area** displays the source code and is collapsed by default.
The theme provides support for three different types of frontend code demos:
- ==Vue Component Demo==: Supports demonstrations of `Vue` components.
Write your demo code as you would a `Vue` component.
It can be used to demonstrate external dependencies like component libraries or `Composables API`.
- ==Markdown Demo==: Supports demonstrations of `Markdown`.
- ==Normal Code Demo==: Supports native `HTML` + `JS/TS` + `CSS/Less/Sass/Stylus` code demos. Write your demo code as you would a web page.
The theme also provides two different methods for writing demo code:
- Embed Demo Code Files:
```md
@[demo type](url)
```
Use simple embedding syntax to import demo code from files.
- Inline Demo Code within Demo Container:
````md
::: demo type
``` [lang]
code
```
:::
````
Write demo code directly within the markdown file, wrapped in a `demo` container.
## Configuration
Frontend code demos are powered by [vuepress-plugin-md-power](../../config/plugins/markdown-power.md).
Frontend code demos are disabled by default. You can enable them via configuration.
```ts title=".vuepress/config.ts"
export default defineUserConfig({
theme: plumeTheme({
markdown: {
demo: true, // [!code ++]
},
})
})
```
## Language Support
Code demos support the following languages:
- javascript
- typescript
- html
- css
- less
- sass
- stylus
For CSS preprocessor languages, you need to install the corresponding preprocessor in your project, such as `less`, `sass`, or `stylus`.
## Embed Syntax
Different code demos use the same embed syntax, allowing you to quickly grasp their usage.
```md
@[demo](url)
@[demo [type]](url)
@[demo [type] title="" desc="" expanded code-setting=""](url)
```
`@[demo](url)` is a fixed syntax format.
`[type]` indicates the type and supports three different values:
- `normal`: Normal code demo type. This is the default type when the `[type]` parameter is omitted.
- `vue`: Vue component demo type.
- `markdown`: Markdown demo type.
`url` indicates the path to the demo code file, which can be relative or absolute.
- Relative paths start with `./` or `../` and are relative to the current markdown file's path.
- Absolute paths start with `/` and are resolved from the [VuePress source directory path](../quick-start/project-structure.md#document-source-directory).
```md
@[demo](./demo/normal.html)
@[demo normal](./demo/normal.html)
@[demo](/.vuepress/demo/normal.html)
@[demo vue](./demo/Counter.vue)
@[demo vue](./demo/Counter.ts)
@[demo](/.vuepress/demo/Counter.vue)
@[demo markdown](./demo/example.md)
@[demo markdown](/.vuepress/demo/example.md)
```
Additional parameters:
- `title="xxx"`: Demo title.
- `desc="xxx"`: Demo description.
- `expanded`: Expand the code area.
- `code-setting="xxx"`: Code settings. The value will be appended after ``` [lang]` and is used to add configurations to the code block.
`code-setting=":lines-number"` will add `:lines-number` after the code block, enabling line numbers.
`code-setting=":collapsed-lines=10"` will add `:collapsed-lines=10` after the code block,
causing the code block to be collapsed starting from line 10.
```md
@[demo vue expanded title="Title" desc="Description" code-setting=":collapsed-lines=10"](./demo/Counter.vue)
```
## Demo Container Inline Demo
Demo container inline demos use the `demo` container to wrap the demo code, allowing quick writing of demo code within the markdown file:
```md
::: demo [type] title="" desc="" expanded
:::
```
All parameters are the same as the `@[demo](url)` syntax.
````md
::: demo
```html
```
``` js
// js code
```
``` css
/* css code */
```
:::
::: demo vue
``` vue
```
:::
::: demo markdown
``` md
```
:::
````
You can also wrap code blocks with the `::: code-tabs` container within the `::: demo` container for better interactive presentation.
````md
:::: demo
::: code-tabs
@tab HTML
```html
```
@tab javascript
``` js
// js code
```
@tab css
``` css
/* css code */
```
:::
:::::
````
When using TypeScript or `Less/Sass/Stylus`, simply modify the value of ` ``` [lang]`:
````md
:::: demo
::: code-tabs
@tab HTML
```html
```
@tab Typescript
``` ts
// ts code
```
@tab Scss
``` scss
/* scss code */
```
:::
:::::
````
## Vue Component Demo
Vue component demo is a powerful feature with no restrictions on the demo codeāit ultimately depends on the `bundler`'s support for Vue.
You can directly import dependencies installed in your project within the demo code, just as you would when writing a component in a Vue project.
Therefore, you can use it directly to provide demonstration examples for your component library or for your `Composables API`.
### Embed Syntax
You can directly embed a Vue component demo in a page using the following method:
**Input:**
```md
@[demo vue title="Counter" desc="Click the +1 button to increment the counter by 1"](./demo/Counter.vue)
```
::: details View `./demo/Counter.vue` Code
@[code](./demo/Counter.vue)
:::
**Output:**
@[demo vue title="Counter" desc="Click the +1 button to increment the counter by 1"](./demo/Counter.vue)
---
You can also embed a Vue component written in `.ts`:
**Input:**
```md
@[demo vue title="Counter" desc="Click the +1 button to increment the counter by 1"](./demo/Counter.ts)
```
::: details View `./demo/Counter.ts` Code
::: code-tabs
@tab Counter.ts
@[code](./demo/Counter.ts)
@tab Counter.module.css
@[code](./demo/Counter.module.css)
:::
**Output:**
@[demo vue title="Counter" desc="Click the +1 button to increment the counter by 1"](./demo/Counter.ts)
:::info For components written in `.js/.ts`, use `CSS Module` to write styles for style isolation.
:::
---
You can import external dependencies in the demo code.
Example using `useToggle()` from `@vueuse/core`:
**Input:**
```md
@[demo vue title="useToggle" desc="useToggle() Demo"](./demo/Toggle.vue)
```
::: details ./demo/Toggle.vue
@[code](./demo/Toggle.vue)
:::
**Output:**
@[demo vue title="useToggle" desc="useToggle() Demo"](./demo/Toggle.vue)
### Container Syntax
Using the `demo` container to wrap demo code in a markdown file allows for quick demo code writing:
**Input:**
:::: details Expand to view full code
````md
::: demo vue title="Counter" desc="Click the +1 button to increment the counter by 1"
```vue
Counter: {{ count }} Counter: {{ count }}