@politecnicoopenunixlabs/slidev-addon-code-runners 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@politecnicoopenunixlabs/slidev-addon-code-runners",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "keywords": [
5
5
  "slidev-addon",
6
6
  "slidev"
@@ -12,7 +12,8 @@
12
12
  "license": "GPL-3.0",
13
13
  "type": "module",
14
14
  "files": [
15
- "setup"
15
+ "setup",
16
+ "runners"
16
17
  ],
17
18
  "scripts": {
18
19
  "build": "slidev build example.md",
@@ -0,0 +1,65 @@
1
+ import { defineCodeRunnersSetup } from '@slidev/types'
2
+
3
+ // Stupid ahh brainfuck interpreter for testing
4
+
5
+ const TAPE_SIZE = 30000
6
+ const MAX_STEPS = 10_000_000
7
+
8
+ const runBrainfuck = (code: string, input = ''): string => {
9
+ // Precompute matching brackets for O(1) loop jumps
10
+ const jump: number[] = new Array<number>(code.length)
11
+ const stack: number[] = []
12
+ for (let i = 0; i < code.length; i++) {
13
+ if (code[i] === '[') {
14
+ stack.push(i)
15
+ }
16
+ else if (code[i] === ']') {
17
+ const open = stack.pop()
18
+ if (open === undefined)
19
+ throw new SyntaxError(`Unmatched ']' at position ${i.toString()}`)
20
+ jump[i] = open
21
+ jump[open] = i
22
+ }
23
+ }
24
+ if (stack.length)
25
+ throw new SyntaxError(`Unmatched '[' at position ${stack.pop()?.toString() || 'unknown'}`)
26
+
27
+ const tape = new Uint8Array(TAPE_SIZE) // cells wrap at 0-255
28
+ let ptr = 0
29
+ let inPtr = 0
30
+ let steps = 0
31
+ let out = ''
32
+
33
+ for (let pc = 0; pc < code.length; pc++) {
34
+ if (++steps > MAX_STEPS)
35
+ throw new Error(`Step limit exceeded (${MAX_STEPS.toString()}), possible infinite loop`)
36
+
37
+ switch (code[pc]) {
38
+ case '>': ptr = (ptr + 1) % TAPE_SIZE; break
39
+ case '<': ptr = (ptr - 1 + TAPE_SIZE) % TAPE_SIZE; break
40
+ case '+': tape[ptr]++; break
41
+ case '-': tape[ptr]--; break
42
+ case '.': out += String.fromCharCode(tape[ptr]); break
43
+ case ',': tape[ptr] = inPtr < input.length ? input.charCodeAt(inPtr++) : 0; break
44
+ case '[': if (!tape[ptr]) pc = jump[pc]; break
45
+ case ']': if (tape[ptr]) pc = jump[pc]; break
46
+ }
47
+ }
48
+ return out
49
+ }
50
+
51
+ export default defineCodeRunnersSetup(() => {
52
+ return {
53
+ brainfuck(code) {
54
+ try {
55
+ const output = runBrainfuck(code)
56
+
57
+ return { text: output || '(no output)', highlightLang: 'text' }
58
+ }
59
+ catch (e) {
60
+ const message = e instanceof Error ? e.message : String(e)
61
+ return { error: message }
62
+ }
63
+ },
64
+ }
65
+ })
package/runners/go.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { defineCodeRunnersSetup } from "@slidev/types"
2
+
3
+ const GO_COMPILE_ENDPOINT = "https://play.go-lang.org/compile"
4
+
5
+ export default defineCodeRunnersSetup(() => {
6
+ return {
7
+ async go(code) {
8
+ try {
9
+ const formData = new FormData()
10
+ formData.append("version", "2")
11
+ formData.append("body", code)
12
+ formData.append("withVet", "true")
13
+
14
+ const res = await fetch(GO_COMPILE_ENDPOINT, {
15
+ method: "POST",
16
+ body: formData
17
+ })
18
+
19
+ if (!res.ok) return { error: `Go Playground returned HTTP ${String(res.status)}` }
20
+
21
+ const data = (await res.json()) as { Errors: string; Events: { Message: string }[] }
22
+ if (data.Errors) return { error: data.Errors }
23
+
24
+ const output = data.Events.map(event => event.Message).join("")
25
+ return { text: output || "(no output)", highlightLang: "text" }
26
+ } catch (e) {
27
+ const message = e instanceof Error ? e.message : String(e)
28
+ return { error: `Request failed: ${message}` }
29
+ }
30
+ }
31
+ }
32
+ })
@@ -0,0 +1,52 @@
1
+ import { defineCodeRunnersSetup } from "@slidev/types"
2
+
3
+ type KotlinCodeRunnerError = {
4
+ severity?: string
5
+ message?: string
6
+ }
7
+
8
+ type KotlinCodeRunnerResponse = {
9
+ text?: string
10
+ exception?: string | null
11
+ errors?: Record<string, KotlinCodeRunnerError[]>
12
+ }
13
+
14
+ const KOTLIN_COMPILER_VERSION = "2.4.21"
15
+ const KOTLIN_COMPILER_URL = `https://api.kotlinlang.org/api/${KOTLIN_COMPILER_VERSION}/compiler/run`
16
+
17
+ export default defineCodeRunnersSetup(() => {
18
+ return {
19
+ async kotlin(code) {
20
+ try {
21
+ const res = await fetch(KOTLIN_COMPILER_URL, {
22
+ method: "POST",
23
+ headers: { "Content-Type": "application/json; charset=utf-8" },
24
+ body: JSON.stringify({
25
+ args: "",
26
+ confType: "java",
27
+ files: [{ name: "File.kt", text: code, publicId: "" }]
28
+ })
29
+ })
30
+
31
+ if (!res.ok) return { error: `Kotlin Playground returned HTTP ${String(res.status)}` }
32
+
33
+ const data = (await res.json()) as KotlinCodeRunnerResponse
34
+ const errors = Object.values(data.errors || {})
35
+ .flat()
36
+ .filter(error => error.severity === "ERROR")
37
+ .map(error => error.message)
38
+ .filter((message): message is string => !!message)
39
+
40
+ if (errors.length > 0) return { error: errors.join("\n") }
41
+
42
+ if (data.exception) return { error: data.exception }
43
+
44
+ const output = data.text?.replace(/^<outStream>/, "").replace(/<\/outStream>$/, "")
45
+ return { text: output || "(no output)", highlightLang: "text" }
46
+ } catch (e) {
47
+ const message = e instanceof Error ? e.message : String(e)
48
+ return { error: `Request failed: ${message}` }
49
+ }
50
+ }
51
+ }
52
+ })
@@ -0,0 +1,42 @@
1
+ import { defineCodeRunnersSetup } from "@slidev/types"
2
+
3
+ type RustCodeRunnerResponse = {
4
+ success: boolean
5
+ stdout?: string
6
+ stderr?: string
7
+ error?: string
8
+ }
9
+
10
+ export default defineCodeRunnersSetup(() => {
11
+ return {
12
+ async rust(code) {
13
+ try {
14
+ const res = await fetch("https://play.rust-lang.org/execute", {
15
+ method: "POST",
16
+ headers: { "Content-Type": "application/json" },
17
+ body: JSON.stringify({
18
+ channel: "stable",
19
+ mode: "debug",
20
+ edition: "2021",
21
+ crateType: "bin",
22
+ tests: false,
23
+ backtrace: false,
24
+ code: code
25
+ })
26
+ })
27
+
28
+ const data = (await res.json()) as RustCodeRunnerResponse
29
+
30
+ if (data.success) return { text: data.stdout || "(no output)", highlightLang: "text" }
31
+
32
+ return {
33
+ text: data.stderr || data.error || "Unknown error",
34
+ highlightLang: "text"
35
+ }
36
+ } catch (e) {
37
+ const message = e instanceof Error ? e.message : String(e)
38
+ return { error: `Request failed: ${message}` }
39
+ }
40
+ }
41
+ }
42
+ })