Last active
August 8, 2020 08:22
-
-
Save Und3rf10w/b0c9222eecfa70015bb363c2350b3e1e to your computer and use it in GitHub Desktop.
Very quick and dirty script to fix image paths in boostnote for exporting them to obsidian. You may have to run this multiple times on the same file until you no longer get FileNotFoundErrors
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 re | |
import argparse | |
from shutil import copyfile | |
boostnote_attachment_base_dir = "/path/to/Boostnote/attachments/" | |
obsidian_note_attachment_dir = "/path/to/obsidian/notes/attachments/dir" | |
orig_image_dir = "/path/where/original/boostnote/photos/are/stored" | |
parser = argparse.ArgumentParser() | |
required_args = parser.add_argument_group("Required Arguments") | |
required_args.add_argument("-f", required=True, dest="input_file", help="Obsidian Markdown file to fix") | |
args = parser.parse_args() | |
with open(f"{args.input_file}", 'r+') as f: | |
old = f.read() | |
matches = re.findall(r"!\[(?P<orig_filename>.*)]\(:storage/(?P<dir>.*)/(?P<img_file>.*\.png)\)", old) | |
for match in matches: | |
try: | |
print(f"Replacing {match[1]}/{match[2]}") | |
copyfile(f"{boostnote_attachment_base_dir}{match[1]}/{match[2]}", f"{obsidian_note_attachment_dir}/{match[2]}") | |
old = re.sub(fr"!\[.*]\(:storage/{match[1]}/{match[2]}\)", f"![[{match[2]}]]", old) | |
except FileNotFoundError as e: | |
print(e) | |
print(f"Matches {match[0]}") | |
copyfile(fr"{orig_image_dir}{match[0]}", f"{boostnote_attachment_base_dir}{match[1]}/{match[2]}") | |
continue | |
f.seek(0) | |
f.write(old) | |
f.truncate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment