Skip to content

Commit 5cd2e3a

Browse files
committed
fix: Remove markdown code fences in HTML conversion and improve solution link generation
- Add code fence removal in convert_to_html.py to prevent HTML conversion errors - Improve _build_problems_lookup to better match problem IDs from various sources - Enhance _add_github_solution_links to correctly add Solution links when solution files exist - Solution links now properly generated as: [LeetCode X](url) | [Solution](github_url)
1 parent 6bd460d commit 5cd2e3a

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

tools/ai-markmap-agent/convert_to_html.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ def convert_file_to_html(
135135
title = f"{input_path.stem.replace('_', ' ').title()} - NeetCode Mind Maps"
136136

137137
markdown_content = input_path.read_text(encoding="utf-8")
138+
139+
# Remove markdown code fence if present (LLM sometimes wraps output)
140+
markdown_content = markdown_content.strip()
141+
if markdown_content.startswith("```markdown"):
142+
markdown_content = markdown_content[len("```markdown"):].strip()
143+
if markdown_content.startswith("```md"):
144+
markdown_content = markdown_content[len("```md"):].strip()
145+
if markdown_content.startswith("```"):
146+
markdown_content = markdown_content[3:].strip()
147+
if markdown_content.endswith("```"):
148+
markdown_content = markdown_content[:-3].strip()
149+
138150
converter = StandaloneHTMLConverter(template_path=template_path)
139151
html_content = converter.convert(markdown_content, title=title)
140152
output_path.write_text(html_content, encoding="utf-8")

tools/ai-markmap-agent/src/post_processing.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,35 @@ def _build_problems_lookup(self, problems: dict[str, Any]) -> dict[str, dict]:
6464

6565
for key, value in problems.items():
6666
if isinstance(value, dict):
67-
problem_id = value.get("id", key)
68-
# Normalize ID to 4 digits
69-
if isinstance(problem_id, str) and problem_id.isdigit():
70-
problem_id = problem_id.zfill(4)
71-
lookup[problem_id] = value
72-
# Also store without leading zeros for flexibility
73-
try:
74-
lookup[str(int(problem_id))] = value
75-
except (ValueError, TypeError):
76-
pass
67+
# Try to get ID from various sources
68+
problem_id = value.get("id") or value.get("leetcode_id")
69+
70+
# If no ID found, try to extract from key (slug format: "0079_word_search")
71+
if not problem_id:
72+
# Extract ID from slug if it starts with digits
73+
match = re.match(r'^(\d+)_', key)
74+
if match:
75+
problem_id = match.group(1)
76+
77+
# If still no ID, use key as fallback (but this is less reliable)
78+
if not problem_id:
79+
problem_id = key
80+
81+
# Normalize ID to string and ensure 4 digits
82+
if isinstance(problem_id, int):
83+
problem_id = str(problem_id)
84+
elif not isinstance(problem_id, str):
85+
problem_id = str(problem_id)
86+
87+
# Store with 4-digit format
88+
if problem_id.isdigit():
89+
normalized_id = problem_id.zfill(4)
90+
lookup[normalized_id] = value
91+
# Also store without leading zeros for flexibility
92+
lookup[problem_id] = value
93+
# Also store as integer string if different
94+
if normalized_id != problem_id:
95+
lookup[str(int(problem_id))] = value
7796

7897
return lookup
7998

@@ -283,7 +302,12 @@ def add_solution_link(match: re.Match) -> str:
283302
problem_id = id_match.group(1)
284303

285304
# Look up problem in our data
286-
problem = self.problems_lookup.get(problem_id.zfill(4)) or self.problems_lookup.get(problem_id)
305+
# Try both 4-digit format and numeric format
306+
problem = (
307+
self.problems_lookup.get(problem_id.zfill(4)) or
308+
self.problems_lookup.get(problem_id) or
309+
self.problems_lookup.get(str(int(problem_id)).zfill(4)) if problem_id.isdigit() else None
310+
)
287311
if not problem:
288312
return full_text
289313

0 commit comments

Comments
 (0)