Skip to content

Commit 6707927

Browse files
Merge pull request #27 from justrhysism/fix-comment-handling
Fix comment handling in mermaid parser
2 parents 0a4a780 + 62e8c99 commit 6707927

3 files changed

Lines changed: 78 additions & 4 deletions

File tree

cmd/parse.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ type textEdge struct {
2828
}
2929

3030
func parseNode(line string) textNode {
31+
// Trim any whitespace from the line that might be left after comment removal
32+
trimmedLine := strings.TrimSpace(line)
33+
3134
nodeWithClass, _ := regexp.Compile(`^(.+):::(.+)$`)
3235

33-
if match := nodeWithClass.FindStringSubmatch(line); match != nil {
34-
return textNode{match[1], match[2]}
36+
if match := nodeWithClass.FindStringSubmatch(trimmedLine); match != nil {
37+
return textNode{strings.TrimSpace(match[1]), strings.TrimSpace(match[2])}
3538
} else {
36-
return textNode{line, ""}
39+
return textNode{trimmedLine, ""}
3740
}
3841
}
3942

@@ -167,7 +170,32 @@ func (gp *graphProperties) parseString(line string) ([]textNode, error) {
167170
func mermaidFileToMap(mermaid, styleType string) (*graphProperties, error) {
168171
// Allow split on both \n and the actual string "\n" for curl compatibility
169172
newlinePattern := regexp.MustCompile(`\n|\\n`)
170-
lines := newlinePattern.Split(string(mermaid), -1)
173+
rawLines := newlinePattern.Split(string(mermaid), -1)
174+
175+
// Process lines to remove comments
176+
lines := []string{}
177+
for _, line := range rawLines {
178+
// Stop processing at "---" separator (used in test files)
179+
if line == "---" {
180+
break
181+
}
182+
183+
// Skip lines that start with %% (comment lines)
184+
if strings.HasPrefix(strings.TrimSpace(line), "%%") {
185+
continue
186+
}
187+
188+
// Remove inline comments (anything after %%) and trim resulting whitespace
189+
if idx := strings.Index(line, "%%"); idx != -1 {
190+
line = strings.TrimSpace(line[:idx])
191+
}
192+
193+
// Skip empty lines after comment removal
194+
if len(strings.TrimSpace(line)) > 0 {
195+
lines = append(lines, line)
196+
}
197+
}
198+
171199
data := orderedmap.NewOrderedMap[string, []textEdge]()
172200
styleClasses := make(map[string]styleClass)
173201
properties := graphProperties{data, &styleClasses, "", styleType}

cmd/testdata/ascii/comments.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
graph LR
2+
%% This is a comment
3+
A --> B
4+
%% Another comment
5+
B --> C
6+
A --> C
7+
%% Final comment
8+
---
9+
+---+ +---+
10+
| | | |
11+
| A |---->| B |
12+
| | | |
13+
+---+ +---+
14+
| |
15+
| |
16+
| |
17+
| |
18+
| v
19+
| +---+
20+
| | |
21+
+------>| C |
22+
| |
23+
+---+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
graph LR
2+
%% This is a comment
3+
A --> B
4+
%% Another comment
5+
B --> C
6+
A --> C
7+
%% Final comment
8+
---
9+
┌───┐ ┌───┐
10+
│ │ │ │
11+
│ A ├────►│ B │
12+
│ │ │ │
13+
└─┬─┘ └─┬─┘
14+
│ │
15+
│ │
16+
│ │
17+
│ │
18+
│ ▼
19+
│ ┌───┐
20+
│ │ │
21+
└──────►│ C │
22+
│ │
23+
└───┘

0 commit comments

Comments
 (0)