6.0.0 • Published 19 days ago

vue-mermaid-string v6.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
19 days ago

vue-mermaid-string

A Vue.js component that turns a Mermaid string into a diagram.

In contrast to vue-mermaid, which works by passing structured data to it, this component uses the diagram string directly. The advantage is that it always supports the latest language standard and is easier to use if you have an existing diagram. Depends on your use case which fits better.

Install via a package manager

# npm
$ npm install vue-mermaid-string

# Yarn
$ yarn add vue-mermaid-string

Add to local components:

<script>
import VueMermaidString from 'vue-mermaid-string'

export default {
  components: {
    VueMermaidString,
  },
}
</script>

Or register as a global component:

import VueMermaidString from 'vue-mermaid-string'

app.component('VueMermaidString', VueMermaidString)

Or register as a plugin:

import VueMermaidString from 'vue-mermaid-string'

app.use(VueMermaidString)

Install via CDN

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://unpkg.com/vue-mermaid-string"></script>

Version compatibility

vue-mermaid-stringVue.js
>= v4v3
<= v3v2

Usage

Usage is simple, you pass a Mermaid string to the component and you get a visual diagram. For ease of use, we will use the endent package to declare multiline strings. Of course you can also write them using \n.

<template>
  <vue-mermaid-string :value="diagram" />
</template>
<script>
import endent from 'endent'

export default {
  computed: {
    // equals graph TD\n  A --> B
    diagram: () => endent`
      graph TD
        A --> B
    `,
  },
}
</script>

Mermaid options

It is possible to customize the diagram by passing options to the component. The options are internally passed to mermaid.initialize. See the default config for a list of available options.

<template>
  <vue-mermaid-string :value="diagram" :options="{ maxTextSize: 10000 }" />
</template>

Click events

You can register click events by declaring them in the diagram string. To react to a click event, you need to declare it in the diagram via a click <nodeId> declaration. See the Mermaid docs for details.

When registering a callback, you do not need to specify the callback name, the component will magically inject it into the diagram by itself. Implement the node-click event handler to react to click events:

<template>
  <vue-mermaid-string :value="diagram" @node-click="nodeClick" />
</template>
<script>
import endent from 'endent'

export default {
  computed: {
    diagram: () => endent`
      graph TD
        A --> B
        click A
        click B
    `,
  },
  methods: {
    nodeClick: nodeId => console.log(nodeId),
  },
}
</script>

You can also still implement node links. In this case, the handler won't be called but instead the node will be an <a> tag that opens the link on click:

<template>
  <vue-mermaid-string :value="diagram" />
</template>
<script>
import endent from 'endent'

export default {
  computed: {
    diagram: () => endent`
      graph TD
        A --> B
        click B href "https://github.com"
    `,
  },
}
</script>

Error handling

Mermaid has its own default error handling behavior, outputting a little graphical error message if a parsing error occurs. If you want to have custom error handling, you can react to the @parse-error event. Here is a simple example that outputs the error message as a plain string:

<template>
  <div v-if="error">{{ error }}</div>
  <self v-else value="foo" @parse-error="error = $event" />
</template>
<script>
export default {
  data: () => ({
    error: undefined,
  }),
}
</script>

Contribute

Are you missing something or want to contribute? Feel free to file an issue or a pull request! ⚙️

Support

Hey, I am Sebastian Landwehr, a freelance web developer, and I love developing web apps and open source packages. If you want to support me so that I can keep packages up to date and build more helpful tools, you can donate here:

Thanks a lot for your support! ❤️

See also

License

MIT License © Sebastian Landwehr

5.0.1

19 days ago

6.0.0

19 days ago

5.0.0

3 months ago

4.0.70

7 months ago

4.0.63

1 year ago

4.0.62

1 year ago

4.0.65

1 year ago

4.0.64

1 year ago

4.0.61

1 year ago

4.0.60

1 year ago

4.0.67

1 year ago

4.0.66

1 year ago

4.0.69

1 year ago

4.0.68

1 year ago

4.0.52

1 year ago

4.0.5

1 year ago

4.0.51

1 year ago

4.0.4

1 year ago

4.0.54

1 year ago

4.0.7

1 year ago

4.0.53

1 year ago

4.0.6

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.0.50

1 year ago

4.0.3

1 year ago

4.0.2

1 year ago

4.0.59

1 year ago

4.0.56

1 year ago

4.0.55

1 year ago

4.0.58

1 year ago

4.0.57

1 year ago

4.0.9

1 year ago

4.0.8

1 year ago

3.1.0

1 year ago

4.0.19

1 year ago

4.0.21

1 year ago

4.0.20

1 year ago

4.0.27

1 year ago

4.0.26

1 year ago

4.0.29

1 year ago

4.0.28

1 year ago

4.0.23

1 year ago

4.0.22

1 year ago

4.0.25

1 year ago

4.0.24

1 year ago

4.0.10

1 year ago

4.0.16

1 year ago

4.0.15

1 year ago

4.0.18

1 year ago

4.0.17

1 year ago

4.0.12

1 year ago

4.0.11

1 year ago

4.0.14

1 year ago

4.0.13

1 year ago

4.0.41

1 year ago

4.0.40

1 year ago

4.0.43

1 year ago

4.0.42

1 year ago

4.0.49

1 year ago

4.0.48

1 year ago

4.0.45

1 year ago

4.0.44

1 year ago

4.0.47

1 year ago

4.0.46

1 year ago

4.0.30

1 year ago

4.0.32

1 year ago

4.0.31

1 year ago

4.0.38

1 year ago

4.0.37

1 year ago

4.0.39

1 year ago

4.0.34

1 year ago

4.0.33

1 year ago

4.0.36

1 year ago

4.0.35

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.2.1

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.5

2 years ago

2.2.4

2 years ago

2.2.6

2 years ago

2.2.0

2 years ago

2.1.5

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.0

3 years ago

2.0.15

3 years ago

2.0.14

3 years ago

2.0.13

3 years ago

2.0.12

3 years ago

2.0.11

3 years ago

2.0.10

3 years ago

2.0.9

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.8

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

1.0.5

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago