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)
| 64 | # ------------------------------------------------------------ |
| 65 | |
| 66 | async 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 | |
| 112 | def extract_mermaid_blocks(content: str) -> List[Tuple[int, str]]: |
no test coverage detected