This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
generated from jobindjohn/obsidian-publish-mkdocs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_unused_media.py
65 lines (53 loc) · 2.05 KB
/
find_unused_media.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pathlib import Path
import os
import argparse
from typing import Optional
import yaml
def find_unused_media(img_path: Optional[Path] = None, dry_run: bool = False):
# load mkdocs.yml
with open("mkdocs.yml", "r", encoding="utf-8") as f:
# remove all !! pairs from the yaml file
data = f.read()
data = data.replace("!!", "")
config = yaml.safe_load(data)
docs_dir = Path.cwd() / Path(config.get("docs_dir", "docs"))
assets_dir = Path(docs_dir, config["extra"]["attachments"])
print(f"Looking for unused images in {assets_dir}...")
if img_path:
assets_dir = img_path
images = [
file
for file in assets_dir.rglob("*")
if file.is_file() and file.suffix in [".png", ".jpg", ".jpeg", ".gif", ".svg"]
]
md_files = [file for file in docs_dir.rglob("*.md") if file.is_file()]
# Search for images in markdown files
used_images = []
for md_file in md_files:
for image in images:
with open(md_file, "r", encoding="utf-8") as f:
if image.name in f.read():
used_images.append(image)
# compare the two lists
unused_images = [image for image in images if image not in used_images]
# delete unused images
if unused_images:
print(f"Found {len(unused_images)} unused images in {assets_dir}. Deleting...")
for image in unused_images:
if not dry_run:
print(image)
os.remove(image)
else:
print(f"Would delete {image}")
else:
print(f"Found no unused images in {assets_dir}.")
if __name__ == "__main__":
# use argparse to get the path to the assets folder
parser = argparse.ArgumentParser()
parser.add_argument("--path", type=str, help="Path to the assets folder")
parser.add_argument(
"--dry-run", action="store_true", help="Do not delete unused images"
)
args = parser.parse_args()
path = Path(args.path) if args.path else None
find_unused_media(img_path=path, dry_run=args.dry_run)