Use mermaid in nuxt
在 Nuxt3 中使用 mermaid
2024-06-01T00:00:00.000Z
mermaidnuxt content
效果
因为写成了 vue 组件,在 template 里或在 mdc 中写都行
<mermaid>
sequenceDiagram
participant Alice
participant Bob
Alice->>John: Hello John, how are you$3
loop HealthCheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts <br/>prevail!
John-->>Alice: Great!
John->>Bob: How about you$4
Bob-->>John: Jolly good!
</mermaid>
[md]
Install
import mermaid from 'mermaid'
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.provide('mermaid', () => mermaid)
})
[ts] plugins/mermaid.client.ts
import type { Mermaid } from 'mermaid'
declare module '#app' {
interface NuxtApp {
$mermaid(): Mermaid
}
}
declare module 'vue' {
interface ComponentCustomProperties {
$mermaid(): Mermaid
}
}
export {}
[ts] index.d.ts
<script setup lang="ts">
let show = ref(false)
const { $mermaid } = useNuxtApp()
onMounted(async () => {
show.value = true
$mermaid().initialize({ startOnLoad: true })
await nextTick()
$mermaid().init()
})
</script>
<template>
<div class="mermaid" v-if="show">
<slot></slot>
</div>
</template>
[vue] components/global/mermaid.vue