Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Mermaid #465

Open
alaminkouser opened this issue Aug 9, 2023 · 20 comments
Open

Support for Mermaid #465

alaminkouser opened this issue Aug 9, 2023 · 20 comments
Labels
enhancement New feature or request Idea plugins

Comments

@alaminkouser
Copy link

alaminkouser commented Aug 9, 2023

Enter your suggestions in details:

Mermaid

  • A Graph Generator
@alaminkouser alaminkouser added the enhancement New feature or request label Aug 9, 2023
@oscarotero
Copy link
Member

This came up on Discord server some time ago (https://discord.com/channels/794537085641818124/1111980136691146773) but looks like it's not possible to run Mermaid on server side due mermaid-js/mermaid#3650.

There are a couple of alternative options that you can use:

@alaminkouser
Copy link
Author

I found this article: https://css-tricks.com/making-mermaid-diagrams-in-markdown/

Mermaid has cdn. And It can be used in browser. I tested in this page:
https://alaminkouser.github.io/mermaid/
Source: https://github.com/alaminkouser/alaminkouser.github.io/tree/b2ed2c811bfea580b20f89d2625306a00d0db27c

Although I can not find a way to generate SVG and link/embed the svg, but this currently works.

@oscarotero
Copy link
Member

Yes, the only way to run mermaid is in the browser.
But it would be great to have a way to run it in build time, so there's no need to import any script in the html because the code is already generated. That's the problem with mermaid. I hope they implement server side support soon.

@alaminkouser
Copy link
Author

Any thoughts regarding this:
https://github.com/mermaid-js/mermaid-cli

@oscarotero
Copy link
Member

Seems that it uses puppeteer for rendering.
I'm not sure about performance (specially if there are many diagrams and charts) but I guess we can give a try.

@alaminkouser
Copy link
Author

deno has puppeteer. So we may avoid npm and use puppeteer in deno. I will test it.

@alaminkouser
Copy link
Author

Here is a quick code for running puppeteer in deno and getting all svg and save the svgs in a dir:
https://github.com/alaminkouser/deno-mermaid/tree/61d526f78d71bd9ab3b1cf12f40a9b39c90e1bbc

@oscarotero
Copy link
Member

Okay, thanks!
I'm getting this error:

➜  deno-mermaid git:(main) deno run -A main.ts
error: Uncaught PermissionDenied: Permission denied (os error 13)
    this.proc = Deno.run({
                     ^
    at opRun (ext:runtime/40_process.js:46:14)
    at Object.run (ext:runtime/40_process.js:132:15)
    at BrowserRunner.start (https://deno.land/x/[email protected]/src/deno/BrowserRunner.ts:64:22)
    at ChromeLauncher.launch (https://deno.land/x/[email protected]/src/deno/Launcher.ts:108:12)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async file:///Users/oscarotero/www/deno-mermaid/main.ts:3:17

@alaminkouser
Copy link
Author

Okay, thanks! I'm getting this error:

➜  deno-mermaid git:(main) deno run -A main.ts
error: Uncaught PermissionDenied: Permission denied (os error 13)
    this.proc = Deno.run({
                     ^
    at opRun (ext:runtime/40_process.js:46:14)
    at Object.run (ext:runtime/40_process.js:132:15)
    at BrowserRunner.start (https://deno.land/x/[email protected]/src/deno/BrowserRunner.ts:64:22)
    at ChromeLauncher.launch (https://deno.land/x/[email protected]/src/deno/Launcher.ts:108:12)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async file:///Users/oscarotero/www/deno-mermaid/main.ts:3:17

Read this: DOC

I am not sure, but you may need to run this command:
PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/[email protected]/install.ts

@oscarotero
Copy link
Member

@alaminkouser ok, the problem was the execution permissions for the downloaded binary.

I've been experimenting with puppeteer (Deno already supports the Node version which is more updated).
You can take a look to this experimental plugin:
https://github.com/lumeland/experimental-plugins/tree/main/puppeteer

There are some issues yet but it's a good start.

@alaminkouser
Copy link
Author

Interesting. I will also try this.

@oscarotero
Copy link
Member

I was able to render mermaid on server side with the Astral experimental plugin:
https://github.com/lumeland/experimental-plugins/tree/main/astral

It runs the javascript code on server side and save the generated HTML code.

For example, this page https://github.com/lumeland/experimental-plugins/blob/main/astral/demo/page.vto#L24-L28

  • Run the javascript code
  • Remove the script element (all elements with astral-delete attribute are removed)

@alaminkouser
Copy link
Author

Amazing! Next week I will try this. It can be powerful and faster. Now generated websites can be without/less JS codes!!!

@alaminkouser
Copy link
Author

  • I cloned the experimental repo

  • went to astral/demo directory

  • ran deno task build

  • It generated the website.

  • It is the output:
    Screenshot from 2024-06-28 22-56-08

  • It is working.

  • I found this output in command line while I ran deno task build:
    Screenshot from 2024-06-28 22-50-24

It is downloading chrome. It may not use puppeteer, but uses chrome in server side.

I tested it with a newly deno install in my computer.

@oscarotero
Copy link
Member

Yes, it doesn't use puppeteer, but astral https://github.com/lino-levan/astral which is like Puppeteer but for Deno (Puppeteer doesn't work in Deno).

For now, the plugin is just a POC and there are some limitations (for example, it only can load assets from external urls). But for this specific use case it works pretty well.

@alaminkouser
Copy link
Author

Only downside being it uses chrome. So cicd will be slow.

But seems stable.

@oscarotero
Copy link
Member

I didn't play much with the library, but seems that Firefox is also supported.

If no browser is specified, it downloads chrome automatically.

@ooker777
Copy link
Contributor

ooker777 commented Aug 7, 2024

Discussion for anyone interested: What makes mermaid.js cannot work on server-side, while other visualization libs can?

@ooker777
Copy link
Contributor

FYI I made a plugin to inject Mermaid script at the front end: https://github.com/ooker777/lume-mermaid-plugin

@foster-hangdaan
Copy link

To add to the list of options, I've created a Kroki plugin for Lume which converts codeblocks containing diagram descriptions into an <img/> during the site build. It currently has support for all of the Kroki Diagram Types; Mermaid being one of them.

The images are rendered by a Kroki server. By default, the plugin uses the official instance at https://kroki.io, but can be configured to use a self-hosted instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Idea plugins
Projects
None yet
Development

No branches or pull requests

4 participants