Sign in

    This version of the module is deprecated: Moved to moonbit-community/diago. Use moonbit-community/diago@0.4.0 or later and update import paths.

    diago

    A D2-compatible diagram toolkit for MoonBit with Dagre, ELK, and Railway layout engines, rendering SVG, ASCII, and Unicode outputs.

    graph
    Download zip
    Author
    Version
    0.2.1
    License
    Apache-2.0
    Last updated
    7 months ago
    Downloads
    43K

    #Diago

    Diago is a MoonBit implementation of the D2 diagram language. It compiles .d2 files into SVG (and optionally ASCII/Unicode text) using multiple layout engines.

    #Overview

    This repository contains:

    • A CLI (cmd/diago) with explicit subcommands (render, fmt, validate, layout, themes, version)
    • A WASM-based playground (web/) deployed via GitHub Pages
    • Multiple layout engines: dagre, elk, and railway

    #Installation

    moon update moon build

    #Quick Start

    moon run cmd/diago -- render diagram.d2

    #CLI

    Show help:

    moon run cmd/diago -- --help

    Common usage:

    # Render SVG (default output: input.svg) moon run cmd/diago -- render diagram.d2 moon run cmd/diago -- render diagram.d2 diagram.svg moon run cmd/diago -- render diagram.d2 --output diagram.svg # Choose layout engine moon run cmd/diago -- render --layout elk diagram.d2 diagram.svg moon run cmd/diago -- render -l dagre diagram.d2 diagram.svg # ASCII / Unicode text moon run cmd/diago -- render --format ascii diagram.d2 --output diagram.ascii.txt moon run cmd/diago -- render --format unicode diagram.d2 --output diagram.unicode.txt # Format / validate moon run cmd/diago -- fmt diagram.d2 moon run cmd/diago -- fmt --check diagram.d2 moon run cmd/diago -- validate diagram.d2 # Watch mode moon run cmd/diago -- render --watch diagram.d2 # Introspection moon run cmd/diago -- layout moon run cmd/diago -- layout elk moon run cmd/diago -- themes moon run cmd/diago -- version

    #Example

    Create a file example.d2:

    server: Web Server database: Database { shape: cylinder } cache: Cache { shape: oval } server -> database: queries server -> cache: reads cache -> database: fallback

    Compile it:

    moon run cmd/diago -- render example.d2 example.svg

    #Pipeline

    At a high level:

    D2 Source → Lexer → Parser → AST → IR → Graph → Layout (dagre/elk/railway) → Render (SVG/ASCII/Unicode)

    #Tests

    moon test # Run all tests moon test -v # Verbose output

    #License

    Apache-2.0 (see LICENSE).

    DiagoError

    pub(all) suberror DiagoError {
    ParseError(String)
    IOError(String)
    IrError(String)
    GraphError(String)
    LayoutError(String)
    RenderError(String)
    ConfigError(String)
    TargetError(String)
    }

    impl Eq for DiagoError
    impl Show for DiagoError

    CompileOptions

    pub struct CompileOptions {
    output_mode : OutputMode
    layout_engine : LayoutEngine
    direction :
    Direction

    target_spec : String?
    layout_plugin : LayoutPlugin?
    route_plugin : RoutePlugin?
    parse_options : ParseOptions
    theme_name : String?
    dark_theme_id : Int?
    theme_overrides : Map[String, String]
    dark_theme_overrides : Map[String, String]
    scale : Double?
    no_xml_tag : Bool?
    salt : String?
    omit_version : Bool?
    }

    CompileOptions::new

    CompileOptions::with_dark_theme_id

    fn CompileOptions::with_dark_theme_id(self : CompileOptions, dark_theme_id : Int?) -> CompileOptions

    CompileOptions::with_dark_theme_overrides

    fn CompileOptions::with_dark_theme_overrides(self : CompileOptions, dark_theme_overrides : Map[String, String]) -> CompileOptions

    CompileOptions::with_direction

    CompileOptions::with_layout_engine

    fn CompileOptions::with_layout_engine(self : CompileOptions, layout_engine : LayoutEngine) -> CompileOptions

    CompileOptions::with_layout_plugin

    fn CompileOptions::with_layout_plugin(self : CompileOptions, layout_plugin : LayoutPlugin?) -> CompileOptions

    CompileOptions::with_no_xml_tag

    fn CompileOptions::with_no_xml_tag(self : CompileOptions, no_xml_tag : Bool?) -> CompileOptions

    CompileOptions::with_omit_version

    fn CompileOptions::with_omit_version(self : CompileOptions, omit_version : Bool?) -> CompileOptions

    CompileOptions::with_output_mode

    fn CompileOptions::with_output_mode(self : CompileOptions, output_mode : OutputMode) -> CompileOptions

    CompileOptions::with_parse_options

    fn CompileOptions::with_parse_options(self : CompileOptions, parse_options : ParseOptions) -> CompileOptions

    CompileOptions::with_route_plugin

    fn CompileOptions::with_route_plugin(self : CompileOptions, route_plugin : RoutePlugin?) -> CompileOptions

    CompileOptions::with_salt

    fn CompileOptions::with_salt(self : CompileOptions, salt : String?) -> CompileOptions

    CompileOptions::with_scale

    fn CompileOptions::with_scale(self : CompileOptions, scale : Double?) -> CompileOptions

    CompileOptions::with_target_spec

    fn CompileOptions::with_target_spec(self : CompileOptions, target_spec : String?) -> CompileOptions

    CompileOptions::with_theme_name

    fn CompileOptions::with_theme_name(self : CompileOptions, theme_name : String?) -> CompileOptions

    CompileOptions::with_theme_overrides

    fn CompileOptions::with_theme_overrides(self : CompileOptions, theme_overrides : Map[String, String]) -> CompileOptions

    CompletionItem

    pub struct CompletionItem {
    label : String
    kind : CompletionKind
    detail : String
    insert_text : String
    }

    CompletionKind

    pub(all) enum CompletionKind {
    Keyword
    Style
    Shape
    Value
    }

    LayoutEngine

    pub(all) enum LayoutEngine {
    Auto
    Dagre
    Elk
    Railway
    }

    impl Eq for LayoutEngine

    OutputMode

    pub(all) enum OutputMode {
    Svg
    Ascii
    Unicode
    }

    impl Eq for OutputMode
    impl Show for OutputMode

    ParseOptions

    pub struct ParseOptions {
    base_path : String?
    import_resolver :
    ImportResolver
    ?
    }

    ParseOptions::new

    ParseOptions::with_base_path

    fn ParseOptions::with_base_path(self : ParseOptions, base_path : String?) -> ParseOptions

    ParseOptions::with_import_resolver

    fn ParseOptions::with_import_resolver(self : ParseOptions, import_resolver :
    ImportResolver
    ?) -> ParseOptions

    RoutePlugin

    RoutePlugin::inner

    #deprecated("Use `struct T(A)` to declare a newtype and use `.0` access the underlying type instead.")
    fn RoutePlugin::inner(self : RoutePlugin) -> ((
    GraphInput
    ) -> Result[
    GraphInput
    , String])
    Convert newtype to its underlying type, automatically derived.

    compile

    fn compile(source : String, options? : CompileOptions) -> String raise DiagoError

    compile_diagram

    fn compile_diagram(source : String, options? : CompileOptions) ->
    Diagram
    raise DiagoError

    compile_file

    fn compile_file(file_path : String, options? : CompileOptions) -> String raise DiagoError

    get_board_path_at_position

    fn get_board_path_at_position(source : String, line : Int, column : Int) -> Array[String]

    get_completion_items

    fn get_completion_items(source : String, line : Int, column : Int) -> Array[CompletionItem]

    get_ref_ranges

    fn get_ref_ranges(source : String, key : String, board_path? : Array[String], options? : ParseOptions) -> Array[
    Range
    ] raise DiagoError

    parse

    fn parse(source : String, options? : ParseOptions) ->
    Map
    raise DiagoError

    parse_file

    fn parse_file(file_path : String, options? : ParseOptions) ->
    Map
    raise DiagoError