Skip to content

Instantly share code, notes, and snippets.

@digitalsignalperson
Created January 14, 2025 06:12
Show Gist options
  • Save digitalsignalperson/7d86bb1a57911a6ea817db98070f723f to your computer and use it in GitHub Desktop.
Save digitalsignalperson/7d86bb1a57911a6ea817db98070f723f to your computer and use it in GitHub Desktop.
obsidian bookmarks to markdown
#!/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