Skip to content

Commit

Permalink
feat: allow Mermaid setup function to be async (#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX authored Jul 13, 2024
1 parent 4f20e30 commit 82af1e1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/client/modules/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function renderMermaid(lzEncoded: string, options: any) {

mermaid.initialize({
startOnLoad: false,
...clearUndefined(setupMermaid() || {}),
...clearUndefined(await setupMermaid() || {}),
...clearUndefined(options),
})
const code = lz.decompressFromBase64(lzEncoded)
Expand Down
10 changes: 5 additions & 5 deletions packages/client/setup/mermaid.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { MermaidOptions } from '@slidev/types'
import { defineMermaidSetup } from '@slidev/types'
import type { MermaidConfig } from 'mermaid'
import { createSingletonPromise } from '@antfu/utils'
import setups from '#slidev/setups/mermaid'

export default defineMermaidSetup(() => {
const setupReturn: MermaidOptions = {
export default createSingletonPromise(async () => {
const setupReturn: MermaidConfig = {
theme: 'default',
}

for (const setup of setups)
Object.assign(setupReturn, setup())
Object.assign(setupReturn, await setup())

return setupReturn
})
6 changes: 2 additions & 4 deletions packages/types/src/setups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { Awaitable } from '@antfu/utils'
import type * as monaco from 'monaco-editor'
import type { App, ComputedRef, Ref } from 'vue'
import type { RouteRecordRaw, Router } from 'vue-router'
import type mermaid from 'mermaid'
import type { KatexOptions } from 'katex'
import type { BuiltinLanguage, BuiltinTheme, CodeOptionsMeta, CodeOptionsThemes, CodeToHastOptionsCommon, Highlighter, LanguageInput } from 'shiki'
import type { VitePluginConfig as UnoCssConfig } from 'unocss/vite'
import type { MermaidConfig } from 'mermaid'
import type { SlidevPreparserExtension } from './types'
import type { CodeRunnerProviders } from './code-runner'
import type { ContextMenuItem } from './context-menu'
Expand All @@ -19,8 +19,6 @@ export interface MonacoSetupReturn {
editorOptions?: monaco.editor.IEditorOptions
}

export type MermaidOptions = (typeof mermaid.initialize) extends (a: infer A) => any ? A : never

export interface NavOperations {
next: () => void
prev: () => Promise<void>
Expand Down Expand Up @@ -78,7 +76,7 @@ export type MonacoSetup = (m: typeof monaco) => Awaitable<MonacoSetupReturn | vo
export type AppSetup = (context: AppContext) => Awaitable<void>
export type RootSetup = () => Awaitable<void>
export type RoutesSetup = (routes: RouteRecordRaw[]) => RouteRecordRaw[]
export type MermaidSetup = () => Partial<MermaidOptions> | void
export type MermaidSetup = () => Awaitable<Partial<MermaidConfig> | void>
export type ShortcutsSetup = (nav: NavOperations, defaultShortcuts: ShortcutOptions[]) => Array<ShortcutOptions>
export type CodeRunnersSetup = (runners: CodeRunnerProviders) => Awaitable<CodeRunnerProviders | void>
export type ContextMenuSetup = (items: ComputedRef<ContextMenuItem[]>) => ComputedRef<ContextMenuItem[]>
Expand Down

0 comments on commit 82af1e1

Please sign in to comment.