|
8 | 8 | from anthropic._exceptions import RateLimitError
|
9 | 9 | from pydantic import BaseModel
|
10 | 10 | from functools import lru_cache
|
| 11 | +import re |
11 | 12 |
|
12 | 13 | load_dotenv()
|
13 | 14 |
|
@@ -122,11 +123,13 @@ async def generate(request: Request, body: ApiRequest):
|
122 | 123 | if "BAD_INSTRUCTIONS" in mermaid_code:
|
123 | 124 | return {"error": "Invalid or unclear instructions provided"}
|
124 | 125 |
|
125 |
| - # Process the diagram text before sending to client |
126 |
| - processed_diagram = mermaid_code\ |
127 |
| - .replace("[username]", body.username)\ |
128 |
| - .replace("[repo]", body.repo)\ |
129 |
| - .replace("[branch]", default_branch) |
| 126 | + # Process click events to include full GitHub URLs |
| 127 | + processed_diagram = process_click_events( |
| 128 | + mermaid_code, |
| 129 | + body.username, |
| 130 | + body.repo, |
| 131 | + default_branch |
| 132 | + ) |
130 | 133 |
|
131 | 134 | print("component_mapping_text:", component_mapping_text)
|
132 | 135 |
|
@@ -167,3 +170,28 @@ async def get_generation_cost(request: Request, body: ApiRequest):
|
167 | 170 | return {"cost": cost_string}
|
168 | 171 | except Exception as e:
|
169 | 172 | return {"error": str(e)}
|
| 173 | + |
| 174 | + |
| 175 | +def process_click_events(diagram: str, username: str, repo: str, branch: str) -> str: |
| 176 | + """ |
| 177 | + Process click events in Mermaid diagram to include full GitHub URLs. |
| 178 | + Detects if path is file or directory and uses appropriate URL format. |
| 179 | + """ |
| 180 | + def replace_path(match): |
| 181 | + # Extract the path from the click event |
| 182 | + path = match.group(2).strip('"\'') |
| 183 | + |
| 184 | + # Determine if path is likely a file (has extension) or directory |
| 185 | + is_file = '.' in path.split('/')[-1] |
| 186 | + |
| 187 | + # Construct GitHub URL |
| 188 | + base_url = f"https://github.com/{username}/{repo}" |
| 189 | + path_type = "blob" if is_file else "tree" |
| 190 | + full_url = f"{base_url}/{path_type}/{branch}/{path}" |
| 191 | + |
| 192 | + # Return the full click event with the new URL |
| 193 | + return f'click {match.group(1)} "{full_url}"' |
| 194 | + |
| 195 | + # Match click events: click ComponentName "path/to/something" |
| 196 | + click_pattern = r'click ([^\s"]+)\s+"([^"]+)"' |
| 197 | + return re.sub(click_pattern, replace_path, diagram) |
0 commit comments