Closed ottopaulsen closed 1 year ago
For multiline to work, use back tick and you should be able to do this.
<MyMermaid
type="sequenceDiagram"
:src="`
AliceJohn: Hello John, how are you?
loop Healthcheck
...
`"
/>
Now the containing >
is different story. When we add >
, we get
[plugin:vite:vue] Attribute name cannot contain U+0022 ("), U+0027 ('), and U+003C (<).
So this sounds expected error 🤔
@patak-dev Do you know how we can work around this issue?
Well, I have found a very nice solution to this, but then I am using Vuepress, not Vitepress:
First I have this component:
// Mermaid.vue
<template>
<div class="mermaid" v-html="svg"></div>
</template>
<script setup>
import mermaid from "mermaid";
import { useDarkMode } from "@vuepress/theme-default/lib/client/composables/useDarkMode";
import { ref, watchEffect } from "vue";
const props = defineProps({
src: null,
});
const isDarkMode = useDarkMode();
const svg = ref(null);
watchEffect(() => {
if (__VUEPRESS_SSR__ || props.src === "undefined") {
return;
}
const config = {
startOnLoad: false,
theme: "base",
themeVariables: {
primaryTextColor: "#000F3C",
primaryColor: isDarkMode.value ? "#000F3C" : "#E8FDFF",
},
};
mermaid.initialize(config);
const id = Math.floor(Math.random() * 100000);
mermaid.render("mermaid-" + id, props.src, (svgCode) => {
svg.value = svgCode;
});
});
</script>
Then I have this container that I install as a plugin:
// mermaid-container.js
const { containerPlugin } = require("@vuepress/plugin-container");
module.exports = containerPlugin({
type: "mermaid",
marker: "`",
render: (tokens, idx, options, env, slf) => {
if (tokens[idx].nesting === 1) {
// opening tag
const content = tokens.filter(
(t, i) => i > idx && t.level === 2 && t.nesting === 0
)[0]?.content;
return `<Mermaid src="${content}">`;
} else {
// closing tag
return "</Mermaid>\n";
}
},
});
Now you can write mermaid diagrams like this:
``` mermaid
sequenceDiagram
User->>Server: Request page
```
Which is perfect!
I am using Vuepress 2.0.0-beta.45
.
Ah, interesting. Cool. So essentially, we need to have Markdown plugin to do this. So maybe you could already do the same with VitePress by passing in markdown
option to config.js
? Not sure but...
I'll not be working on with this since I think it's a bit low priority in regards to releasing 1.0.0, but I guess this is nice feature to at least document 👍
So I'll keep this issue open as enhancement for now and see if anyone from the community can work on with it.
Just leaving a note here that https://sli.dev also supports mermaid out of the box, so maybe a good idea to see how they integrated it: https://sli.dev/guide/syntax.html#diagrams
Use some plugin like https://github.com/emersonbottero/vitepress-plugin-mermaid (this one is used in mermaid's next docs too).
Also, in your md plugin you're doing `<Mermaid src="${content}">`
, which will break in many cases. It is better to encode it, pass it as prop, then decode it in component.
Describe the bug
I cannot find any way to get a multiline text as input to a Vue component. In Vuepress 2 I managed to do this using a prop, but this seems not to be possible in Vitepress.
Ideally I would use the slot, but that did not work in Vuepress either. Using a prop named src is good enough.
My purpose is to make a Vue component that can display Mermaid diagrams. It works fine in Vuepress 2, but I cannot get it to work in Vitepress.
This is the bug I am getting:
Reproduction
npm install -s mermaid
docs/components
folder)index.md
file with data shown belowMyMermaid.vue:
index.md:
Expected behavior
I would expect to get the sequence diagram nicely displayed.
System Info
Additional context
The above describes my purpose. However, the problem seems to be related to having a prop containing both multiple lines and the
>
character.This much simpler scenario also gives the same bug:
MyComponent.vue:
index.md:
If you remove either the
>
or the newline from the src prop, it will not fail.Validations