| 1 | import type { Awaitable } from '@antfu/utils' |
| 2 | import type { KatexOptions } from 'katex' |
| 3 | import type { MermaidConfig } from 'mermaid' |
| 4 | import type * as monaco from 'monaco-editor' |
| 5 | import type { BuiltinLanguage, BuiltinTheme, CodeOptionsMeta, CodeOptionsThemes, CodeToHastOptionsCommon, LanguageInput, LanguageRegistration, MaybeArray } from 'shiki' |
| 6 | import type { VitePluginConfig as UnoCssConfig } from 'unocss/vite' |
| 7 | import type { PluginOption as VitePluginOption } from 'vite' |
| 8 | import type { App, ComputedRef, Ref } from 'vue' |
| 9 | import type { Router, RouteRecordRaw } from 'vue-router' |
| 10 | import type { CodeRunnerProviders } from './code-runner' |
| 11 | import type { ContextMenuItem } from './context-menu' |
| 12 | import type { ResolvedSlidevOptions } from './options' |
| 13 | import type { CodeblockTransformer, MarkdownTransformer } from './transform' |
| 14 | import type { SlidevPreparserExtension } from './types' |
| 15 | |
| 16 | export interface AppContext { |
| 17 | app: App |
| 18 | router: Router |
| 19 | } |
| 20 | |
| 21 | export interface MonacoSetupReturn { |
| 22 | editorOptions?: monaco.editor.IEditorOptions |
| 23 | } |
| 24 | |
| 25 | export interface NavOperations { |
| 26 | next: () => void |
| 27 | prev: () => Promise<void> |
| 28 | nextSlide: () => void |
| 29 | prevSlide: () => Promise<void> |
| 30 | go: (index: number) => void |
| 31 | goFirst: () => void |
| 32 | goLast: () => void |
| 33 | downloadPDF: () => Promise<void> |
| 34 | toggleDark: () => void |
| 35 | toggleOverview: () => void |
| 36 | toggleDrawing: () => void |
| 37 | escapeOverview: () => void |
| 38 | showGotoDialog: () => void |
| 39 | } |
| 40 | |
| 41 | export interface ShortcutOptions { |
| 42 | key: string | Ref<boolean> |
| 43 | fn?: () => void |
| 44 | autoRepeat?: boolean |
| 45 | name?: string |
| 46 | } |
| 47 | |
| 48 | export interface ShikiContext { |
| 49 | /** |
| 50 | * @deprecated Pass directly the theme name it's supported by Shiki. |
| 51 | * For custom themes, load it manually via `JSON.parse(fs.readFileSync(path, 'utf-8'))` and pass the raw JSON object instead. |
| 52 | */ |
| 53 | loadTheme: (path: string) => Promise<any> |
| 54 | } |
| 55 | |
| 56 | export type ShikiSetupReturn |
| 57 | = Partial< |
| 58 | & Omit<CodeToHastOptionsCommon<BuiltinLanguage>, 'lang'> |
| 59 | & CodeOptionsThemes<BuiltinTheme> |
| 60 | & CodeOptionsMeta |
| 61 | & { |
| 62 | langs: (MaybeArray<LanguageRegistration> | BuiltinLanguage)[] | Record<string, LanguageInput> |
| 63 | } |
| 64 | > |
| 65 | |
| 66 | export interface TransformersSetupReturn { |
| 67 | pre: (MarkdownTransformer | false)[] |
| 68 | /** |
| 69 | * @deprecated It has the same effect as `pre` |
| 70 | */ |
| 71 | preCodeblock: (MarkdownTransformer | false)[] |
| 72 | /** |
| 73 | * @deprecated It has the same effect as `pre` |
| 74 | */ |
| 75 | postCodeblock: (MarkdownTransformer | false)[] |
| 76 | /** |
| 77 | * @deprecated It has the same effect as `pre` |
| 78 | */ |
| 79 | post: (MarkdownTransformer | false)[] |
| 80 | |
| 81 | codeblocks: (CodeblockTransformer | false)[] |
| 82 | } |
| 83 | |
| 84 | // node side |
| 85 | export type ShikiSetup = (shiki: ShikiContext) => Awaitable<ShikiSetupReturn | void> |
| 86 | export type KatexSetup = () => Awaitable<Partial<KatexOptions> | void> |
| 87 | export type UnoSetup = () => Awaitable<Partial<UnoCssConfig> | void> |
| 88 | export type TransformersSetup = () => Awaitable<Partial<TransformersSetupReturn>> |
| 89 | export type PreparserSetup = (context: { |
| 90 | filepath: string |
| 91 | headmatter: Record<string, unknown> |
| 92 | mode?: string |
| 93 | }) => Awaitable<SlidevPreparserExtension[]> |
| 94 | export type VitePluginsSetup = (options: ResolvedSlidevOptions) => VitePluginOption |
| 95 | |
| 96 | // client side |
| 97 | export type MonacoSetup = (m: typeof monaco) => Awaitable<MonacoSetupReturn | void> |
| 98 | export type AppSetup = (context: AppContext) => Awaitable<void> |
| 99 | export type RootSetup = () => Awaitable<void> |
| 100 | export type RoutesSetup = (routes: RouteRecordRaw[]) => RouteRecordRaw[] |
| 101 | export type MermaidSetup = () => Awaitable<Partial<MermaidConfig> | void> |
| 102 | export type MermaidRenderFn = (code: string, options: Record<string, any>) => Awaitable<string> |
| 103 | export type MermaidRendererSetup = () => Awaitable<MermaidRenderFn | void> |
| 104 | export type ShortcutsSetup = (nav: NavOperations, defaultShortcuts: ShortcutOptions[]) => Array<ShortcutOptions> |
| 105 | export type CodeRunnersSetup = (runners: CodeRunnerProviders) => Awaitable<CodeRunnerProviders | void> |
| 106 | export type ContextMenuSetup = (items: ComputedRef<ContextMenuItem[]>) => ComputedRef<ContextMenuItem[]> |
| 107 | |
| 108 | function defineSetup<Fn>(fn: Fn) { |
| 109 | return fn |
| 110 | } |
| 111 | |
| 112 | export const defineShikiSetup = defineSetup<ShikiSetup> |
| 113 | export const defineUnoSetup = defineSetup<UnoSetup> |
| 114 | export const defineMonacoSetup = defineSetup<MonacoSetup> |
| 115 | export const defineAppSetup = defineSetup<AppSetup> |
| 116 | export const defineRootSetup = defineSetup<RootSetup> |
| 117 | export const defineRoutesSetup = defineSetup<RoutesSetup> |
| 118 | export const defineMermaidSetup = defineSetup<MermaidSetup> |
| 119 | export const defineMermaidRendererSetup = defineSetup<MermaidRendererSetup> |
| 120 | export const defineKatexSetup = defineSetup<KatexSetup> |
| 121 | export const defineShortcutsSetup = defineSetup<ShortcutsSetup> |
| 122 | export const defineTransformersSetup = defineSetup<TransformersSetup> |
| 123 | export const definePreparserSetup = defineSetup<PreparserSetup> |
| 124 | export const defineVitePluginsSetup = defineSetup<VitePluginsSetup> |
| 125 | export const defineCodeRunnersSetup = defineSetup<CodeRunnersSetup> |
| 126 | export const defineContextMenuSetup = defineSetup<ContextMenuSetup> |
| 127 |