-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_to_mermaid_2.py
69 lines (52 loc) · 1.71 KB
/
markdown_to_mermaid_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import re
def markdown_to_mermaid(markdown_text):
"""Converts Markdown to Mermaid code.
Args:
markdown_text: A string containing Markdown text.
Returns:
A string containing Mermaid code.
"""
# Split the Markdown text into lines.
current_heading_level = 0
lines = markdown_text.splitlines()
# Create a Mermaid code string.
mermaid_code = "(\n"
# Iterate over the lines of Markdown text.
for line in lines:
# Remove the leading whitespace.
line = line.lstrip()
# Ignore empty lines.
if not line:
continue
# Get the heading level.
heading_level = len(re.findall(r"^#", line))
# If the heading level is greater than the current heading level, add a
# new Mermaid code block for the heading.
if heading_level > current_heading_level:
mermaid_code += " " * (heading_level - 1) + f"{line}(\n"
current_heading_level = heading_level
# If the heading level is less than the current heading level, close the
# Mermaid code block for the current heading.
elif heading_level < current_heading_level:
mermaid_code += " " * (current_heading_level - heading_level) + ")\n"
current_heading_level = heading_level
# Otherwise, the heading level is the same as the current heading level,
# so just add the line to the Mermaid code string.
else:
mermaid_code += " " * heading_level + line + "\n"
# Close the remaining Mermaid code blocks.
for i in range(current_heading_level):
mermaid_code += " " * i + ")\n"
# Return the Mermaid code string.
return mermaid_code
# Example usage:
markdown_text = """
# h1
## h2
### h3
## h2
### h3
#### h4
"""
mermaid_code = markdown_to_mermaid(markdown_text)
print(mermaid_code)