Skip to content

Commit

Permalink
add link attribute for mermaid
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Nov 14, 2024
1 parent c2eeec0 commit 4c0e6f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ fmt.Println(dot.MermaidGraph(g, dot.MermaidTopToBottom))
flowchart LR;n8-->n3;subgraph one;n2("a1");n3("a2");n2-->n3;end;subgraph three;n8("c1");n9("c2");n8-->n9;end;subgraph two;n5("b1");n6("b2");n5-->n6;end;
```

### mermaid specific attributes

|attr|type|description|
|----|----|-----------|
|link|Edge|examples are {-->,-.->,--x,o--o}|
|shape|Node|examples are {MermaidShapeRound,MermaidShapeCircle,MermaidShapeTrapezoid}
|style|Node|example is fill:#90EE90|

## extensions

See also package `dot/dotx` for types that can help in constructing complex graphs.
Expand Down
33 changes: 21 additions & 12 deletions mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ const (
)

var (
MermaidShapeRound = shape{"(", ")"}
MermaidShapeStadium = shape{"([", "])"}
MermaidShapeSubroutine = shape{"[[", "]]"}
MermaidShapeCylinder = shape{"[(", ")]"}
MermaidShapeCirle = shape{"((", "))"} // Deprecated: use MermaidShapeCircle instead
MermaidShapeCircle = shape{"((", "))"}
MermaidShapeAsymmetric = shape{">", "]"}
MermaidShapeRhombus = shape{"{", "}"}
MermaidShapeTrapezoid = shape{"[/", "\\]"}
MermaidShapeTrapezoidAlt = shape{"[\\", "/]"}
MermaidShapeRound = shape{"(", ")"}
MermaidShapeStadium = shape{"([", "])"}
MermaidShapeSubroutine = shape{"[[", "]]"}
MermaidShapeCylinder = shape{"[(", ")]"}
MermaidShapeCirle = shape{"((", "))"} // Deprecated: use MermaidShapeCircle instead
MermaidShapeCircle = shape{"((", "))"}
MermaidShapeAsymmetric = shape{">", "]"}
MermaidShapeRhombus = shape{"{", "}"}
MermaidShapeTrapezoid = shape{"[/", "\\]"}
MermaidShapeTrapezoidAlt = shape{"[\\", "/]"}
MermaidShapeHexagon = shape{"[{{", "}}]"}
MermaidShapeParallelogram = shape{"[/", "/]"}
MermaidShapeParallelogramAlt = shape{"[\\", "\\]"}
// TODO more shapes see https://mermaid.js.org/syntax/flowchart.html#node-shapes
)

type shape struct {
Expand Down Expand Up @@ -96,10 +100,15 @@ func diagramGraph(g *Graph, sb *strings.Builder) {
for _, each := range g.sortedEdgesFromKeys() {
all := g.edgesFrom[each]
for _, each := range all {
// The edge can override the link style
link := denoteEdge
if l := each.GetAttr("link"); l != nil {
link = l.(string)
}
if label := each.GetAttr("label"); label != nil {
fmt.Fprintf(sb, "\tn%d%s|%s|n%d;\n", each.from.seq, denoteEdge, escape(label.(string)), each.to.seq)
fmt.Fprintf(sb, "\tn%d%s|%s|n%d;\n", each.from.seq, link, escape(label.(string)), each.to.seq)
} else {
fmt.Fprintf(sb, "\tn%d%sn%d;\n", each.from.seq, denoteEdge, each.to.seq)
fmt.Fprintf(sb, "\tn%d%sn%d;\n", each.from.seq, link, each.to.seq)
}
}
}
Expand Down

0 comments on commit 4c0e6f9

Please sign in to comment.