-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
113 lines (94 loc) · 3.5 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import re
import os
class ObsidianLink:
text: str
content: str
def __init__(self, text, content):
self.text = text
self.content = content
def main():
print("Please select your logseq folder with files to change.")
logseq_dir = input("Input: ")
# print(logseq_dir)
if logseq_dir.startswith('"'):
logseq_dir = logseq_dir[1:]
if logseq_dir.endswith('"'):
logseq_dir = logseq_dir[:-1]
if not os.path.exists(logseq_dir):
print("Folder doesn't exist!")
return
pages_dir = os.path.join(logseq_dir, "pages")
if not os.path.exists(pages_dir):
print("Sub-Folder /pages doesn't exist!")
return
pages = []
page_names = []
for f in os.scandir(pages_dir):
if not f.is_dir() and f.name.endswith(".md"):
pages.append(f)
page_names.append(f.name)
for f in pages:
if ".excalidraw." not in f.name:
print(f"Converting '{f.name}'...")
file_content = ""
with open(f.path, mode="r") as file:
file_content = file.read()
image_links, exca_links, other_links = extract_links(
file_content, page_names
)
new_content = file_content
for link in image_links:
new_content = new_content.replace(
link.text, str(convert_image_link(link))
)
for link in exca_links:
new_content = new_content.replace(
link.text, str(convert_excalidraw_link(link, page_names))
)
for link in other_links:
new_content = new_content.replace(link.text, link.text[1:])
with open(f.path, mode="w") as file:
file.write(new_content)
def extract_links(text: str, page_names: list):
"""
Links need to have the format ![[content]]
Returns image_links, exca_links, other_links"""
image_links = set()
exca_links = set()
other_links = set()
regex = r"(!\[\[([^\]]+)\]\])"
matches = re.finditer(regex, text, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
link = match.group(0)
link_content = match.group(2)
if (
link_content.endswith(".png")
or link_content.endswith(".jpg")
or link_content.endswith(".jpeg")
or link_content.endswith(".webp")
or link_content.endswith(".svg")
):
image_links.add(ObsidianLink(link, link_content))
elif (
link_content.endswith(".excalidraw")
or link_content + ".excalidraw.md" in page_names
or link_content + ".excalidraw" in page_names
):
exca_links.add(ObsidianLink(link, link_content))
else:
other_links.add(ObsidianLink(link, link_content))
return list(image_links), list(exca_links), list(other_links)
def convert_image_link(image_link: ObsidianLink) -> str:
link_name = image_link.content
return f"![{link_name}](../assets/{image_link.content})"
def convert_excalidraw_link(exca_link: ObsidianLink, page_names: list) -> str:
link_name = exca_link.content
if link_name.endswith(".excalidraw"):
link_name = link_name[:-11]
elif link_name.endswith(".excalidraw.md"):
link_name = link_name[:-14]
elif link_name.endswith(".md"):
link_name = link_name[:-3]
return f"{{{{renderer excalidraw, {link_name}}}}}"
if __name__ == "__main__":
main()