MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / validate_mermaid_diagrams

Function validate_mermaid_diagrams

codewiki/src/be/utils.py:66–109  ·  view source on GitHub ↗

Validate all Mermaid diagrams in a markdown file. Args: md_file_path: Path to the markdown file to check relative_path: Relative path to the markdown file Returns: "All mermaid diagrams are syntax correct" if all diagrams are valid, otherwise returns

(md_file_path: str, relative_path: str)

Source from the content-addressed store, hash-verified

64# ------------------------------------------------------------
65# ---------------------- Mermaid Validation -----------------
66# ------------------------------------------------------------
67
68
69async def validate_mermaid_diagrams(md_file_path: str, relative_path: str) -> str:
70 """
71 Validate all Mermaid diagrams in a markdown file.
72
73 Args:
74 md_file_path: Path to the markdown file to check
75 relative_path: Relative path to the markdown file
76 Returns:
77 "All mermaid diagrams are syntax correct" if all diagrams are valid,
78 otherwise returns error message with details about invalid diagrams
79 """
80
81 try:
82 # Read the markdown file
83 file_path = Path(md_file_path)
84 if not file_path.exists():
85 return f"Error: File '{md_file_path}' does not exist"
86
87 content = file_path.read_text(encoding="utf-8")
88
89 # Extract all mermaid code blocks
90 mermaid_blocks = extract_mermaid_blocks(content)
91
92 if not mermaid_blocks:
93 return "No mermaid diagrams found in the file"
94
95 # Validate each mermaid diagram sequentially to avoid segfaults
96 errors = []
97 for i, (line_start, diagram_content) in enumerate(mermaid_blocks, 1):
98 error_msg = await validate_single_diagram(diagram_content, i, line_start)
99 if error_msg:
100 errors.append("\n")
101 errors.append(error_msg)
102
103 # if errors:
104 # logger.debug(f"Mermaid syntax errors found in file: {md_file_path}: {errors}")
105
106 if errors:
107 return (
108 "Mermaid syntax errors found in file: " + relative_path + "\n" + "\n".join(errors)
109 )
110 else:
111 return "All mermaid diagrams in file: " + relative_path + " are syntax correct"
112

Callers 4

str_replace_editorMethod · 0.90
_validate_mermaidFunction · 0.90
utils.pyFile · 0.85
str_replace_editorFunction · 0.85

Calls 2

extract_mermaid_blocksFunction · 0.85
validate_single_diagramFunction · 0.85

Tested by

no test coverage detected