Created
February 9, 2022 17:20
-
-
Save Vonter/a394df6964e892d904863e64ffe90009 to your computer and use it in GitHub Desktop.
Script to convert org-roam v2 .org files to Obsidian .md files. Definitely missing a lot of required features, but gets the basic job done as I needed.
This file contains hidden or 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
| import glob, os | |
| import fileinput | |
| import re | |
| import shutil | |
| dict_file_id = {} | |
| # Rename file to the title of the markdown | |
| for filename in glob.iglob('**', recursive=True): | |
| if os.path.isfile(filename): # filter dirs | |
| if '.md' in filename: | |
| with open(filename, 'r') as file_fd: | |
| lines = file_fd.readlines() | |
| for line in lines: | |
| if '#+title' in line: | |
| file_path = "/".join(filename.split("/")[:-1]) | |
| original_filename = "/".join([file_path, filename.split("/")[-1]]) | |
| new_filename = "/".join([file_path, line.replace('#+title: ', '').rstrip('\n').replace("/", ">")]) + ".md" | |
| shutil.move(original_filename, new_filename) | |
| # Get list of all IDs and create a dictionary to map IDs to filenames | |
| for filename in glob.iglob('**', recursive=True): | |
| if os.path.isfile(filename): # filter dirs | |
| if '.md' in filename: | |
| with open(filename, 'r') as file_fd: | |
| lines = file_fd.readlines() | |
| for line in lines: | |
| if ':ID:' in line: | |
| node_id = line.split(" ")[1:][-1].rstrip('\n') | |
| dict_file_id[node_id] = '(' + filename.replace(' ', '%20') + ')' | |
| for filename in glob.iglob('**', recursive=True): | |
| if os.path.isfile(filename): # filter dirs | |
| if '.md' in filename: | |
| for i in range(0, 9): | |
| with open(filename, 'r+') as file_fd: | |
| #for line in file_fd.readlines(): | |
| for line in fileinput.input(filename, inplace = True): | |
| # Convert node ID tags | |
| if ':ID:' in line and i == 0: | |
| node_id = line.split(" ")[1:][-1].rstrip('\n') | |
| if node_id in dict_file_id.keys(): | |
| print(line.replace(node_id, dict_file_id[node_id]).rstrip('\n')) | |
| pass | |
| # Convert ID links | |
| elif '[id:' in line: | |
| node_id = re.search('.*(\[id\:.*)\]\[.*', line).group(1).rstrip('\n') | |
| node_id = node_id.split(":")[1:][-1].rstrip('\n') | |
| node_tag = re.search('.*(\[.*\]\]).*', line).group(1).rstrip('\n') | |
| node_tag_edit = node_tag[:-1] | |
| print(line.replace('[[id:' + node_id + ']', node_tag_edit).rstrip('\n').replace(node_tag, dict_file_id[node_id])) | |
| # Convert title to h1 for obsidian to use as node name | |
| elif '#+title' in line: | |
| print(line.replace('+title:', '').rstrip('\n')) | |
| # Convert filetags to obsidian tags | |
| elif '#+filetags' in line: | |
| print(line.replace('#+filetags: ', 'Tags ').replace(':', ' #').rstrip('\n')[:-1]) | |
| # Lines with no change | |
| else: | |
| print(line.rstrip('\n')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment