Dart JS interop for Mermaid - The Javascript tool that makes use of a markdown based syntax to render customizable diagrams, charts and visualizations.

Overview

Mermaid (Dart)

Dart JS interop for Mermaid - Javascript library that makes use of a markdown based syntax to render customizable diagrams, charts and visualizations..

Live example of Markdown Live Editor with Mermaid support can be found here.

The usage is very simple. Mermaid diagrams are placed within

``​`mermaid

...diagram source...

``​`

blocks within the markdown source. The markdown is then converted to HTML using the markdownToHtml() method from the Markdown package. This html is placed within a div on the html page. The Markdown package will create <code></code> elements with the class 'language-mermaid' for each of the mermaid blocks within the original markdown source. Here is where the Mermaid package comes into play. The Mermaid package is initialized with a call to MermaidApi.initialize(), the mermaid theme and other configurations options can be set in this call. The next step is to simply call mermaidRender() with one of the following - a W3C selector for the html elements containing mermaid source, a List<Element> of elements containing mermaid source, or a single html Element containing mermaid diagram source.

There are other ways to invoke the conversion that the example app.dart illustrates. These include methods that return the SVG code directly.
Note that the Mermaid javascript library requires a browser dom to create the SVG, so it is not possible to create SVG from Mermaid diagram source outside of the browser environment (ie. server side), although it may be possible using something like JSDom or puppeteer that allows dom use by javascript on the server. Such a server side project remains outside the scope of Mermaid.dart.

Usage

  1. Register the Dart package in your pubspec.yaml:

    dependencies:
      mermaid: ^0.9.3
  2. Load the latest Mermaid source (js and css) in your html layout:

    <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
    
    <div id="html"></div>
  3. Call methods on Mermaid:

    import 'dart:html';
    
    import 'package:markdown/markdown.dart' as md;
    import 'package:mermaid/mermaid.dart';
    
    const testMarkdownWithMermaid="""
    [Sequence Diagram](http://mermaid-js.github.io/mermaid/#/./sequenceDiagram)
    ------------------
    ``​`mermaid
    sequenceDiagram
        participant Alice
        participant Bob
        Alice->>John: Hello John, how are you?
        loop Healthcheck
            John->>John: Fight against hypochondria
        end
        Note right of John: Rational thoughts <br/>prevail!
        John-->>Alice: Great!
        John->>Bob: How about you?
        Bob-->>John: Jolly good!
    ``​`
    """;
    
    final nullSanitizer_SVGCantBeInsertedWithoutIt = NullTreeSanitizer();
    
    void main() {
      // This Config() object is show with default args for illustration of
      // available options, and indeed passing a Config object is not
      // even required.
      MermaidApi.initialize(
          Config(
            securityLevel:SecurityLevel.Strict,
            theme:Theme.Forest,
            logLevel: LogLevel.Error,
            startOnLoad:false,
            arrowMarkerAbsolute:true,
            flowchart:FlowChartConfig(htmlLabels: true),
            sequence:SequenceDiagramConfig(),
            gnatt:GnattConfig(),
          )
        );
    
      final htmlDiv = document.getElementById('html');
    
      htmlDiv.setInnerHtml(
        md.markdownToHtml(testMarkdownWithMermaid,
            extensionSet: md.ExtensionSet.gitHubWeb,
            treeSanitizer: nullSanitizer_SVGCantBeInsertedWithoutIt,
        )
      );
    
      mermaidRender(htmlDiv.querySelectorAll('code.language-mermaid'));
    }
    
    /// This sanitizer filters NO node types, allowing an SVG file to be inserted
    /// using setInnerHtml.  Without it all the SVG nodes would be sanitized
    /// out of the SVG file.
    class NullTreeSanitizer implements NodeTreeSanitizer {
      @override
      void sanitizeTree(Node node) {}
    }

This example does not make user of any of the methods that accept a dart function to be used as a callback. Please remember that when passing a Dart function as a callback, make sure to wrap it with allowInterop().

Check the Mermaid and API reference for detailed documentation.

To view the example, run dart pub global run webdev serve example from the root directory of this project (or shorter webdev serve example). run dart pub global activate webdev to activate webdev if needed.

You might also like...

This library provides a customizable Flutter widget that makes it easy to display text in the middle of a Divider.

This library provides a customizable Flutter widget that makes it easy to display text in the middle of a Divider.

1. About 1.1. Introduction 1.1.1. Install Library 1.1.2. Import It 1.1.3. Use TextDivider 1.2. Details 1.2.1. Customization Options 1.2.2. Horizontal

Feb 9, 2022

This is tool to create 3D Models which can be used in Flutter Applications. Tool is developed completely using Flutter.

three_d_model_tool A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you start

Nov 8, 2022

Flutter package to render html as widgets that supports hyperlink, image, audio, video, iframe and many other tags.

Flutter package to render html as widgets that supports hyperlink, image, audio, video, iframe and many other tags.

HtmlWidget monorepo This repo contains the source code for everything HtmlWidget-related. Name Link flutter_widget_from_html_core flutter_widget_from_

Jan 6, 2023

A Flutter widget for rendering static html as Flutter widgets (Will render over 80 different html tags!)

A Flutter widget for rendering static html as Flutter widgets (Will render over 80 different html tags!)

flutter_html A Flutter widget for rendering HTML and CSS as Flutter widgets. Screenshot 1 Screenshot 2 Screenshot 3 Table of Contents: Installing Curr

Jan 5, 2023

Flutter plugin to render stacked page view

Stacked Page View! pub.dev/stacked_page_view Hi! This package will create stacked page view in your flutter app. it's as lightweight as it can get ⚡ ⚡

Nov 25, 2022

Flutter package to render a Material Design Speed Dial.

Flutter package to render a Material Design Speed Dial.

Flutter Speed Dial Flutter package to render a Material Design Speed Dial. Usage The SpeedDial widget is built to be placed in the Scaffold.floatingAc

May 20, 2022

A widget based on Flutter's new Interactive Viewer that makes picture pinch zoom, and return to its initial size and position when released.

A widget based on Flutter's new Interactive Viewer that makes picture pinch zoom, and return to its initial size and position when released.

pinch_zoom A widget based on Flutter's new Interactive Viewer that makes picture pinch zoom, and return to its initial size and position when released

Dec 30, 2022

Simple and beautiful smooth animated charts.

Simple and beautiful smooth animated charts.

Charts Flutter Simple and beautiful smooth animated charts. Supported charts Bar Group bar Candle Line Pie Example Check /example folder for more deta

Jan 3, 2023

🙌🏾 This package makes it easy to use the Mono connect widget in a flutter project

🙌🏾 This package makes it easy to use the Mono connect widget in a flutter project

Flutter Mono ** This is an unofficial SDK for flutter This package makes it easy to use the Mono connect widget in a flutter project. 📸 Screen Shots

Dec 20, 2022
Comments
  • Render in flutter

    Render in flutter

    Hi @timmaffett . This package looks cool.

    Technically, we can use a Flutter TextField to take input markdown and render the graph in an embedded webview inside Flutter app below the TextField.

    Just wondering if that's correct and have you tried that?

    opened by liangxianzhe 2
Owner
Tim Maffett
Coding, Compassion and Kindness
Tim Maffett
An intuitive Token Parser that includes grammar definition, tokenization, parsing, syntax error and debugging. Implementation based on Lexical Analysis for Dart.

Token Parser An intuitive Token Parser that includes syntax/grammar definition, tokenization and parsing. Implementation based on Lexical Analysis. Re

JUST A SNIPER ツ 2 Dec 15, 2022
A highly customizable Flutter widget to render and interact with JSON objects.

The spreadsheet with superpowers ✨ ! JSON Data Explorer A highly customizable widget to render and interact with JSON objects. Features Expand and col

rows 15 Dec 21, 2022
A feature-rich cross-platform webview using webview_flutter for mobile and iframe for web. JS interop-ready.

A feature-rich cross-platform webview using webview_flutter for mobile and iframe for web. JS interop-ready. Getting started Gallery Basic usage Featu

null 2 Mar 17, 2022
Import & use javascript libraries in your flutter web projects

Import JS Library Import & use javascript libraries in your flutter web projects. flutter: assets: - assets/howler.js importJsLibrary(url: "./as

Florent CHAMPIGNY 29 Oct 1, 2022
Syntax highlighting for Dart and Flutter

highlight.dart Syntax highlighting for Dart and Flutter, which supports lots of languages and themes. View gallery built with Flutter web Package Vers

GitTouch 181 Jan 8, 2023
A Flutter Package to render Mathematics, Physics and Chemistry Equations based on LaTeX

flutter_tex Contents About Demo Video Screenshots How to use? Android iOS Web Examples Quick Example TeXView Document TeXView Markdown TeXView Quiz Te

Shahzad Akram 220 Dec 21, 2022
Dart equivalent of console.count() in JavaScript

fCount A Dart equivalent for console.count() in JavaScript. You can learn how it works on the web in the MDN docs Available on pub.dev ?? ?? But why U

Chinmay Kabi 2 Jul 31, 2022
Life-saving helpers for working with JavaScript libraries when compiling Dart/Flutter to Web.

dartified Life-saving helpers for working with JavaScript libraries when compiling Dart/Flutter to Web. Features The functions included in this librar

Hammed Oyedele 2 Aug 19, 2022
MXFlutter 是一套使用 TypeScript/JavaScript 来开发 Flutter 应用的框架

MXFlutter 是一套使用 TypeScript/JavaScript 来开发 Flutter 应用的框架。 框架支持两种开发方式 基于 mxflutter-js 前端框架,使用 TypeScript 语言,以类似 Flutter 的 Widget 组

Tencent 719 Jan 3, 2023
A Flutter tool that makes golden testing easy.

???? Alchemist Developed with ?? by Very Good Ventures ?? and Betterment ☀️ . A Flutter tool that makes golden testing easy. Alchemist is a Flutter pa

Betterment 210 Dec 12, 2022