-
-
Save VFansss/b4d57064ba60d11a1932ac593269e82f to your computer and use it in GitHub Desktop.
Converts dendron note structure to obsidian note structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# converts dendron note structure to obsidian note structure | |
# - removes frontmatter | |
# - adds tags | |
# - converts internal links | |
# - ignores empty (frontmatter-only) notes | |
# | |
# use at your own risk | |
# !!!!! | |
# | |
# Remember to manually copy any single media manually from Dendron folder to Obsidian Vault! | |
# | |
# !!!!! | |
import glob | |
import re | |
import os | |
# Replace Vaults paths with your owns | |
# Note: leave r before path string to avoid sanitizing slash and backslash | |
PATH_TO_DENDRON_VAULT = r"C:\Users\Alex\Desktop\dendron-vault" | |
PATH_OBSIDIAN_VAULT = r"C:\Users\Alex\Desktop\obsidian" | |
TAGS = { | |
"study": "#study", | |
"study.k8s": "#k8s", | |
"study.k8s.ckad": "#ckad", | |
# ... | |
"tools": "#tools", | |
"tools.vscode": "#vscode", | |
} | |
def to_wikilink(dendron_link): | |
segments = dendron_link.split("#") | |
link_components = segments[0].split(".") | |
if len(segments) == 1: | |
link = dendron_link | |
appendix = "" | |
note_name = link_components[-1] | |
else: | |
link = segments[0] | |
appendix = f"#{'#'.join(segments[1:])}" | |
note_name = segments[-1] | |
obsidian_link = link.replace(".", "/") | |
return f"{obsidian_link}.md{appendix}|{note_name}" | |
def _cleanse_markdown(dendron_path): | |
with open(dendron_path, "r", encoding="utf8") as f: | |
raw_content = f.read() | |
# remove frontmatter | |
blocks = raw_content.split("---\n") | |
md_content = "---\n".join(blocks[2:]) | |
# add tags | |
file_name = os.path.basename(dendron_path) | |
if not any([tag in file_name for tag in ["daily", "monthly"]]) and not _is_empty( | |
md_content | |
): | |
tags = [v for k, v in TAGS.items() if file_name.startswith(k)] | |
md_content = f"{' '.join(tags)}\n{md_content}" | |
# convert internal links | |
for match in re.findall(r"\[\[(.*?)\]\]", md_content): | |
md_content = md_content.replace(f"[[{match}]]", f"[[{to_wikilink(match)}]]") | |
first_non_newline = None | |
for i, char in enumerate(md_content): | |
if char != '\n': | |
first_non_newline = i | |
break | |
if first_non_newline is not None: | |
md_content = md_content[first_non_newline:].lstrip('\n') | |
return md_content | |
def _is_empty(md_content): | |
return len(md_content.strip()) == 0 | |
def _convert_dendron_note(dendron_path): | |
dendron_filename = os.path.basename( | |
dendron_path | |
) # tech.powershell.commands.md | cmd.journal.daily.2023.03.09.md | cmd.journal.monthly.2022.10.md | |
if "daily" in dendron_filename: | |
obs_dirs = "journal/daily" | |
date_part = dendron_filename.split(".")[-4:-1] | |
obs_name = "-".join(date_part) + ".md" | |
elif "monthly" in dendron_filename: | |
year = dendron_filename.split(".")[-3] | |
obs_dirs = f"journal/monthly/{year}" | |
obs_name = dendron_filename.split(".")[-2] + ".md" | |
print(obs_dirs, obs_name) | |
else: | |
obs_dirs = "/".join(dendron_filename.split(".")[:-2]) | |
obs_name = f"{dendron_filename.split('.')[-2]}.md" | |
return obs_dirs, obs_name | |
def _create_note(obs_dirs, obs_note, md_content): | |
if not os.path.exists(f"{PATH_OBSIDIAN_VAULT}/{obs_dirs}"): | |
os.makedirs(f"{PATH_OBSIDIAN_VAULT}/{obs_dirs}") | |
with open(f"{PATH_OBSIDIAN_VAULT}/{obs_dirs}/{obs_note}", "w", encoding="utf8") as f: | |
f.write(md_content) | |
if __name__ == "__main__": | |
for filepath in glob.iglob(f"{PATH_TO_DENDRON_VAULT}/*.md"): | |
md_content = _cleanse_markdown(filepath) | |
if not _is_empty(md_content): | |
obs_dirs, obs_name = _convert_dendron_note(filepath) | |
_create_note(obs_dirs, obs_name, md_content) | |
print(filepath, "converted to", f"{obs_dirs}/{obs_name}") | |
else: | |
print("Skipping empty file", filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment