Code Demo
Less than 1 minute
Let you insert code demos in your Markdown file.
Settings
import { hopeTheme } from "vuepress-theme-hope";
export default hopeTheme({
markdown: {
demo: true,
},
});
Syntax
You should use the following syntax:
::: [type]-demo Optional title text
```html
<!-- ↑ use available ones -->
<!-- your code here -->
<!-- you can have multiple code block, but each language must appear only once. -->
```
```json
// json block is for config
{
// your config here (optional)
}
```
:::
Tips
The json block is optional, for config please see config.
The plugin support three types:
- normal
- vue
- react
Normal
::: normal-demo Optional title text
```html
<!-- html code -->
```
```js
// js code
```
```css
/* css code */
```
```json
// config (optional)
{
"jsLib": [
...
],
"cssLib":[
...
]
}
```
:::
Vue
::: vue-demo Optional title text
```vue
<!-- ↑ You can also use `html` -->
<template>
<!-- vue template -->
</template>
<script>
export default {
// vue component
};
</script>
<style>
/* css code */
</style>
```
```json
// Config (Optional)
```
:::
React
::: react-demo Optional title text
```js
// your script, exporting your react component through `export default`
```
```css
/* Your css content */
```
```json
// Config (Optional)
```
:::
Available Languages
You can use different language in your demo block.
When you set language which can not run on browsers, since the plugin is not able to resolve them, so demo display will be disabled. The plugin will only show the code and provide you a button to open it in CodePen.
Available HTML languages:
"html"
(default)"slim"
"haml"
"markdown"
You can also use
md
in code block.
Available JS languages:
"javascript"
(default)"coffeescript"
"babel"
"livescript"
"typescript"
You can also use
js
,ts
,coffee
andls
in code block.
Available CSS languages:
"css"
(default)"less"
"scss"
"sass"
"stylus"
You can also use
styl
in code block.
Demo
Demo
<h1>VuePress Theme Hope</h1>
<p>is <span id="very">very</span> powerful!</p>
document.querySelector("#very").addEventListener("click", () => {
alert("Very powerful");
});
span {
color: red;
}
Normal demo
::: normal-demo Demo
```html
<h1>VuePress Theme Hope</h1>
<p>is <span id="very">very</span> powerful!</p>
```
```js
document.querySelector("#very").addEventListener("click", () => {
alert("Very powerful");
});
```
```css
span {
color: red;
}
```
:::
Vue demo 1
<template>
<div class="box">
<code>vuepress-theme-hope</code> is
<span @click="handler">{{ message }}</span
>!
</div>
</template>
<script>
const { ref } = Vue;
export default {
setup() {
const message = ref("powerful");
const handler = () => {
message.value = "very " + message.value;
};
return {
message,
handler,
};
},
};
</script>
<style>
.box span {
color: red;
}
</style>
A Vue Composition Demo
::: vue-demo Vue demo 1
```vue
<template>
<div class="box">
<code>vuepress-theme-hope</code> is
<span @click="handler">{{ message }}</span
>!
</div>
</template>
<script>
const { ref } = Vue;
export default {
setup() {
const message = ref("powerful");
const handler = () => {
message.value = "very " + message.value;
};
return {
message,
handler,
};
},
};
</script>
<style>
.box span {
color: red;
}
</style>
```
:::
Vue demo 2
<template>
<div class="box">
<code>vuepress-theme-hope</code> is
<span @click="handler">{{ message }}</span
>!
</div>
</template>
<script>
export default {
data: () => ({ message: "powerful" }),
methods: {
handler() {
this.message = "very " + this.message;
},
},
};
</script>
<style>
.box span {
color: red;
}
</style>
A Vue Option Demo
::: vue-demo Vue demo 2
```vue
<template>
<div class="box">
<code>vuepress-theme-hope</code> is
<span @click="handler">{{ message }}</span
>!
</div>
</template>
<script>
export default {
data: () => ({ message: "powerful" }),
methods: {
handler() {
this.message = "very " + this.message;
},
},
};
</script>
<style>
.box span {
color: red;
}
</style>
```
:::
React demo 1
const { useState } = React;
export default () => {
const [message, setMessage] = useState(" powerful");
const handler = () => {
setMessage(` very${message}`);
};
return (
<div className="box">
<code>vuepress-theme-hope</code> is
<span id="powerful" onClick={handler}>
{message}
</span>
!
</div>
);
};
.box #powerful {
color: blue;
}
A function-based React Demo
::: react-demo React demo 1
```js
const { useState } = React;
export default () => {
const [message, setMessage] = useState(" powerful");
const handler = () => {
setMessage(` very${message}`);
};
return (
<div className="box">
<code>vuepress-theme-hope</code> is
<span id="powerful" onClick={handler}>
{message}
</span>
!
</div>
);
};
```
```css
.box #powerful {
color: blue;
}
```
:::
React demo 2
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { message: " powerful" };
}
handler() {
this.setState((state) => ({
message: ` very${state.message}`,
}));
}
render() {
return (
<div className="box">
<code>vuepress-theme-hope</code> is
<span id="powerful" onClick={this.handler.bind(this)}>
{this.state.message}!
</span>
</div>
);
}
}
.box #powerful {
color: blue;
}
A class-based React Demo
::: react-demo React demo 2
```js
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { message: " powerful" };
}
handler() {
this.setState((state) => ({
message: ` very${state.message}`,
}));
}
render() {
return (
<div className="box">
<code>vuepress-theme-hope</code> is
<span id="powerful" onClick={this.handler.bind(this)}>
{this.state.message}!
</span>
</div>
);
}
}
```
```css
.box #powerful {
color: blue;
}
```
:::
Normal demo
# Title
is very powerful!
const message: string = "VuePress Theme Hope";
document.querySelector("h1").innerHTML = message;
h1 {
font-style: italic;
+ p {
color: red;
}
}
A demo using language not supported by browsers
::: normal-demo Normal demo
```md
# Title
is very powerful!
```
```ts
const message: string = "VuePress Theme Hope";
document.querySelector("h1").innerHTML = message;
```
```scss
h1 {
font-style: italic;
+ p {
color: red;
}
}
```
:::
Changelog
6/4/25, 1:34 PM
View All Changelog
1bd5f
-on55e92
-onb1230
-on1b917
-on22787
-on08f11
-on4894d
-onb41c0
-on671db
-on54c46
-on55ea3
-on792dc
-on757fd
-on5b1a7
-on83bff
-on83b85
-on86180
-on71423
-on8174c
-on4104e
-on90e9c
-ona5112
-on8b5af
-on2fa50
-on3c8d6
-onf1dae
-on9856d
-on23515
-on48bca
-on9cdd7
-on63d09
-onad023
-on1ca3e
-onf635d
-onca966
-on88e69
-on5a601
-onfd395
-on1eb77
-on60053
-on4d1ba
-on4a252
-on92d8a
-on84de2
-onf6ff0
-on95e4f
-on3c199
-ond7584
-onace60
-on688d8
-on4b8e5
-on