返回 slidev
transform.ts
根目录 / packages / types / src / transform.ts
1 import type { Awaitable } from '@antfu/utils'
2 import type MagicString from 'magic-string-stack'
3 import type { ResolvedSlidevOptions } from './options'
4 import type { SlideInfo } from './types'
5
6 export interface MarkdownTransformContext {
7 /**
8 * The magic string instance for the current markdown content
9 */
10 s: MagicString
11
12 /**
13 * The slide info of the current slide
14 */
15 slide: SlideInfo
16
17 /**
18 * Resolved Slidev options
19 */
20 options: ResolvedSlidevOptions
21 }
22
23 export type MarkdownTransformer = (ctx: MarkdownTransformContext) => Awaitable<void>
24
25 export interface CodeblockTransformContext {
26 /**
27 * The language and meta info of the code block, i.e., the content after "```"
28 */
29 info: string
30
31 /**
32 * The content of the code block
33 */
34 code: string
35
36 /**
37 * Get the highlighted code HTML.
38 */
39 renderHighlighted: (override: { info?: string, code?: string }) => Awaitable<string>
40
41 /**
42 * The number of "`"
43 */
44 fence: number
45
46 /**
47 * The slide info of the current slide
48 */
49 slide: SlideInfo | null
50
51 /**
52 * Resolved Slidev options
53 */
54 options: ResolvedSlidevOptions
55 }
56
57 export type CodeblockTransformer = (ctx: CodeblockTransformContext) => Awaitable<string | undefined | null>
58
59 function defineSetup<Fn>(fn: Fn) {
60 return fn
61 }
62
63 export const defineMarkdownTransformer = defineSetup<MarkdownTransformer>
64 export const defineCodeblockTransformer = defineSetup<CodeblockTransformer>
65
65 lines TYPESCRIPT