Skip to content

Commit c0923a5

Browse files
NCMBianchiclaude
andcommitted
RegEx fix for non-standard arrow patterns
Changed: - cmd/parse.go: replaced hardcoded "-->" with 'edgeArrowPattern' array of non-standard operators - cmd/parse_test.go: new tests for non-standard operators Co-Authored-By: Claude Sonnet 5.0 <noreply@anthropic.com>
1 parent b1b35f6 commit c0923a5

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

cmd/parse.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ func setData(parent textNode, edge textEdge, data *orderedmap.OrderedMap[string,
202202
}
203203
}
204204

205+
// edgeArrowPattern matches the classic arrow (-->) plus the dotted, thick,
206+
// open, circle, and cross link operators Mermaid also supports (-.->, ==>,
207+
// ---, --o, --x).
208+
const edgeArrowPattern = `(?:-->|-\.->|==>|---|--o|--x)`
209+
205210
func (gp *graphProperties) parseString(line string) ([]textNode, error) {
206211
log.Debugf("Parsing line: %v", line)
207212
var lhs, rhs []textNode
@@ -243,7 +248,7 @@ func (gp *graphProperties) parseString(line string) ([]textNode, error) {
243248
},
244249
},
245250
{
246-
regex: regexp.MustCompile(`(?s)^(.+)\s*-->\s*\|(.+)\|\s*(.+)$`),
251+
regex: regexp.MustCompile(`(?s)^(.+)\s*` + edgeArrowPattern + `\s*\|(.+)\|\s*(.+)$`),
247252
handler: func(match []string) ([]textNode, error) {
248253
if lhs, err = gp.parseString(match[0]); err != nil {
249254
lhs = []textNode{parseNode(match[0])}
@@ -255,7 +260,7 @@ func (gp *graphProperties) parseString(line string) ([]textNode, error) {
255260
},
256261
},
257262
{
258-
regex: regexp.MustCompile(`(?s)^(.+)\s*-->\s*(.+)$`),
263+
regex: regexp.MustCompile(`(?s)^(.+)\s*` + edgeArrowPattern + `\s*(.+)$`),
259264
handler: func(match []string) ([]textNode, error) {
260265
if lhs, err = gp.parseString(match[0]); err != nil {
261266
lhs = []textNode{parseNode(match[0])}

cmd/parse_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ func TestMermaidFileToMapUsesLatestExplicitLabel(t *testing.T) {
175175
}
176176
}
177177

178+
// Tests non-standard arrow operators.
179+
func TestMermaidFileToMapParsesNonStandardEdgeOperators(t *testing.T) {
180+
for _, op := range []string{"-->", "-.->", "==>", "---", "--o", "--x"} {
181+
t.Run(op, func(t *testing.T) {
182+
properties, err := mermaidFileToMap("graph LR\nA "+op+" B", "cli")
183+
if err != nil {
184+
t.Fatalf("mermaidFileToMap() error = %v", err)
185+
}
186+
edges, ok := properties.data.Get("A")
187+
if !ok || len(edges) != 1 || edges[0].child.name != "B" {
188+
t.Fatalf("edges from A = %#v, want one edge to B", edges)
189+
}
190+
})
191+
}
192+
}
193+
194+
func TestMermaidFileToMapParsesChainedNonStandardEdges(t *testing.T) {
195+
input := "graph LR\nA -.-> B\nB ==> C\nC --- D\nD --o E\nE --x F"
196+
properties, err := mermaidFileToMap(input, "cli")
197+
if err != nil {
198+
t.Fatalf("mermaidFileToMap() error = %v", err)
199+
}
200+
201+
for _, link := range [][2]string{{"A", "B"}, {"B", "C"}, {"C", "D"}, {"D", "E"}, {"E", "F"}} {
202+
edges, ok := properties.data.Get(link[0])
203+
if !ok || len(edges) != 1 || edges[0].child.name != link[1] {
204+
t.Fatalf("edges from %q = %#v, want one edge to %q", link[0], edges, link[1])
205+
}
206+
}
207+
}
208+
178209
// TestGraphTypeDetection verifies that the diagram declaration line is parsed
179210
// tolerantly: surrounding whitespace, a missing direction (defaults to
180211
// top-down), and the reverse directions RL/BT are all accepted.

0 commit comments

Comments
 (0)