Created
January 14, 2025 06:12
-
-
Save digitalsignalperson/7d86bb1a57911a6ea817db98070f723f to your computer and use it in GitHub Desktop.
obsidian bookmarks to markdown
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
#!/bin/python | |
import os | |
import json | |
vault_path = "/mnt/0/obsidian/andy" # <--- SET THIS TO YOUR VAULT PATH | |
output_path = os.path.join(vault_path, "bookmarks.md") # <--- SET TO OUTPUT MARKDOWN FILE | |
format_as_list = False # <--- set to True or False to change output format | |
bookmarks_path = os.path.join(vault_path, ".obsidian/bookmarks.json") | |
with open(bookmarks_path, "r") as f: | |
data = json.load(f) | |
lines = [] | |
items = [data['items'].copy()] | |
deindented = False | |
while items: | |
while items and len(items[-1]) == 0: | |
items = items[:-1] | |
deindented = True | |
if not items: | |
break | |
item = items[-1].pop(0) | |
level = len(items) | |
indent = ' ' * 4 * (level - 1) | |
line_prefix = indent + '- ' if format_as_list else '' | |
heading = '#' * level | |
if item['type'] == 'file': | |
if deindented and not format_as_list: | |
# close off nested headings when not immediately followed by a new heading | |
deindented = False | |
lines += [f"\n{heading}"] | |
path = item['path'] | |
if path.endswith('.md'): | |
path = path[:-3] | |
lines += [f"{line_prefix}[[{path}]]"] | |
elif item['type'] == 'group': | |
deindented = False | |
if format_as_list: | |
lines += [f"{line_prefix}{item['title']}"] | |
else: | |
lines += [f"\n{heading} {item['title']}"] | |
items.append(item['items'].copy()) | |
for line in lines: | |
print(line) | |
with open(output_path, 'w') as f: | |
for line in lines: | |
if format_as_list and not line: | |
continue | |
f.write(line + '\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment