Skip to content

Commit d4cb510

Browse files
committed
fix: use portable arithmetic in validate-mermaid.sh
Replace ((var++)) with var=$((var + 1)) to avoid exit code 1 when incrementing from 0 with set -e enabled.
1 parent 344b402 commit d4cb510

1 file changed

Lines changed: 10 additions & 25 deletions

File tree

scripts/validate-mermaid.sh

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
#!/bin/bash
2-
# Validate Mermaid diagrams in markdown files and standalone .mmd files
3-
# Extracts inline ```mermaid blocks and validates with mermaid-cli (mmdc)
4-
52
set -e
63

74
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
85
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
96
TEMP_DIR=$(mktemp -d)
107
trap "rm -rf $TEMP_DIR" EXIT
118

12-
# Colors for output
139
RED='\033[0;31m'
1410
GREEN='\033[0;32m'
1511
YELLOW='\033[1;33m'
16-
NC='\033[0m' # No Color
12+
NC='\033[0m'
1713

1814
echo "Validating Mermaid diagrams..."
1915
echo ""
2016

21-
# Check if mmdc is installed
2217
if ! command -v mmdc &> /dev/null; then
2318
echo -e "${YELLOW}Warning: mermaid-cli (mmdc) is not installed${NC}"
2419
echo "Install with: npm install -g @mermaid-js/mermaid-cli"
@@ -32,7 +27,6 @@ fi
3227
ERRORS=0
3328
TOTAL=0
3429

35-
# Function to extract and validate mermaid blocks from a file
3630
validate_markdown_file() {
3731
local file="$1"
3832
local block_num=0
@@ -42,7 +36,7 @@ validate_markdown_file() {
4236
local block_start_line=0
4337

4438
while IFS= read -r line || [[ -n "$line" ]]; do
45-
((line_num++))
39+
line_num=$((line_num + 1))
4640

4741
if [[ "$line" =~ ^\`\`\`mermaid ]]; then
4842
in_mermaid=true
@@ -54,33 +48,27 @@ validate_markdown_file() {
5448
if [[ "$in_mermaid" == true ]]; then
5549
if [[ "$line" =~ ^\`\`\` ]]; then
5650
in_mermaid=false
57-
((block_num++))
58-
((TOTAL++))
51+
block_num=$((block_num + 1))
52+
TOTAL=$((TOTAL + 1))
5953

60-
# Write to temp file and validate
6154
local temp_file="$TEMP_DIR/diagram_${block_num}.mmd"
6255
echo "$mermaid_content" > "$temp_file"
6356

6457
if [[ "$MMDC_AVAILABLE" == true ]]; then
65-
# Use mmdc for full validation
6658
if mmdc -i "$temp_file" -o "$TEMP_DIR/output.svg" 2>"$TEMP_DIR/error.log"; then
6759
echo -e "${GREEN}${NC} $file:$block_start_line (block $block_num)"
6860
else
6961
echo -e "${RED}${NC} $file:$block_start_line (block $block_num)"
70-
echo " Error: $(cat "$TEMP_DIR/error.log" | head -3)"
71-
((ERRORS++))
62+
echo " Error: $(head -3 "$TEMP_DIR/error.log")"
63+
ERRORS=$((ERRORS + 1))
7264
fi
7365
else
74-
# Basic syntax validation without mmdc
75-
# Check for common syntax issues
7666
local has_error=false
77-
78-
# Check if diagram type is specified
7967
if ! echo "$mermaid_content" | head -1 | grep -qE '^[[:space:]]*(flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|gantt|pie|gitGraph|journey|mindmap|timeline|quadrantChart|sankey|xychart|block)'; then
8068
echo -e "${RED}${NC} $file:$block_start_line (block $block_num)"
8169
echo " Error: No valid diagram type found on first line"
8270
has_error=true
83-
((ERRORS++))
71+
ERRORS=$((ERRORS + 1))
8472
fi
8573

8674
if [[ "$has_error" == false ]]; then
@@ -94,22 +82,21 @@ validate_markdown_file() {
9482
done < "$file"
9583
}
9684

97-
# Find and validate standalone .mmd files
9885
echo "=== Standalone .mmd files ==="
9986
MMD_FILES=$(find "$REPO_ROOT" -name "*.mmd" -not -path "*/node_modules/*" -not -path "*/.git/*" 2>/dev/null || true)
10087

10188
if [ -z "$MMD_FILES" ]; then
10289
echo "No standalone .mmd files found"
10390
else
10491
for file in $MMD_FILES; do
105-
((TOTAL++))
92+
TOTAL=$((TOTAL + 1))
10693
if [[ "$MMDC_AVAILABLE" == true ]]; then
10794
if mmdc -i "$file" -o "$TEMP_DIR/output.svg" 2>"$TEMP_DIR/error.log"; then
10895
echo -e "${GREEN}${NC} $file"
10996
else
11097
echo -e "${RED}${NC} $file"
111-
echo " Error: $(cat "$TEMP_DIR/error.log" | head -3)"
112-
((ERRORS++))
98+
echo " Error: $(head -3 "$TEMP_DIR/error.log")"
99+
ERRORS=$((ERRORS + 1))
113100
fi
114101
else
115102
echo -e "${YELLOW}?${NC} $file - skipped (mmdc not available)"
@@ -120,14 +107,12 @@ fi
120107
echo ""
121108
echo "=== Inline mermaid blocks in markdown ==="
122109

123-
# Find all markdown files and extract mermaid blocks
124110
MD_FILES=$(find "$REPO_ROOT" -name "*.md" -not -path "*/node_modules/*" -not -path "*/.git/*" 2>/dev/null || true)
125111

126112
if [ -z "$MD_FILES" ]; then
127113
echo "No markdown files found"
128114
else
129115
for file in $MD_FILES; do
130-
# Check if file contains mermaid blocks
131116
if grep -q '```mermaid' "$file" 2>/dev/null; then
132117
validate_markdown_file "$file"
133118
fi

0 commit comments

Comments
 (0)