// Package ast defines the Abstract Syntax Tree structures for Mermaid diagrams. // This package mirrors the TypeScript types from mermaid.js for compatibility. package ast // DiagramType represents the type of mermaid diagram type DiagramType string const ( DiagramTypeFlowchart DiagramType = "flowchart" DiagramTypeSequence DiagramType = "sequence" DiagramTypeClassDiagram DiagramType = "classDiagram" DiagramTypeStateDiagram DiagramType = "state" DiagramTypeERDiagram DiagramType = "erDiagram" DiagramTypeUserJourney DiagramType = "journey" DiagramTypeTimeline DiagramType = "timeline" DiagramTypeBlock DiagramType = "block" DiagramTypeGantt DiagramType = "gantt" DiagramTypePie DiagramType = "pie" DiagramTypeQuadrant DiagramType = "quadrantChart" DiagramTypeRequirement DiagramType = "requirementDiagram" DiagramTypeArchitecture DiagramType = "architecture" DiagramTypeNetwork DiagramType = "network" DiagramTypeBPMN DiagramType = "bpmn" DiagramTypeOrganization DiagramType = "organization" ) // Diagram is the base interface for all mermaid diagrams type Diagram interface { Type() DiagramType Validate() error } // FlowVertexTypeParam represents valid vertex types from flow.jison // Corresponds to FlowVertexTypeParam in mermaid.js type FlowVertexTypeParam string const ( VertexTypeSquare FlowVertexTypeParam = "square" VertexTypeDoubleCircle FlowVertexTypeParam = "doublecircle" VertexTypeCircle FlowVertexTypeParam = "circle" VertexTypeEllipse FlowVertexTypeParam = "ellipse" VertexTypeStadium FlowVertexTypeParam = "stadium" VertexTypeSubroutine FlowVertexTypeParam = "subroutine" VertexTypeRect FlowVertexTypeParam = "rect" VertexTypeCylinder FlowVertexTypeParam = "cylinder" VertexTypeRound FlowVertexTypeParam = "round" VertexTypeDiamond FlowVertexTypeParam = "diamond" VertexTypeHexagon FlowVertexTypeParam = "hexagon" VertexTypeOdd FlowVertexTypeParam = "odd" VertexTypeTrapezoid FlowVertexTypeParam = "trapezoid" VertexTypeInvTrapezoid FlowVertexTypeParam = "inv_trapezoid" VertexTypeLeanRight FlowVertexTypeParam = "lean_right" VertexTypeLeanLeft FlowVertexTypeParam = "lean_left" VertexTypeFlag FlowVertexTypeParam = "flag" ) // FlowVertex represents a node in a flowchart // Mirrors the FlowVertex interface from mermaid.js type FlowVertex struct { ID string `json:"id"` Classes []string `json:"classes"` Dir *string `json:"dir,omitempty"` DomID string `json:"domId"` HaveCallback *bool `json:"haveCallback,omitempty"` LabelType string `json:"labelType"` // Always "text" in original Link *string `json:"link,omitempty"` LinkTarget *string `json:"linkTarget,omitempty"` Props map[string]any `json:"props,omitempty"` Styles []string `json:"styles"` Text *string `json:"text,omitempty"` Type *FlowVertexTypeParam `json:"type,omitempty"` Icon *string `json:"icon,omitempty"` Form *string `json:"form,omitempty"` Pos *string `json:"pos,omitempty"` // 't' or 'b' Img *string `json:"img,omitempty"` AssetWidth *int `json:"assetWidth,omitempty"` AssetHeight *int `json:"assetHeight,omitempty"` DefaultWidth *int `json:"defaultWidth,omitempty"` ImageAspectRatio *float64 `json:"imageAspectRatio,omitempty"` Constraint *string `json:"constraint,omitempty"` // "on" or "off" Tooltip *string `json:"tooltip,omitempty"` OnClick *ClickEvent `json:"onClick,omitempty"` } // FlowText represents text content in diagrams type FlowText struct { Text string `json:"text"` Type string `json:"type"` // Always "text" } // FlowEdgeStroke represents edge stroke types type FlowEdgeStroke string const ( StrokeNormal FlowEdgeStroke = "normal" StrokeThick FlowEdgeStroke = "thick" StrokeInvisible FlowEdgeStroke = "invisible" StrokeDotted FlowEdgeStroke = "dotted" ) // FlowEdge represents a connection between vertices // Mirrors the FlowEdge interface from mermaid.js type FlowEdge struct { ID string `json:"id,omitempty"` IsUserDefinedID bool `json:"isUserDefinedId"` Start string `json:"start"` End string `json:"end"` Interpolate *string `json:"interpolate,omitempty"` Type *string `json:"type,omitempty"` Stroke *FlowEdgeStroke `json:"stroke,omitempty"` Style []string `json:"style,omitempty"` Length *int `json:"length,omitempty"` Text string `json:"text"` LabelType string `json:"labelType"` // Always "text" Classes []string `json:"classes"` Animation *string `json:"animation,omitempty"` // "fast" or "slow" Animate *bool `json:"animate,omitempty"` Pattern *string `json:"pattern,omitempty"` Tooltip *string `json:"tooltip,omitempty"` } // FlowClass represents CSS class definitions type FlowClass struct { ID string `json:"id"` Styles []string `json:"styles"` TextStyles []string `json:"textStyles"` } // FlowSubGraph represents a subgraph container type FlowSubGraph struct { ID string `json:"id"` Classes []string `json:"classes"` Dir *string `json:"dir,omitempty"` LabelType string `json:"labelType"` Nodes []string `json:"nodes"` Title string `json:"title"` } // FlowLink represents link connection details type FlowLink struct { Length *int `json:"length,omitempty"` Stroke string `json:"stroke"` Type string `json:"type"` Text *string `json:"text,omitempty"` } // Flowchart represents a complete flowchart diagram // Mirrors the FlowDB state in mermaid.js type Flowchart struct { Direction string `json:"direction,omitempty"` Vertices map[string]*FlowVertex `json:"vertices"` Edges []*FlowEdge `json:"edges"` Classes map[string]*FlowClass `json:"classes"` SubGraphs []*FlowSubGraph `json:"subGraphs"` SubGraphLookup map[string]*FlowSubGraph `json:"subGraphLookup"` Tooltips map[string]string `json:"tooltips"` Version string `json:"version"` // "gen-1" or "gen-2" } // Type returns the diagram type func (f *Flowchart) Type() DiagramType { return DiagramTypeFlowchart } // Validate checks if the flowchart is valid func (f *Flowchart) Validate() error { // Basic validation - ensure all edges reference valid vertices for _, edge := range f.Edges { if _, exists := f.Vertices[edge.Start]; !exists { return NewValidationError("edge references non-existent start vertex: " + edge.Start) } if _, exists := f.Vertices[edge.End]; !exists { return NewValidationError("edge references non-existent end vertex: " + edge.End) } } return nil } // ValidationError represents a diagram validation error type ValidationError struct { Message string } func (e *ValidationError) Error() string { return "validation error: " + e.Message } // NewValidationError creates a new validation error func NewValidationError(message string) *ValidationError { return &ValidationError{Message: message} } // EdgeAnimation represents animation properties for edges type EdgeAnimation struct { Duration string `json:"duration,omitempty"` Delay string `json:"delay,omitempty"` Easing string `json:"easing,omitempty"` } // MarkdownText represents markdown-formatted text type MarkdownText struct { Content string `json:"content"` Bold bool `json:"bold,omitempty"` Italic bool `json:"italic,omitempty"` Strike bool `json:"strike,omitempty"` } // NewFlowchart creates a new flowchart with default values func NewFlowchart() *Flowchart { return &Flowchart{ Vertices: make(map[string]*FlowVertex), Edges: make([]*FlowEdge, 0), Classes: make(map[string]*FlowClass), SubGraphs: make([]*FlowSubGraph, 0), SubGraphLookup: make(map[string]*FlowSubGraph), Tooltips: make(map[string]string), Version: "gen-2", } }