-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_to_mermaid_1.py
50 lines (37 loc) · 1.07 KB
/
markdown_to_mermaid_1.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
import re
def markdown_to_mermaid(markdown):
"""Converts Markdown to Mermaid.
Args:
markdown: A string containing Markdown text.
Returns:
A string containing Mermaid text.
"""
# Remove any leading or trailing whitespace.
markdown = markdown.strip()
# Split the Markdown into lines.
lines = markdown.splitlines()
# Create a Mermaid graph.
mermaid_graph = "( \n"
# Iterate over the Markdown lines.
for line in lines:
# Ignore any lines that start with a hash (#).
if line.startswith("#"):
continue
# Find the level of the heading (h1, h2, h3, etc.).
heading_level = re.search(r"^(\#{1,6}) (.+)$", line).group(1)
# Calculate the number of spaces to indent the heading.
indent = (heading_level - 1) * 2
# Add the heading to the Mermaid graph.
mermaid_graph += f" {' ' * indent}{heading_level}({line[len(heading_level) + 1:]}) \n"
# Close the Mermaid graph.
mermaid_graph += ")"
return mermaid_graph
# Print the Mermaid output.
print(markdown_to_mermaid(markdown="""
# h1
## h2
### h3
## h2
### h3
#### h4
"""))