This document describes the implementation of nested subgraph support in Ferrite's flowchart renderer. Mermaid supports subgraphs that can contain other subgraphs, creating a hierarchical structure.
Added nested_subgraph_margin configuration (10.0 pixels by default) to create proper visual separation between parent and child subgraph boundaries.
When computing subgraph bounds in compute_subgraph_layouts():
- Child subgraph bounds include extra margin around them
- Parent subgraphs with nested children use increased effective padding
- This prevents parent and child borders from appearing too close together
The compute_subgraph_depths() function computes actual nesting depth:
- Depth 0 = top-level subgraph (no parent)
- Depth 1 = child of top-level
- Depth 2 = grandchild, etc.
This enables proper alternating fill colors for visual distinction between nesting levels.
Infrastructure added to support per-subgraph direction overrides:
FlowGraph.subgraph_directionsstores direction overrides from parsingSubgraphLayoutEngineuses effective direction for each subgraph- Example: A TD flowchart can have a subgraph with
direction LR
src/markdown/mermaid/flowchart.rs
├── FlowLayoutConfig
│ └── nested_subgraph_margin: f32 // Extra margin between nested boundaries
├── FlowGraph
│ └── subgraph_directions: HashMap<String, FlowDirection> // Direction overrides
├── SubgraphLayoutEngine
│ ├── layout_subgraph() - Uses effective direction per subgraph
│ ├── layout_simple_subgraph() - Accepts direction parameter
│ └── layout_hierarchical_subgraph() - Accepts direction parameter
├── compute_subgraph_layouts() - Adds nested margins to bounds
└── compute_subgraph_depths() - Calculates actual nesting depth
| Parameter | Value | Description |
|---|---|---|
subgraph_padding |
15.0 | Standard padding around subgraph content |
subgraph_title_height |
24.0 | Height reserved for title |
nested_subgraph_margin |
10.0 | Extra margin between nested boundaries |
See test_md/test_flowcharts.md for nested subgraph test cases:
- True Nested Subgraphs - Parent-child relationship
- Deeply Nested Subgraphs - 3 levels deep
- Nested with Direction Override - Child uses different direction
-
Internal Layouts Not Applied: The
SubgraphLayoutEnginecomputes internal layouts for subgraphs, but these aren't currently applied to actual node positions. Nodes are positioned by the global Sugiyama algorithm. -
Direction Override Visual Effect: While direction overrides are parsed and stored, the visual effect is limited because internal layouts aren't applied to final positions.
- Apply internal subgraph layouts to node positions
- Support treating subgraphs as "super-nodes" in the global layout
- Improve edge routing through multiple nested levels
flowchart-subgraph-title.md- Title width expansion (Task 55)subgraph-internal-layout.md- Internal positioning