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

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