zero-md icon indicating copy to clipboard operation
zero-md copied to clipboard

how to add mermaid support?

Open CetinSert opened this issue 3 years ago • 6 comments

Following https://github.blog/2022-02-14-include-diagrams-markdown-files-mermaid/, there might be greater use of mermaid overall. How can one go about adding official mermaid block support to zero-md?

CetinSert avatar Feb 15 '22 09:02 CetinSert

Hey, thanks for your note. Given that Github now supports mermaid natively, I might add first-class support of this feature in a future release. In the meantime, something like this will work:

<head>
  ...
  <!-- Load mermaid -->
  <script defer src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>    
  <script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@2/dist/zero-md.min.js"></script>
</head>

<body>
  <!-- Render zero-md in light dom -->
  <zero-md id="app" src="mermaid.md" no-shadow manual-render></zero-md>
  <script>
    app.addEventListener('zero-md-ready', async () => {
      // Create custom marked renderer
      const renderer = new marked.Renderer()
      renderer.code = function (code, lang) {
        return lang === 'mermaid'
          ? `<div class="mermaid">${code}</div>`
          : `<pre><code>${code}</code></pre>`
      }
      await app.render({ renderer })
      mermaid.init()
    })
  </script>
</body>

Here's a demo: https://plnkr.co/edit/Q64k8e498RfGDdlC

zerodevx avatar Feb 15 '22 12:02 zerodevx

amazing library, looking forward to this feature

simboonlong avatar Aug 05 '22 09:08 simboonlong

This was exactly what I was looking for! Awesome project, thanks a lot :)

mmoehrlein avatar Sep 09 '22 07:09 mmoehrlein

@zerodevx – the approach suggested above breaks encapsulation.

<zero-md> is the only third-party custom element I have seen that behaves like the standard elements of HTML in that it does not gain attributes on getting connected to a document or leaks state in other weird ways.

Demo of just how awesome <zero-md> is

The demo linked below uses RTCode.io. (I am building it.)


See what we mean with the following steps:

  1. Go to md.rt.ht/? 👈🏻
  2. Press / keys
  3. Notice the video does not restart on edit. This is all because you are doing custom elements right™ ❤️!

SyncHTML.io – the next-gen, no-reload, real-time web playground with instant two-way input ⇄ output sync. (try, learn more)

CetinSert avatar Sep 09 '22 08:09 CetinSert

RTCode.io ❤️ <zero-md>

Dear @zerodevx,

We have dedicated the short domain md.rt.ht to the latest stable version of <zero-md>! It is the shortest domain you can get that serves as an editable quick starting point! See below ↙️!

In the address bar of your browser:

type get
md.rt.ht/? editor
md.rt.ht output

All you need is a single /? to edit your edge-deployed syncs! Just hit Ctrl+S1 to update it in 275+ cities around the world!

(1 RTEdge.net restricts saves/updates to Elefunc, Inc. until we reach public beta.)

Best Regards, @CetinSert

CetinSert avatar Sep 09 '22 08:09 CetinSert

@CetinSert

The demo linked below uses SyncHTML.io. (I am building it.)

Wow! That's a cool project - looks really good!

the approach suggested above breaks encapsulation.

Unfortunately, yes, mainly because of how mermaid works. For now, the markdown must be rendered into light dom - until first-class support is eventually added into zero-md (soon!). 😞

zerodevx avatar Sep 09 '22 17:09 zerodevx

First class support for mermaid diagrams (into the shadow dom no less) is now available in V3, so do check it out!

zerodevx avatar May 10 '24 08:05 zerodevx