Skip to content

Instantly share code, notes, and snippets.

@thiborose
Created October 16, 2023 20:09
Show Gist options
  • Select an option

  • Save thiborose/8de91bd69440c1504292338995928ded to your computer and use it in GitHub Desktop.

Select an option

Save thiborose/8de91bd69440c1504292338995928ded to your computer and use it in GitHub Desktop.
Convert each markdown file from an obsidian vault to html, preserving formatting and images. You can then import the vault with Apple Notes.
import os
import re
# regex to match the image format of Obsidian
old_format_pattern = r'!\[\[([\w\s.-]+)\]\]'
# Define a function to replace the obsidian format with the standard markdown format
def replace_image_links(file_path, match):
relative_filename = match.group(1)
absolute_filename = os.path.abspath(os.path.join(os.path.dirname(file_path), relative_filename))
return f'![image]({absolute_filename})'
# Recursively process Markdown files in a directory
def process_markdown_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
with open(file_path, 'r') as f:
content = f.read()
new_content = re.sub(old_format_pattern, lambda match: replace_image_links(file_path, match), content)
with open(file_path, 'w') as f:
f.write(new_content)
# Specify the top-level directory where you want to start the process
top_directory = 'path_to_vault'
# Call the function to process Markdown files in all subfolders
for root, dirs, files in os.walk(top_directory):
for dir in dirs:
folder_path = os.path.join(root, dir)
process_markdown_files(folder_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment