Kotlin Playground
Let the Markdown file support kotlin playground in your VuePress site.
Settings
Install kotlin-playground in your project:
pnpm add -D kotlin-playgroundyarn add -D kotlin-playgroundnpm i -D kotlin-playgroundThen enabling via:
import { hopeTheme } from "vuepress-theme-hope";
export default hopeTheme({
markdown: {
kotlinPlayground: true,
},
});Usage
To use kotlin playground, you should use a container named kotlin-playground.
In it, you can use 2 directives:
@file Filenamethen a code block to add filesThe first file will be the main file, and others will be considered as hidden dependency.
@settingthen a json block to customize settings.Supported options:
data-version: Target Kotlin compiler versiondata-min-compiler-version: Minimum target Kotlin compiler versiondata-target-platform: target platform, can bejunit,canvas,jsorjava(default).data-highlight-only: Read-only mode, with only highlighting.data-highlight-only: "nocursor"means no focus on editor.Or, you can make only a part of code read-only by placing it between
//sampleStartand//sampleEndmarkers. If you don't need this just setnone-markers.data-js-libs: By default component loads jQuery and makes it available to the code running in the editor. If you need any additional JS libraries, specify them as comma-separated list in this attribute.data-output-height: Set the iframe height in px in output. Use for target platform canvas, default is200.data-crosslink: Show link for open in playground, can beenabledordisabled, defaults toundefined– only supported in playground.data-shorter-height: show expander if height more than value of attribute, defaults to100.data-scrollbar-style: Chooses a scrollbar implementation, Defaults tooverlay.args: Command line arguments.folded-button: set to false to hide code snippetauto-indent: Whether to use the context-sensitive indentation, can betrueorfalse(default).theme: Editor IntelliJ IDEA themes, can beidea,darculadefault.mode: Different languages styles. Runnable snippets only with kotlin, can bekotlin(default) ,js,java,groovy,xml,c,shell,swift,obj-c.autocompleteGet completion on every key press, can betrueorfalse(default). If false => Pressctrl+spaceto activate autocompletion.highlight-on-fly: Errors and warnings check for each change in the editor, can betrueorfalse(default).indent: How many spaces a block should be indented. Defaults to4.lines: Whether to show line numbers to the left of the editor, can betrueorfalse(default).fromandto: Create a part of code. Example from line 5 to line 10.match-brackets: Determines whether brackets are matched whenever the cursor is moved next to a bracket, can betrueorfalse(default).
You can see the below demos to see more details.
You can import and call defineKotlinPlaygroundConfig in client config file to customize kotlin-playground:
import { defineKotlinPlaygroundConfig } from "vuepress-plugin-md-enhance/client";
defineKotlinPlaygroundConfig({
// `kotlin-playground` options here
});interface KotlinPlaygroundOptions {
server?: string;
version?: string;
onChange?: (code: string) => void;
onRun?: () => void;
onError?: () => void;
getJsCode?: (code: string) => void;
onTestPassed?: () => void;
onTestFailed?: () => void;
onOpenConsole?: () => void;
onCloseConsole?: () => void;
callback?: (targetNode: HTMLElement, mountNode: HTMLElement) => void;
getInstance?: (instance: KotlinPlaygroundInstance) => void;
}Demo
class Contact(val id: Int, var email: String)
fun main(args: Array<String>) {
val contact = Contact(1, "mary@gmail.com")
println(contact.id)
}
::: kotlin-playground Playground title
@file main.kt
```kotlin
class Contact(val id: Int, var email: String)
fun main(args: Array<String>) {
val contact = Contact(1, "mary@gmail.com")
println(contact.id)
}
```
:::fun main(args: Array<String>) {
println("Hello World!")
}
::: kotlin-playground Readonly Playground
@file main.kt
```kotlin
fun main(args: Array<String>) {
println("Hello World!")
}
```
@settings
```json
{
"data-highlight-only": ""
}
```
:::import org.junit.Test
import org.junit.Assert
class TestExtensionFunctions() {
@Test fun testIntExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(4, 1), 4.r())
}
@Test fun testPairExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(2, 3), Pair(2, 3).r())
}
}
//sampleStart
/*
Then implement extension functions Int.r() and Pair.r() and make them convert Int and Pair to RationalNumber.
*/
fun Int.r(): RationalNumber = RationalNumber(this, 2)
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(first, second)
data class RationalNumber(val numerator: Int, val denominator: Int)
//sampleEnd
::: kotlin-playground Test Playground
@file main.kt
```kotlin
import org.junit.Test
import org.junit.Assert
class TestExtensionFunctions() {
@Test fun testIntExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(4, 1), 4.r())
}
@Test fun testPairExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(2, 3), Pair(2, 3).r())
}
}
//sampleStart
/*
Then implement extension functions Int.r() and Pair.r() and make them convert Int and Pair to RationalNumber.
*/
fun Int.r(): RationalNumber = RationalNumber(this, 2)
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(first, second)
data class RationalNumber(val numerator: Int, val denominator: Int)
//sampleEnd
```
@settings
```json
{
"data-target-platform": "junit"
}
```
:::external fun moment(): dynamic
fun main() {
val startOfDay = moment().startOf("day").fromNow()
println("The start of the day was $startOfDay")
}
::: kotlin-playground External JS Libraries
@file main.kt
```kotlin
external fun moment(): dynamic
fun main() {
val startOfDay = moment().startOf("day").fromNow()
println("The start of the day was $startOfDay")
}
```
@settings
```json
{
"data-target-platform": "js",
"data-js-libs": "https://unpkg.com/moment@2"
}
```
:::import cat.Cat
fun main(args: Array<String>) {
//sampleStart
val cat = Cat("Kitty")
println(cat.name)
//sampleEnd
}
::: kotlin-playground Multiple files
@file main.kt
```kotlin
import cat.Cat
fun main(args: Array<String>) {
//sampleStart
val cat = Cat("Kitty")
println(cat.name)
//sampleEnd
}
```
@file cat.kt
```kotlin
package cat
class Cat(val name: String)
```
:::Changelog
05fbb-on1bd5f-onb1741-on55e92-onb1230-on1b917-on22787-onb41c0-on671db-on54c46-on05f91-onc8aac-on8a130-on68339-on4d282-on1669d-on49ab9-ond86cb-on55ea3-on792dc-on83bff-on48f71-on3a6d6-on86180-on71423-on8174c-on52ad0-on7c8cf-on