Skip to content

Commit

Permalink
subgraphs in mermaid
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Jul 22, 2024
1 parent 274cbb8 commit 95cd1ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ g := dot.NewGraph(dot.Directed)
fmt.Println(dot.MermaidGraph(g, dot.MermaidTopToBottom))
```

### subgraphs in mermaid

```mermaid
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;
```

## extensions

See also package `dot/dotx` for types that can help in constructing complex graphs.
Expand Down
12 changes: 11 additions & 1 deletion mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ func diagram(g *Graph, diagramType string, orientation int) string {
sb.WriteString("TD")
}
writeEnd(sb)
diagramGraph(g, sb)
for _, id := range g.sortedSubgraphsKeys() {
each := g.subgraphs[id]
fmt.Fprintf(sb, "subgraph %s;\n", id)
diagramGraph(each, sb)
fmt.Fprintln(sb, "end;")
}
return sb.String()
}

func diagramGraph(g *Graph, sb *strings.Builder) {
// graph nodes
for _, key := range g.sortedNodesKeys() {
nodeShape := MermaidShapeRound
Expand Down Expand Up @@ -92,7 +103,6 @@ func diagram(g *Graph, diagramType string, orientation int) string {
}
}
}
return sb.String()
}

func writeEnd(sb *strings.Builder) {
Expand Down
18 changes: 18 additions & 0 deletions mermaid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,21 @@ func TestUndirectedMermaid(t *testing.T) {
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
}
}

// example from https://mermaid.js.org/syntax/flowchart.html
// note that c1 and a2 are nodes created in their subgraphs to make the diagram match with the example.
func TestMermaidSubgraph(t *testing.T) {
di := NewGraph(Directed)
sub1 := di.Subgraph("one")
sub1.Node("a1").Edge(sub1.Node("a2"))
sub2 := di.Subgraph("two")
sub2.Node("b1").Edge(sub2.Node("b2"))
sub3 := di.Subgraph("three")
sub3.Node("c1").Edge(sub3.Node("c2"))

sub3.Node("c1").Edge(sub1.Node("a2"))
mf := MermaidFlowchart(di, MermaidLeftToRight)
if got, want := flatten(mf), `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;`; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
}

0 comments on commit 95cd1ec

Please sign in to comment.