mermaid

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package mermaid provides parsing and conversion of Mermaid diagrams to D2.

Package mermaid provides parsing and conversion of Mermaid diagrams to D2.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConverter

func NewConverter() convert.Converter

NewConverter creates a new Mermaid converter.

Types

type Actor

type Actor struct {
	ID    string
	Label string
	Shape string // participant, actor
	Line  int
}

Actor represents a participant in a sequence diagram.

type ArrowType

type ArrowType string

ArrowType represents the type of arrow on an edge.

const (
	ArrowNormal ArrowType = "normal"
	ArrowOpen   ArrowType = "open"
	ArrowCircle ArrowType = "circle"
	ArrowCross  ArrowType = "cross"
	ArrowBidir  ArrowType = "bidir"
	ArrowNone   ArrowType = "none"
)

type Class

type Class struct {
	ID         string
	Label      string
	Attributes []string
	Methods    []string
	Line       int
}

Class represents a class in a class diagram.

type ClassConverter

type ClassConverter struct{}

ClassConverter converts Mermaid class diagrams to DiagramSpec.

func (*ClassConverter) Convert

func (c *ClassConverter) Convert(doc *Document) *generate.DiagramSpec

Convert transforms a parsed Mermaid class document into a DiagramSpec.

type Converter

type Converter struct{}

Converter implements the convert.Converter interface for Mermaid diagrams.

func (*Converter) Convert

func (c *Converter) Convert(source string) (*convert.ConversionResult, error)

Convert transforms Mermaid source into a DiagramSpec.

func (*Converter) Format

func (c *Converter) Format() convert.SourceFormat

Format returns the source format.

func (*Converter) Lint

func (c *Converter) Lint(source string) (*convert.LintResult, error)

Lint analyzes Mermaid source for D2 compatibility.

type DiagramType

type DiagramType string

DiagramType represents the type of Mermaid diagram.

const (
	DiagramFlowchart DiagramType = "flowchart"
	DiagramSequence  DiagramType = "sequence"
	DiagramClass     DiagramType = "class"
	DiagramState     DiagramType = "state"
	DiagramER        DiagramType = "er"
	DiagramGantt     DiagramType = "gantt"
	DiagramPie       DiagramType = "pie"
	DiagramGitGraph  DiagramType = "gitGraph"
	DiagramUnknown   DiagramType = "unknown"
)

type Direction

type Direction string

Direction represents the layout direction.

const (
	DirectionTB Direction = "TB" // Top to Bottom
	DirectionTD Direction = "TD" // Top to Down (same as TB)
	DirectionBT Direction = "BT" // Bottom to Top
	DirectionRL Direction = "RL" // Right to Left
	DirectionLR Direction = "LR" // Left to Right
)

func (Direction) ToD2Direction

func (d Direction) ToD2Direction() string

ToD2Direction converts Mermaid direction to D2 direction.

type Document

type Document struct {
	Type      DiagramType
	Direction Direction
	Nodes     []*Node
	Edges     []*Edge
	Subgraphs []*Subgraph

	// Sequence diagram specific
	Actors   []*Actor
	Messages []*Message
	Groups   []*MessageGroup

	// Class diagram specific
	Classes []*Class

	// Raw lines for diagnostics
	Lines []string
}

Document represents a parsed Mermaid document.

func Parse

func Parse(source string) (*Document, error)

Parse parses Mermaid source into a Document AST.

type Edge

type Edge struct {
	From  string
	To    string
	Label string
	Style EdgeStyle
	Line  int // Source line number
}

Edge represents a connection between nodes.

type EdgeStyle

type EdgeStyle struct {
	Dashed    bool
	Thick     bool
	Invisible bool
	ArrowType ArrowType
}

EdgeStyle represents the visual style of an edge.

type FlowchartConverter

type FlowchartConverter struct{}

FlowchartConverter converts Mermaid flowcharts to DiagramSpec.

func (*FlowchartConverter) Convert

func (c *FlowchartConverter) Convert(doc *Document) *generate.DiagramSpec

Convert transforms a parsed Mermaid flowchart document into a DiagramSpec.

type GroupType

type GroupType string

GroupType represents the type of message group.

const (
	GroupAlt      GroupType = "alt"
	GroupOpt      GroupType = "opt"
	GroupLoop     GroupType = "loop"
	GroupPar      GroupType = "par"
	GroupCritical GroupType = "critical"
	GroupBreak    GroupType = "break"
)

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes Mermaid source code.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the given input.

func (*Lexer) Tokenize

func (l *Lexer) Tokenize() []Token

Tokenize converts the input into tokens.

type Linter

type Linter struct{}

Linter analyzes Mermaid source for D2 compatibility.

func (*Linter) Lint

func (l *Linter) Lint(source string) *convert.LintResult

Lint analyzes the source code for unsupported features.

type Message

type Message struct {
	From  string
	To    string
	Label string
	Style MessageStyle
	Line  int
}

Message represents a message in a sequence diagram.

type MessageGroup

type MessageGroup struct {
	Type     GroupType
	Label    string
	Messages []*Message
	Else     []*Message // For alt/else groups
	Line     int
}

MessageGroup represents a group of messages (alt, opt, loop, etc.).

type MessageStyle

type MessageStyle string

MessageStyle represents the style of a sequence message.

const (
	MessageSolid  MessageStyle = "solid"
	MessageDashed MessageStyle = "dashed"
	MessageAsync  MessageStyle = "async"
)

type Node

type Node struct {
	ID    string
	Label string
	Shape NodeShape
	Line  int // Source line number
}

Node represents a Mermaid node.

type NodeShape

type NodeShape string

NodeShape represents the shape of a node.

const (
	ShapeRectangle     NodeShape = "rectangle"
	ShapeRoundedRect   NodeShape = "rounded"
	ShapeCircle        NodeShape = "circle"
	ShapeDiamond       NodeShape = "diamond"
	ShapeCylinder      NodeShape = "cylinder"
	ShapeHexagon       NodeShape = "hexagon"
	ShapeParallelogram NodeShape = "parallelogram"
	ShapeTrapezoid     NodeShape = "trapezoid"
	ShapeStadium       NodeShape = "stadium"
	ShapeSubroutine    NodeShape = "subroutine"
	ShapeAsymmetric    NodeShape = "asymmetric"
	ShapeDouble        NodeShape = "double"
)

func (NodeShape) ToD2Shape

func (s NodeShape) ToD2Shape() string

ToD2Shape converts Mermaid shape to D2 shape.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses Mermaid source code into an AST.

func NewParser

func NewParser(tokens []Token) *Parser

NewParser creates a new parser for the given tokens.

func (*Parser) Parse

func (p *Parser) Parse() (*Document, error)

Parse parses the tokens into a Document.

type SequenceConverter

type SequenceConverter struct{}

SequenceConverter converts Mermaid sequence diagrams to DiagramSpec.

func (*SequenceConverter) Convert

func (c *SequenceConverter) Convert(doc *Document) *generate.DiagramSpec

Convert transforms a parsed Mermaid sequence document into a DiagramSpec.

type Subgraph

type Subgraph struct {
	ID        string
	Label     string
	Direction Direction
	Nodes     []*Node
	Edges     []*Edge
	Subgraphs []*Subgraph
	Line      int // Source line number
}

Subgraph represents a container in Mermaid.

type Token

type Token struct {
	Type  TokenType
	Value string
	Line  int
	Col   int
}

Token represents a lexical token.

type TokenType

type TokenType int

TokenType represents the type of a lexical token.

const (
	TokenEOF TokenType = iota
	TokenNewline
	TokenWhitespace
	TokenIdent
	TokenString       // "quoted string"
	TokenText         // Text in brackets/braces
	TokenArrow        // -->, -.->. ==>, etc.
	TokenPipe         // |
	TokenColon        // :
	TokenSemicolon    // ;
	TokenOpenParen    // (
	TokenCloseParen   // )
	TokenOpenBrace    // {
	TokenCloseBrace   // }
	TokenOpenBracket  // [
	TokenCloseBracket // ]
	TokenComment      // %% comment
	TokenDirective    // %%{...}%%
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL