Markdown-it Diagram Control Plugin
Jun 08. 2024

Summary
RyanAI
Loading.
This content is generated based on the article and is only used for explanation and summary of the article content.
npm versionnpm downloadsbundleJSDocsLicense

markdown-it-diagramis a markdown-it plugin for creating diagrams in Markdown documents. It supports mermaid and plantuml, and also provides control functions such as zooming and moving.

UML 示例

Markdown supports:plantumlmermaiddotditaa

Features

  • Support PlantUML、Mermaid、Dot、Ditaa syntax;
  • Support zoom、move、rough、download、copy origin code and soon contorls;
  • Support Shift and mouse wheel to zoom in or out;
  • Support modal preview;
  • Support long press mouse click to drag the chart

PlantUML

try it

```plantuml
Bob -> Alice : hello
```
uml diagram

DOT

```dot
digraph example1 {
    1 -> 2 -> { 4, 5 };
    1 -> 3 -> { 6, 7 };
}
```
uml diagram

ditaa

Warning: In PlantUML, only PNG, ASCII Art image generation is supported.

```ditaa
+--------+   +-------+    +-------+
    |        | --+ ditaa +--> |       |
    |  Text  |   +-------+    |diagram|
    |Document|   |!magic!|    |       |
    |     {d}|   |       |    |       |
    +---+----+   +-------+    +-------+
        :                         ^
        |       Lots of work      |
        +-------------------------+
```
uml diagram

mermaid

```mermaid
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
```
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;

Installation

npm install markdown-it-diagram --save

Usage

Vite Configuration

// vite.config.ts
import MarkdownItDiagrams from 'markdown-it-diagram'
import Markdown from 'unplugin-vue-markdown/vite'
export default defineConfig({
  plugins: [
    Markdown({
      markdownItSetup(md) {
        md.use(MarkdownItDiagrams, {
          showController: true, // show controller,default:false
          /**
           * PlantUML options
           * ditaa:imageFormat 'png| txt'
           * plantuml: imageFormat'png| svg| txt'
           * dot: imageFormat'png| svg| txt'
           */
          // imageFormat: 'svg', // image format:svg|png|txt,default:svg
          // server: '', // plantuml server,default:http://www.plantuml.com/plantuml
          // ditaa: {
          // imageFormat: 'svg', // image format:png|txt,default:svg
          // server: '', // plantuml server,default:http://www.plantuml.com/plantuml
          // }
        })
      }
    })
  ]
})

If you open the controller, you need to import the script in the initialization. vue3 example:

<script setup lang="ts">
import { markdownItDiagramDom } from 'markdown-it-diagram/dom'
import { onMounted } from 'vue'
onMounted(async () => {
  // if you want to use mermaid, you need to install mermaid.js
  // npm install mermaid
  // import mermaid from 'mermaid'
  mermaid.initialize({ startOnLoad: false })
  await mermaid.run()
  // initialize markdown-it-diagram/dom script
  await markdownItDiagramDom()
})
</script>
> comment on / twitter
>
CC BY-NC-SA 4.0 2021-PRESENT © Ryan uo