/
code-runner.ts
65 lines (55 loc) · 1.61 KB
/
code-runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { CodeToHastOptions } from 'shiki'
import type { Arrayable, Awaitable } from '@antfu/utils'
export interface CodeRunnerContext {
/**
* Options passed to runner via the `runnerOptions` prop.
*/
options: Record<string, unknown>
/**
* Highlight code with shiki.
*/
highlight: (code: string, lang: string, options?: Partial<CodeToHastOptions>) => Promise<string>
/**
* Use (other) code runner to run code.
*/
run: (code: string, lang: string) => Promise<CodeRunnerOutputs>
}
export interface CodeRunnerOutputHtml {
/**
* The HTML to be rendered.
*
* Slidev does NOT sanitize the HTML for you - make sure it's from trusted sources or sanitize it before passing it in
*/
html: string
}
export interface CodeRunnerOutputDom {
/**
* The DOM element to be rendered.
*/
element: HTMLElement
}
export interface CodeRunnerOutputError {
/**
* The error message to be displayed.
*/
error: string
}
export interface CodeRunnerOutputText {
/**
* The text to be displayed.
*/
text: string
/**
* The class to be applied to the text.
*/
class?: string
/**
* The language to be highlighted.
*/
highlightLang?: string
}
export type CodeRunnerOutputTextArray = CodeRunnerOutputText[]
export type CodeRunnerOutput = CodeRunnerOutputHtml | CodeRunnerOutputError | CodeRunnerOutputText | CodeRunnerOutputTextArray | CodeRunnerOutputDom
export type CodeRunnerOutputs = Arrayable<CodeRunnerOutput>
export type CodeRunner = (code: string, ctx: CodeRunnerContext) => Awaitable<CodeRunnerOutputs>
export type CodeRunnerProviders = Record<string, CodeRunner>