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