Created
September 21, 2024 10:29
-
-
Save alwashali/69b11701be6a666057b5fc2a9a8c6a59 to your computer and use it in GitHub Desktop.
Delete obsidian notes with all the referenced images
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
""" | |
Script to delete all images referenced in Markdown files within a specified notes path. | |
It scans the Markdown files for Obsidian image syntax and deletes all notes with the referenced images. | |
""" | |
import os | |
import re | |
import argparse | |
def delete_by_path(file_path): | |
""" | |
Delete file from the file system by its path. | |
""" | |
try: | |
if os.path.exists(file_path): | |
os.remove(file_path) | |
print(f"Deleted: {file_path}") | |
else: | |
print(f"File {file_path} not found.") | |
except OSError as error: | |
print(f"Error deleting {file_path}: {error}") | |
def get_image_files_dict(folder_path): | |
""" | |
Receives a folder path containing images and returns a dictionary | |
with image file names (with extension) as keys and full file paths as values. | |
Only image files with common extensions are considered. | |
""" | |
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'} | |
image_files_dict = {} | |
for file_name in os.listdir(folder_path): | |
file_path = os.path.join(folder_path, file_name) | |
if os.path.isfile(file_path): | |
file_ext = os.path.splitext(file_name)[1].lower() | |
if file_ext in image_extensions: | |
image_files_dict[file_name] = file_path | |
return image_files_dict | |
def delete_notes_with_images(folder_path, image_files_dict): | |
""" | |
Traverses the folder and subdirectories to find markdown files. | |
For each markdown file, searches for images with the syntax ![[image.png|title]]. | |
If an image is found and exists in image_files_dict, it deletes the image file. | |
""" | |
# Regex to match image syntax ![[image.png|title]] in markdown files | |
image_pattern = re.compile(r"!\[\[(.+?)\|.+?\]\]") | |
for root, _, files in os.walk(folder_path): | |
for file_name in files: | |
if file_name.endswith('.md'): | |
file_path = os.path.join(root, file_name) | |
print(file_path) | |
with open(file_path, 'r', encoding='utf-8') as md_file: | |
content = md_file.read() | |
matches = image_pattern.findall(content) | |
for image_name in matches: | |
if image_name in image_files_dict: | |
image_path = image_files_dict[image_name] | |
delete_by_path(image_path) | |
delete_by_path(file_path) | |
def main(): | |
""" | |
Main function to parse command-line arguments and trigger the deletion process. | |
""" | |
parser = argparse.ArgumentParser(description="Delete all images referenced in Markdown notes.") | |
parser.add_argument("notes_path", help="Path to the folder containing markdown notes.") | |
parser.add_argument("files_root", help="Path to the root folder containing image files.") | |
args = parser.parse_args() | |
images = get_image_files_dict(args.files_root) | |
delete_notes_with_images(args.notes_path, images) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment