Skip to content

Commit

Permalink
feat(plugin-md-power): 支持可编辑的代码演示
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed May 4, 2024
1 parent 2127ca4 commit 5addb31
Show file tree
Hide file tree
Showing 17 changed files with 570 additions and 69 deletions.
6 changes: 5 additions & 1 deletion docs/.vuepress/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ export const theme: Theme = themePlume({
replit: true,
codeSandbox: true,
jsfiddle: true,
repl: true,
repl: {
go: true,
rust: true,
kotlin: true,
},
},
comment: {
provider: 'Giscus',
Expand Down
93 changes: 89 additions & 4 deletions docs/notes/theme/guide/代码演示/golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ permalink: /guide/repl/golang/
主题提供了 Golang 代码演示,支持 在线运行 Go 代码。

::: important
该功能通过将 代码提交到 服务器 进行 编译并执行,且一次只能提交单个代码文件
该功能通过将 代码提交到 服务器 进行 编译并执行。

因此,请不要使用此功能 执行 过于复杂的代码,也不要过于频繁的进行执行请求。
:::
Expand All @@ -28,7 +28,9 @@ export default defineUserConfig({
theme: plumeTheme({
plugins: {
markdownPower: {
repl: true,
repl: {
go: true,
},
},
}
})
Expand All @@ -41,10 +43,26 @@ export default defineUserConfig({

使用 `::: go-repl` 容器语法 将 Go 代码块包裹起来。主题会检查代码块并添加执行按钮。

### 只读代码演示

golang 代码演示默认是只读的,不可编辑。

````md
::: go-repl
::: go-repl 自定义标题
```go
// your go code
```
:::
````

### 可编辑代码演示

如果需要在线编辑并执行,需要将代码块包裹在 `::: go-repl#editable` 容器语法中

````md
::: go-repl#editable 自定义标题
```go
// your rust code
// your go code
```
:::
````
Expand Down Expand Up @@ -88,6 +106,44 @@ func main() {

:::

### 可编辑代码演示

**输入:**

````md
:::go-repl#editable
```go
package main

import (
"fmt"
)

func main() {
fmt.Println("Hello World")
}
```
:::
````

**输出:**

:::go-repl#editable

```go
package main

import (
"fmt"
)

func main() {
fmt.Println("Hello World")
}
```

:::

### 循环随机延迟打印

**输入:**
Expand Down Expand Up @@ -186,3 +242,32 @@ func main() {
```

:::

### 多文件

::: go-repl

```go{10-12}
package main
import (
"play.ground/foo"
)
func main() {
foo.Bar()
}
-- go.mod --
module play.ground
-- foo/foo.go --
package foo
import "fmt"
func Bar() {
fmt.Println("This function lives in an another file!")
}
```

:::
56 changes: 53 additions & 3 deletions docs/notes/theme/guide/代码演示/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default defineUserConfig({
theme: plumeTheme({
plugins: {
markdownPower: {
repl: true,
repl: {
kotlin: true,
},
},
}
})
Expand All @@ -39,10 +41,26 @@ export default defineUserConfig({

## 使用

使用 `::: kotlin-repl` 容器语法 将 Rust 代码块包裹起来。主题会检查代码块并添加执行按钮。
使用 `::: kotlin-repl` 容器语法 将 kotlin 代码块包裹起来。主题会检查代码块并添加执行按钮。

### 只读代码演示

kotlin 代码演示默认是只读的,不可编辑。

````md
::: kotlin-repl
::: kotlin-repl 自定义标题
```kotlin
// your kotlin code
```
:::
````

### 可编辑代码演示

如果需要在线编辑并执行,需要将代码块包裹在 `::: kotlin-repl#editable` 容器语法中

````md
::: kotlin-repl#editable 自定义标题
```kotlin
// your kotlin code
```
Expand Down Expand Up @@ -98,3 +116,35 @@ fun main(args: Array<String>) {
```

:::

### 可编辑代码演示

**输入:**

````md
::: kotlin-repl#editable
```kotlin
class Contact(val id: Int, var email: String)

fun main(args: Array<String>) {
val contact = Contact(1, "mary@gmail.com")
println(contact.id)
}
```
:::
````

**输出:**

::: kotlin-repl#editable

```kotlin
class Contact(val id: Int, var email: String)

fun main(args: Array<String>) {
val contact = Contact(1, "mary@gmail.com")
println(contact.id)
}
```

:::
71 changes: 67 additions & 4 deletions docs/notes/theme/guide/代码演示/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default defineUserConfig({
theme: plumeTheme({
plugins: {
markdownPower: {
repl: true,
repl: {
rust: true,
},
},
}
})
Expand All @@ -41,8 +43,24 @@ export default defineUserConfig({

使用 `::: rust-repl` 容器语法 将 Rust 代码块包裹起来。主题会检查代码块并添加执行按钮。

### 只读代码演示

rust 代码演示默认是只读的,不可编辑。

````md
::: rust-repl
::: rust-repl 自定义标题
```rust
// your rust code
```
:::
````

### 可编辑代码演示

如果需要在线编辑并执行,需要将代码块包裹在 `::: rust-repl#editable` 容器语法中

````md
::: rust-repl#editable 自定义标题
```rust
// your rust code
```
Expand All @@ -56,7 +74,7 @@ export default defineUserConfig({
**输入:**

````md
::: rust-repl
::: rust-repl 打印内容
```rust
fn main() {
println!("Hello, world!");
Expand All @@ -67,7 +85,7 @@ fn main() {

**输出:**

::: rust-repl
::: rust-repl 打印内容

```rust
fn main() {
Expand Down Expand Up @@ -107,6 +125,25 @@ fn main() {

### 等待子进程执行

**输入:**

````md
::: rust-repl
```rust
use std::process::Command;

fn main() {
let mut child = Command::new("sleep").arg("5").spawn().unwrap();
let _result = child.wait().unwrap();

println!("reached end of main");
}
```
:::
````

**输出:**

::: rust-repl

```rust
Expand All @@ -121,3 +158,29 @@ fn main() {
```

:::

### 可编辑的演示

**输入:**

````md
::: rust-repl#editable
```rust
fn main() {
println!("Hello, world!");
}
```
:::
````

**输出:**

::: rust-repl#editable

```rust
fn main() {
println!("Hello, world!");
}
```

:::
Loading

0 comments on commit 5addb31

Please sign in to comment.